Parent

Files

OpenHash

OpenHash

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.

Public Class Methods

new(data={}) click to toggle source

New OpenHash.

# File lib/hashery/openhash.rb, line 13
def initialize(data={})
  super()
  merge!(data)
end

Public Instance Methods

<<(x) click to toggle source
# 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
inspect() click to toggle source
# File lib/hashery/openhash.rb, line 46
def inspect
  super
end
method_missing(s,*a, &b) click to toggle source

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!(*methods) click to toggle source

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
respond_to?(name) click to toggle source
# File lib/hashery/openhash.rb, line 31
def respond_to?(name)
  key?(name.to_sym) || super(name)
end
to_h() click to toggle source
# File lib/hashery/openhash.rb, line 36
def to_h
  dup
end
to_hash() click to toggle source
# File lib/hashery/openhash.rb, line 41
def to_hash
  dup
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.