Method: Cisco::CmdRef#define_helper

Defined in:
lib/cisco_node_utils/command_reference.rb

#define_helper(method_name, base_hash) ⇒ Object

Create a helper method for generating the getter/setter values. This method will automatically handle wildcard arguments.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cisco_node_utils/command_reference.rb', line 179

def define_helper(method_name, base_hash)
  # Which kind of wildcards (if any) do we need to support?
  combined = []
  base_hash.each_value do |v|
    combined += v if v.is_a?(Array)
    combined << v if v.is_a?(String)
  end
  key_value = combined.any? { |i| i.is_a?(String) && /<\S+>/ =~ i }
  printf = combined.any? { |i| i.is_a?(String) && /%/ =~ i }

  if key_value && printf
    fail 'Invalid mixture of key-value and printf wildcards ' \
         "in #{method_name}: #{combined}"
  elsif key_value
    define_key_value_helper(method_name, base_hash)
  elsif printf
    arg_count = combined.join.scan(/%/).length
    define_printf_helper(method_name, base_hash, arg_count)
  else
    # simple static token(s)
    define_static_helper(method_name, base_hash)
  end
  @hash[method_name] = true
end