Class: ConvenientService::Support::MethodParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/convenient_service/support/method_parameters.rb

Overview

Note:

‘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

Instance Method Summary collapse

Constructor Details

#initialize(method_parameters) ⇒ void

Parameters:

  • method_parameters (Array)


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_parametersObject (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`.

Examples:

‘rest_kwargs` means NOT named kwargs.


def foo(a, b = 1, *args, c:, d: 2, **kwargs, &block)
end

def bar(a, b = 1, *args, c:, d: 2, &block)
end

ConvenientService::Support::MethodParameters.new(method(:foo).parameters).has_rest_kwargs?
# => true

ConvenientService::Support::MethodParameters.new(method(:bar).parameters).has_rest_kwargs?
# => false

Returns:

  • (Boolean)

See Also:



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_keysArray<Symbol>

Note:

‘named_kwargs_keys` is an optimized way to get `required_kwargs_keys + optional_kwargs_keys`.

Examples:

‘named_kwargs_keys` are `kwargs` with or without `defaults`.


def foo(a, b = 1, *args, c:, d: 2, **kwargs, &block)
end

ConvenientService::Support::MethodParameters.new(method(:foo).parameters).named_kwargs_keys
# => [:c, :d]

Returns:

  • (Array<Symbol>)

See Also:



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_keysArray<Symbol>

Note:

‘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_keysArray<Symbol>

Note:

‘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