Class: DotHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/extensions/dot_hash.rb

Instance Method Summary collapse

Methods inherited from Hash

#deep_transform_values, #deep_transform_values!, #stringify_keys

Methods included from DeepMergeable

#deep_merge, #deep_merge!, #deep_merge?

Constructor Details

#initialize(hash = {}) ⇒ DotHash

Returns a new instance of DotHash.



4
5
6
7
8
9
# File 'lib/extensions/dot_hash.rb', line 4

def initialize(hash = {})
  super()
  hash.each do |key, value|
    self[key] = value.is_a?(Hash) ? DotHash.new(value) : value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/extensions/dot_hash.rb', line 15

def method_missing(method_name, *args, &)
  key = method_name.to_s
  if key?(key)
    self[key]
  elsif key?(method_name)
    self[method_name]
  else
    super
  end
end

Instance Method Details

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

Returns:

  • (Boolean)


26
27
28
# File 'lib/extensions/dot_hash.rb', line 26

def respond_to_missing?(method_name, include_private = false)
  key?(method_name.to_s) || key?(method_name) || super
end

#to_hObject



11
12
13
# File 'lib/extensions/dot_hash.rb', line 11

def to_h
  transform_values { |value| value.is_a?(DotHash) ? value.to_h : value }
end