Class: Cuprum::Utils::ParametersMapping
- Inherits:
-
Object
- Object
- Cuprum::Utils::ParametersMapping
- Defined in:
- lib/cuprum/utils/parameters_mapping.rb
Overview
Utility class mapping a method’s parameters by parameter name.
Instance Attribute Summary collapse
-
#arguments ⇒ Hash{Symbol=>Integer}
readonly
The named arguments to the method, both required and optional, but excluding variadic args).
-
#block ⇒ Symbol?
readonly
The name of the block parameter, if any.
-
#keywords ⇒ Set<Symbol>
readonly
The keywords for the method, both required and optional, but excluding variadic keywords.
-
#variadic_arguments ⇒ Symbol
readonly
The name of the variadic arguments parameter, if any.
-
#variadic_keywords ⇒ Symbol
readonly
The name of the variadic keywords parameter, if any.
Class Method Summary collapse
-
.build(callable) ⇒ ParametersMapping
Generates a parameters mapping for the given method or Proc.
Instance Method Summary collapse
-
#arguments_count ⇒ Integer
The number of named arguments.
-
#block? ⇒ true, false
True if the method has a block parameter; otherwise false.
-
#call(*arguments, **keywords, &block) ⇒ Hash{Symbol=>Object}
Maps the given parameters to a Hash of parameter names and values.
-
#initialize(arguments: [], keywords: [], block: nil, variadic_arguments: nil, variadic_keywords: nil) ⇒ ParametersMapping
constructor
A new instance of ParametersMapping.
-
#variadic_arguments? ⇒ true, false
True if the method has a variadic arguments parameter; otherwise false.
-
#variadic_keywords? ⇒ true, false
True if the method has a variadic keywords parameter; otherwise false.
Constructor Details
#initialize(arguments: [], keywords: [], block: nil, variadic_arguments: nil, variadic_keywords: nil) ⇒ ParametersMapping
Returns a new instance of ParametersMapping.
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
#arguments ⇒ Hash{Symbol=>Integer} (readonly)
Returns 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 |
#block ⇒ Symbol? (readonly)
Returns the name of the block parameter, if any.
77 78 79 |
# File 'lib/cuprum/utils/parameters_mapping.rb', line 77 def block @block end |
#keywords ⇒ Set<Symbol> (readonly)
Returns 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_arguments ⇒ Symbol (readonly)
Returns 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_keywords ⇒ Symbol (readonly)
Returns 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.
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_count ⇒ Integer
Returns 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.
96 97 98 |
# File 'lib/cuprum/utils/parameters_mapping.rb', line 96 def block? !@block.nil? end |
#call(*arguments, **keywords, &block) ⇒ Hash{Symbol=>Object}
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.
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.
139 140 141 |
# File 'lib/cuprum/utils/parameters_mapping.rb', line 139 def variadic_keywords? !@variadic_keywords.nil? end |