Class: Cuprum::Utils::ParametersMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/utils/parameters_mapping.rb

Overview

Utility class mapping a method’s parameters by parameter name.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments: [], keywords: [], block: nil, variadic_arguments: nil, variadic_keywords: nil) ⇒ ParametersMapping

Returns a new instance of ParametersMapping.

Parameters:

  • arguments (Array<Symbol>) (defaults to: [])

    the named arguments to the method, both required and optional, but excluding variadic args).

  • block (Symbol, nil) (defaults to: nil)

    the name of the block parameter, if any.

  • keywords (Array<Symbol>) (defaults to: [])

    the keywords for the method, both required and optional, but excluding variadic keywords.

  • variadic_arguments (Symbol) (defaults to: nil)

    the name of the variadic arguments parameter, if any.

  • variadic_keywords (Symbol) (defaults to: nil)

    the name of the variadic keywords parameter, if any.



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cuprum/utils/parameters_mapping.rb', line 58

def initialize(
  arguments:          [],
  keywords:           [],
  block:              nil,
  variadic_arguments: nil,
  variadic_keywords:  nil
)
  @arguments          = arguments.map(&:to_sym).freeze
  @block              = block&.to_sym
  @keywords           = Set.new(keywords.map(&:to_sym)).freeze
  @variadic_arguments = variadic_arguments&.to_sym
  @variadic_keywords  = variadic_keywords&.to_sym
end

Instance Attribute Details

#argumentsHash{Symbol=>Integer} (readonly)

Returns the named arguments to the method, both required and optional, but excluding variadic args).

Returns:

  • (Hash{Symbol=>Integer})

    the named arguments to the method, both required and optional, but excluding variadic args).



74
75
76
# File 'lib/cuprum/utils/parameters_mapping.rb', line 74

def arguments
  @arguments
end

#blockSymbol? (readonly)

Returns the name of the block parameter, if any.

Returns:

  • (Symbol, nil)

    the name of the block parameter, if any.



77
78
79
# File 'lib/cuprum/utils/parameters_mapping.rb', line 77

def block
  @block
end

#keywordsSet<Symbol> (readonly)

Returns the keywords for the method, both required and optional, but excluding variadic keywords.

Returns:

  • (Set<Symbol>)

    the keywords for the method, both required and optional, but excluding variadic keywords.



81
82
83
# File 'lib/cuprum/utils/parameters_mapping.rb', line 81

def keywords
  @keywords
end

#variadic_argumentsSymbol (readonly)

Returns the name of the variadic arguments parameter, if any.

Returns:

  • (Symbol)

    the name of the variadic arguments parameter, if any.



84
85
86
# File 'lib/cuprum/utils/parameters_mapping.rb', line 84

def variadic_arguments
  @variadic_arguments
end

#variadic_keywordsSymbol (readonly)

Returns the name of the variadic keywords parameter, if any.

Returns:

  • (Symbol)

    the name of the variadic keywords parameter, if any.



87
88
89
# File 'lib/cuprum/utils/parameters_mapping.rb', line 87

def variadic_keywords
  @variadic_keywords
end

Class Method Details

.build(callable) ⇒ ParametersMapping

Generates a parameters mapping for the given method or Proc.

Parameters:

  • callable (#parameters)

    the method or Proc for which to map parameters.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cuprum/utils/parameters_mapping.rb', line 16

def self.build(callable) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
  arguments          = []
  block              = nil
  keywords           = []
  variadic_arguments = nil
  variadic_keywords  = nil

  callable.parameters.each do |(type, name)|
    next if name.nil?

    case type
    when :opt, :req
      arguments << name
    when :key, :keyreq
      keywords << name
    when :rest
      variadic_arguments = name
    when :keyrest
      variadic_keywords = name
    when :block
      block = name
    end
  end

  new(
    arguments:,
    block:,
    keywords:,
    variadic_arguments:,
    variadic_keywords:
  )
end

Instance Method Details

#arguments_countInteger

Returns the number of named arguments.

Returns:

  • (Integer)

    the number of named arguments.



90
91
92
# File 'lib/cuprum/utils/parameters_mapping.rb', line 90

def arguments_count
  @arguments_count ||= arguments.size
end

#block?true, false

Returns true if the method has a block parameter; otherwise false.

Returns:

  • (true, false)

    true if the method has a block parameter; otherwise false.



96
97
98
# File 'lib/cuprum/utils/parameters_mapping.rb', line 96

def block?
  !@block.nil?
end

#call(*arguments, **keywords, &block) ⇒ Hash{Symbol=>Object}

Maps the given parameters to a Hash of parameter names and values.

Parameters:

  • arguments (Array)

    the positional parameters to map.

  • keywords (Hash)

    the keyword parameters to map.

  • block (Proc)

    the block parameter to map, if any.

Returns:

  • (Hash{Symbol=>Object})

    the mapped parameters.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cuprum/utils/parameters_mapping.rb', line 108

def call(*args, **kwargs, &block_arg) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  params = {}
  extras = {}

  arguments.each.with_index { |name, index| params[name] = args[index] }

  if variadic_arguments?
    params[variadic_arguments] = args[arguments_count..] || []
  end

  keywords.each { |name| params[name] = nil }

  kwargs.each do |(name, value)|
    (keywords.include?(name) ? params : extras)[name] = value
  end

  params[variadic_keywords] = extras if variadic_keywords?

  params[block] = block_arg if block?

  params
end

#variadic_arguments?true, false

Returns true if the method has a variadic arguments parameter; otherwise false.

Returns:

  • (true, false)

    true if the method has a variadic arguments parameter; otherwise false.



133
134
135
# File 'lib/cuprum/utils/parameters_mapping.rb', line 133

def variadic_arguments?
  !@variadic_arguments.nil?
end

#variadic_keywords?true, false

Returns true if the method has a variadic keywords parameter; otherwise false.

Returns:

  • (true, false)

    true if the method has a variadic keywords parameter; otherwise false.



139
140
141
# File 'lib/cuprum/utils/parameters_mapping.rb', line 139

def variadic_keywords?
  !@variadic_keywords.nil?
end