Class: ConvenientService::Support::MethodParameters Private
- Inherits:
-
Object
- Object
- ConvenientService::Support::MethodParameters
- Defined in:
- lib/convenient_service/support/method_parameters.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Method#parameters return value may look like the following.
def foo(a, b = 1, *args, c:, d: 2, **kwargs, &block) end
method(:foo).parameters
=> [[:req, :a], [:opt, :b], [:rest, :args], [:keyreq, :c], [:key, :d], [:keyrest, :kwargs], [:block, :block]]
Wrapps Method#parameters return value to provide a higher level interface.
Defined Under Namespace
Modules: Constants
Instance Attribute Summary collapse
- #method_parameters ⇒ Object readonly private
Instance Method Summary collapse
-
#has_rest_kwargs? ⇒ Boolean
private
Returns
truewhen method definition has**kwargs. - #initialize(method_parameters) ⇒ void constructor private
- #named_kwargs_keys ⇒ Array<Symbol> private
- #optional_kwargs_keys ⇒ Array<Symbol> private
- #required_kwargs_keys ⇒ Array<Symbol> private
Constructor Details
#initialize(method_parameters) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
43 44 45 |
# File 'lib/convenient_service/support/method_parameters.rb', line 43 def initialize(method_parameters) @method_parameters = method_parameters end |
Instance Attribute Details
#method_parameters ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
37 38 39 |
# File 'lib/convenient_service/support/method_parameters.rb', line 37 def method_parameters @method_parameters end |
Instance Method Details
#has_rest_kwargs? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true when method definition has **kwargs.
67 68 69 70 71 |
# File 'lib/convenient_service/support/method_parameters.rb', line 67 def has_rest_kwargs? return @has_rest_kwargs if defined? @has_rest_kwargs @has_rest_kwargs = method_parameters.any? { |type, _name| type == Constants::Types::REST_KEYWORDS } end |
#named_kwargs_keys ⇒ Array<Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
named_kwargs_keys is an optimized way to get required_kwargs_keys + optional_kwargs_keys.
87 88 89 |
# File 'lib/convenient_service/support/method_parameters.rb', line 87 def named_kwargs_keys @named_kwargs_keys ||= method_parameters.select { |type, _name| type == Constants::Types::REQUIRED_KEYWORD || type == Constants::Types::OPTIONAL_KEYWORD }.map { |_type, name| name } end |
#optional_kwargs_keys ⇒ Array<Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
optional_kwargs are named kwargs with defaults.
def foo(a, b = 1, *args, c:, d: 2, **kwargs, &block) end
ConvenientService::Support::MethodParameters.new(method(:foo).parameters).optional_kwargs_keys
=> [:d]
121 122 123 |
# File 'lib/convenient_service/support/method_parameters.rb', line 121 def optional_kwargs_keys @optional_kwargs_keys ||= method_parameters.select { |type, _name| type == Constants::Types::OPTIONAL_KEYWORD }.map { |_type, name| name } end |
#required_kwargs_keys ⇒ Array<Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
required_kwargs are named kwargs without defaults.
def foo(a, b = 1, *args, c:, d: 2, **kwargs, &block) end
ConvenientService::Support::MethodParameters.new(method(:foo).parameters).required_kwargs_keys
=> [:c]
104 105 106 |
# File 'lib/convenient_service/support/method_parameters.rb', line 104 def required_kwargs_keys @required_kwargs_keys ||= method_parameters.select { |type, _name| type == Constants::Types::REQUIRED_KEYWORD }.map { |_type, name| name } end |