Class: HashObject

Inherits:
Object show all
Defined in:
lib/wedge/utilis/hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashObject

Returns a new instance of HashObject.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wedge/utilis/hash.rb', line 83

def initialize(hash = {})
  hash.each do |k,v|
    if v.kind_of? Hash
      self.instance_variable_set("@#{k}", HashObject.new(v))
    end

    self.instance_variable_set("@#{k}", v)
    self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
    self.class.send(:define_method, "#{k}=", proc{|vv| self.instance_variable_set("@#{k}", vv)})
  end
end

Instance Method Details

#dupObject



104
105
106
# File 'lib/wedge/utilis/hash.rb', line 104

def dup
  Wedge::IndifferentHash.new self.to_h.inject({}) {|copy, (key, value)| copy[key] = value.dup rescue value; copy}
end

#to_hObject



95
96
97
98
99
100
101
102
# File 'lib/wedge/utilis/hash.rb', line 95

def to_h
  hash_to_return = Wedge::IndifferentHash.new
  self.instance_variables.each do |var|
    value = self.instance_variable_get(var)
    hash_to_return[var.to_s.gsub("@","")] = value.is_a?(HashObject) ? value.to_h : value
  end
  return hash_to_return
end