OpenObject

OpenObject is similar to OpenStruct, but differs in a few significant ways.

OpenObject is a sublcass of BasicObject, so there are many fewer reserved Kernel methods in the object’s namespace.

Also it does not create any instance methods for the added entries, but rather strictly uses a hash. OpenStruct creates methods, but is actaully inconsitant in their use —try dup on an OpenStruct and all the methods disappear. Morevoer, I beleive this feature was added to OpenStruct for speed, but I have not been able to find this advantage in benchmark tests.

Methods
Public Class methods
new( init=nil )
# File lib/facets/more/openobject.rb, line 52
  def initialize( init=nil )
    case init
    when Array
      @table = Hash[*init].keys_to_sym
    when Hash
      @table = init.keys_to_sym
    when NilClass
      @table = {}
    when OpenObject
      @table = init.__table__.dup
    else
      @table = init.to_h.keys_to_sym
    end
  end
Public Instance methods
==(other)

Compare this object and other for equality.

# File lib/facets/more/openobject.rb, line 106
  def ==(other)
    return false unless other.kind_of?( __class__ )
    return @table == other.__table__
  end
[](name)
# File lib/facets/more/openobject.rb, line 131
  def [](name)
    name = name.to_sym
    #if @table.key?(name)
      @table[name]
    #else
    #  Kernel.null
    #end
  end
[]=(name,val)
# File lib/facets/more/openobject.rb, line 140
  def []=(name,val)
    @table[name.to_sym] = val
  end
__fetch__(k)

def fetch(k) ; @table.key?(k.to_sym) ? @table : null ; end

# File lib/facets/more/openobject.rb, line 162
  def __fetch__(k)  ; @table[k.to_sym] ; end
__key__?(k)
# File lib/facets/more/openobject.rb, line 165
  def __key__?(k) ; @table.key?(k.to_sym) ; end
__keys__()
# File lib/facets/more/openobject.rb, line 166
  def __keys__    ; @table.keys ; end
__merge__( other )
# File lib/facets/more/openobject.rb, line 173
  def __merge__( other )
    o = dup
    o.__update__( other )
    o
  end
__store__(k)
# File lib/facets/more/openobject.rb, line 163
  def __store__(k)  ; @table[k.to_sym]=v ; end
__table__()

Access to internal table.

# File lib/facets/more/openobject.rb, line 153
  def __table__ ; @table ; end
__update__( other )
# File lib/facets/more/openobject.rb, line 168
  def __update__( other )
    other = Hash[*other] if other.is_a?(Array)
    other.each{ |k, v| @table[k.to_sym] = v }
  end
each(&yld)

For compatibility with other types of hash-like objects. Also make OpenObject usable via Enumerator.

# File lib/facets/more/openobject.rb, line 146
  def each(&yld)
    @table.each( &yld )
  end
initialize_copy( orig )

duplicate

# File lib/facets/more/openobject.rb, line 86
  def initialize_copy( orig )
    super
    @table = @table.dup
  end
inspect()
# File lib/facets/more/openobject.rb, line 91
  def inspect
    s = "<#{__class__}"
    s << " " << @table.collect{|k,v| "#{k}=#{v.inspect}"}.join(' ') unless @table.empty?
    s << ">"
  end
marshal_dump()
# File lib/facets/more/openobject.rb, line 97
  def marshal_dump
    @table
  end
marshal_load( tbl )
# File lib/facets/more/openobject.rb, line 101
  def marshal_load( tbl )
    @table = tbl
  end
method_missing( sym, *args )
# File lib/facets/more/openobject.rb, line 111
  def method_missing( sym, *args )
    type = sym.to_s[-1,1]
    name = sym.to_s.gsub(/[=!?]$/, '').to_sym

    if type == '='
      @table[name] = args[0]
    elsif type == '!' and args.size > 0
      @table[name] = args[0]
      self
    else
      if @table.key?(name)
        @table[name]
      #elsif @parent
      #  @parent.__send__(name)
      else
        nil
      end
    end
  end
to_a()
# File lib/facets/more/openobject.rb, line 69
  def to_a    ; @table.values ; end
to_h()
# File lib/facets/more/openobject.rb, line 70
  def to_h    ; @table.dup    ; end
to_hash()
# File lib/facets/more/openobject.rb, line 71
  def to_hash ; @table.dup    ; end
to_openobject()
# File lib/facets/more/openobject.rb, line 67
  def to_openobject ; self    ; end
to_proc()

OpenObejct can be converted to an assignment Proc.

  o = OpenObject.new( :a=>1 )
  p = o.to_proc
  x = OpenObject.new
  p[x]
  x.a #=> 1
# File lib/facets/more/openobject.rb, line 81
  def to_proc
    @table.to_proc
  end