Class: Hash

Inherits:
Object show all
Defined in:
lib/tworgy/hash_and_array_extensions.rb

Instance Method Summary collapse

Instance Method Details

#ostructifyObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tworgy/hash_and_array_extensions.rb', line 39

def ostructify
  temp = inject({}) do |hash, (key, value)|
    hash[key] = (value.is_a?(Hash) || value.is_a?(Array)) ? value.ostructify : value
    hash
  end
  result = OpenStruct.new temp
  
  if result.instance_variable_get('@table')[:id] != nil
    def result.id
      instance_variable_get('@table')[:id]
    end
  end
  result
end

#recursively(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/tworgy/hash_and_array_extensions.rb', line 24

def recursively(&block)
  inject({}) do |hash, (key, value)|
    if value.is_a?(Hash)
      hash[key] = value.recursively(&block)
    else
      hash[key] = value
    end
    yield hash
  end
end

#recursively!(&block) ⇒ Object



35
36
37
# File 'lib/tworgy/hash_and_array_extensions.rb', line 35

def recursively!(&block)
  replace(recursively(&block))
end