Class: Adyen::HashWithAccessors

Inherits:
Hash
  • Object
show all
Defined in:
lib/adyen/hash_with_accessors.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/adyen/hash_with_accessors.rb', line 8

def method_missing(method, *args)
  string_key = method.to_s.sub(/=\z/, '')
  sym_key = string_key.to_sym

  key = if has_key?(string_key)
    string_key
  elsif has_key?(sym_key)
    sym_key
  end

  return super unless key

  assignment = sym_key != method

  if assignment
    raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1)" unless args.size == 1

    self[key] = args.first
  else
    raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)" unless args.size == 0

    self[key]
  end
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ Boolean



33
34
35
36
# File 'lib/adyen/hash_with_accessors.rb', line 33

def respond_to_missing?(method, include_private = false)
  string_key = method.to_s.sub(/=\z/, '')
  has_key?(string_key) || has_key?(string_key.to_sym) || super
end