Class: ConvenientService::Support::MethodParameters
- Inherits:
-
Object
- Object
- ConvenientService::Support::MethodParameters
- Defined in:
- lib/convenient_service/support/method_parameters.rb
Overview
‘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
Returns the value of attribute method_parameters.
Instance Method Summary collapse
-
#has_rest_kwargs? ⇒ Boolean
Returns ‘true` when method definition has `**kwargs`.
- #initialize(method_parameters) ⇒ void constructor
- #named_kwargs_keys ⇒ Array<Symbol>
- #optional_kwargs_keys ⇒ Array<Symbol>
- #required_kwargs_keys ⇒ Array<Symbol>
Constructor Details
#initialize(method_parameters) ⇒ void
38 39 40 |
# File 'lib/convenient_service/support/method_parameters.rb', line 38 def initialize(method_parameters) @method_parameters = method_parameters end |
Instance Attribute Details
#method_parameters ⇒ Object (readonly)
Returns the value of attribute method_parameters.
32 33 34 |
# File 'lib/convenient_service/support/method_parameters.rb', line 32 def method_parameters @method_parameters end |
Instance Method Details
#has_rest_kwargs? ⇒ Boolean
Returns ‘true` when method definition has `**kwargs`.
62 63 64 65 66 |
# File 'lib/convenient_service/support/method_parameters.rb', line 62 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>
‘named_kwargs_keys` is an optimized way to get `required_kwargs_keys + optional_kwargs_keys`.
82 83 84 |
# File 'lib/convenient_service/support/method_parameters.rb', line 82 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>
‘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]
116 117 118 |
# File 'lib/convenient_service/support/method_parameters.rb', line 116 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>
‘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]
99 100 101 |
# File 'lib/convenient_service/support/method_parameters.rb', line 99 def required_kwargs_keys @required_kwargs_keys ||= method_parameters.select { |type, _name| type == Constants::Types::REQUIRED_KEYWORD }.map { |_type, name| name } end |