Class: Render::DottableHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/render/dottable_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#recursive_stringify_keys!, #recursive_symbolize_keys!, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/render/dottable_hash.rb', line 52

def method_missing(method, *arguments)
  if method.match(/\=$/)
    self[method.to_s.chop] = arguments.first
  elsif has_key?(method)
    self[method]
  else
    super
  end
end

Class Method Details

.initialize_element(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/render/dottable_hash.rb', line 12

def initialize_element(value)
  case value
  when Hash
    new(value)
  when Array
    value.collect { |v| initialize_element(v) }
  else
    value
  end
end

.new(element_to_hash = {}) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/render/dottable_hash.rb', line 4

def new(element_to_hash = {})
  hash = super().merge!(element_to_hash.symbolize_keys)
  hash.each do |key, value|
    hash[key] = initialize_element(value)
  end
  hash
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
33
# File 'lib/render/dottable_hash.rb', line 30

def [](key)
  key = key.to_sym
  super
end

#[]=(key, value) ⇒ Object



24
25
26
27
28
# File 'lib/render/dottable_hash.rb', line 24

def []=(key, value)
  key = key.to_sym
  value = self.class.initialize_element(value)
  super
end

#delete(key) ⇒ Object



35
36
37
38
# File 'lib/render/dottable_hash.rb', line 35

def delete(key)
  key = key.to_sym
  super
end

#fetch(key, *args) ⇒ Object



62
63
64
65
# File 'lib/render/dottable_hash.rb', line 62

def fetch(key, *args)
  key = key.to_sym
  super
end

#fetch_path(full_path) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/render/dottable_hash.rb', line 67

def fetch_path(full_path)
  begin
    fetch_path!(full_path)
  rescue KeyError
    nil
  end
end

#fetch_path!(full_path) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/render/dottable_hash.rb', line 75

def fetch_path!(full_path)
  full_path.split(".").inject(self) do |hash, path|
    raise(KeyError) unless hash.is_a?(Hash)

    hash.fetch(path)
  end
end

#has_key?(key) ⇒ Boolean

Returns:



40
41
42
# File 'lib/render/dottable_hash.rb', line 40

def has_key?(key)
  super(key.to_sym)
end

#merge(other_hash) ⇒ Object



48
49
50
# File 'lib/render/dottable_hash.rb', line 48

def merge(other_hash)
  super(other_hash.symbolize_keys)
end

#merge!(other_hash) ⇒ Object



44
45
46
# File 'lib/render/dottable_hash.rb', line 44

def merge!(other_hash)
  super(other_hash.symbolize_keys)
end

#set_path(full_path, value) ⇒ Object



83
84
85
# File 'lib/render/dottable_hash.rb', line 83

def set_path(full_path, value)
  self.dup.set_path!(full_path, value)
end

#set_path!(full_path, value) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/render/dottable_hash.rb', line 87

def set_path!(full_path, value)
  built_hash_for_value = full_path.split(".").reverse.inject({}) do |cumulative, path_to_source|
    if cumulative.empty?
      { path_to_source.to_sym => value }
    else
      { path_to_source.to_sym => cumulative }
    end
  end

  deep_merge!(built_hash_for_value)
end