class Hashery::OpenCascade

OpenCascade is subclass of OpenHash. It differs in a few significant ways. The reason this class is called "cascade" is that every internal Hash is transformed into an OpenCascade dynamically upon access. This makes it easy to create "cascading" references.

h = { :x => { :y => { :z => 1 } } }
c = OpenCascade[h]
c.x.y.z  #=> 1

As soon as you access a node it automatically becomes an OpenCascade.

c = OpenCascade.new   #=> #<OpenCascade:0x7fac3680ccf0 {}>
c.r                   #=> #<OpenCascade:0x7fac368084c0 {}>
c.a.b                 #=> #<OpenCascade:0x7fac3680a4f0 {}>

But if you set a node, then that will be that value.

c.a.b = 4             #=> 4

To query a node without causing the auto-creation of an OpenCasade instance, use the `?`-mark.

c.a.z?                #=> nil

OpenCascade also transforms Hashes within Arrays.

h = { :x=>[ {:a=>1}, {:a=>2} ], :y=>1 }
c = OpenCascade[h]
c.x.first.a.assert == 1
c.x.last.a.assert  == 2

Finally, you can set call a private method via bang methods using the `!`-mark.

c = OpenCascade.new   #=> #<OpenCascade:0x7fac3680ccf0 {}>
c.each = 4
c.each! do |k,v|
  ...
end

c.x!(4).y!(3)         #=> #<OpenCascade:0x7fac3680ccf0 {:x=>4, :y=>3}>

Public Class Methods

new(*default) click to toggle source

Initialize new OpenCascade instance.

default - The usual default object.

# File lib/hashery/open_cascade.rb, line 60
def initialize(*default)
  @read = {}

  leet = lambda { |h,k| h[k] = OpenCascade.new(&leet) }
  super(*default, &leet)
end

Public Instance Methods

method_missing(sym, *args, &blk) click to toggle source
# File lib/hashery/open_cascade.rb, line 90
def method_missing(sym, *args, &blk)
  type = sym.to_s[-1,1]
  name = sym.to_s.gsub(%r[=!?]$/, '').to_sym

  case type
  when '='
    store(name, args.first)
  when '?'
    key?(name) ? read!(name) : nil    # key?(name)
  when '!'
    __send__(name, *args, &blk)
  else
    #if key?(name)
      read(name)
    #else
    #  #default = OpenCascade.new #self.class.new
    #  #default = default_proc ? default_proc.call(self, name) : default
    #  store(name, read(name))
    #end
  end
end
read(key) click to toggle source

Read value given a key.

key - Index key to lookup.

Returns value.

# File lib/hashery/open_cascade.rb, line 79
def read(key)
  if @read[cast_key(key)]
    super(key)
  else
    @read[cast_key(key)] = store(key, cast_value(super(key)))
  end
end
Also aliased as: read!
read!(key) click to toggle source

Alias for original read method.

Alias for: read