Class: Hashit

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_extensions.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash, node_name = nil) ⇒ Hashit

Returns a new instance of Hashit.



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/hash_extensions.rb', line 2

def initialize(hash, node_name=nil)
  if !node_name.nil?
    self.instance_variable_set("@_node_name", node_name)
    self.class.send(:define_method, "_node_name", proc{self.instance_variable_get("@_node_name")}) if !self.respond_to?("_node_name")
    self.class.send(:define_method, "_node_name=", proc{|node_name| self.instance_variable_set("@_node_name", node_name)}) if !self.respond_to?("_node_name=")
  end
  hash.each do |k,v|
    self.instance_variable_set("@#{k}", v.is_a?(Hash) ? Hashit.new(v, k) : v)
    self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) if !self.respond_to?("#{k}")
    self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) if !self.respond_to?("#{k}=")
  end
end