OpenHash is very similar to Ruby's own OpenStruct, but it offers some useful advantages in that it is a true Hash object.
Because OpenHash is a subclass of Hash, it can do everything a Hash can unless a Hash method has been explicity exempted for use as an open read/writer via the omit! method.
New OpenHash.
# File lib/hashery/openhash.rb, line 13 def initialize(data={}) super() merge!(data) end
# File lib/hashery/openhash.rb, line 19 def <<(x) case x when Hash update(x) when Array x.each_slice(2) do |(k,v)| self[k] = v end end end
Route get and set calls.
# File lib/hashery/openhash.rb, line 57 def method_missing(s,*a, &b) type = s.to_s[-1,1] name = s.to_s.sub(/[!?=]$/, '') key = name.to_sym case type when '=' self[key] = a[0] #when '!' # self[s] = OpenHash.new when '?' key?(key) else if key?(key) self[key] else super(s,*a,&b) end end end
Omit specific Hash methods from slot protection.
# File lib/hashery/openhash.rb, line 51 def omit!(*methods) methods.reject!{ |x| x.to_s =~ /^__/ } (class << self; self; end).class_eval{ private *methods } end
Generated with the Darkfish Rdoc Generator 2.