Class: HashWithDotAccess::Hash

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/hash_with_dot_access.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/hash_with_dot_access.rb', line 11

def method_missing(key, *args)
  if "#{key}".end_with?("=")
    self["#{key}".chop] = args.first
  elsif self.key?(key)
    self[key]
  else
    default
  end
end

Instance Method Details

#respond_to_missing?(key) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
# File 'lib/hash_with_dot_access.rb', line 5

def respond_to_missing?(key, *)
  return true if "#{key}".end_with?("=")

  key?[key]
end

#update(other_hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hash_with_dot_access.rb', line 21

def update(other_hash)
  if other_hash.is_a? HashWithDotAccess::Hash
    super(other_hash)
  else
    other_hash.to_hash.each_pair do |key, value|
      if block_given? && key?(key)
        value = yield(convert_key(key), self[key], value)
      end
      regular_writer(convert_key(key), convert_value(value))
    end
    self
  end
end