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.
- ==
- []
- []=
- __fetch__
- __key__?
- __keys__
- __merge__
- __store__
- __table__
- __update__
- each
- initialize_copy
- inspect
- marshal_dump
- marshal_load
- method_missing
- new
- to_a
- to_h
- to_hash
- to_openobject
- to_proc
[ show source ]
# 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
Compare this object and other for equality.
[ show source ]
# File lib/facets/more/openobject.rb, line 106 def ==(other) return false unless other.kind_of?( __class__ ) return @table == other.__table__ end
[ show source ]
# 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
[ show source ]
# File lib/facets/more/openobject.rb, line 140 def []=(name,val) @table[name.to_sym] = val end
def fetch(k) ; @table.key?(k.to_sym) ? @table : null ; end
[ show source ]
# File lib/facets/more/openobject.rb, line 162 def __fetch__(k) ; @table[k.to_sym] ; end
[ show source ]
# File lib/facets/more/openobject.rb, line 165 def __key__?(k) ; @table.key?(k.to_sym) ; end
[ show source ]
# File lib/facets/more/openobject.rb, line 166 def __keys__ ; @table.keys ; end
[ show source ]
# File lib/facets/more/openobject.rb, line 173 def __merge__( other ) o = dup o.__update__( other ) o end
[ show source ]
# File lib/facets/more/openobject.rb, line 163 def __store__(k) ; @table[k.to_sym]=v ; end
Access to internal table.
[ show source ]
# File lib/facets/more/openobject.rb, line 153 def __table__ ; @table ; end
[ show source ]
# 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
For compatibility with other types of hash-like objects. Also make OpenObject usable via Enumerator.
[ show source ]
# File lib/facets/more/openobject.rb, line 146 def each(&yld) @table.each( &yld ) end
duplicate
[ show source ]
# File lib/facets/more/openobject.rb, line 86 def initialize_copy( orig ) super @table = @table.dup end
[ show source ]
# 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
[ show source ]
# File lib/facets/more/openobject.rb, line 97 def marshal_dump @table end
[ show source ]
# File lib/facets/more/openobject.rb, line 101 def marshal_load( tbl ) @table = tbl end
[ show source ]
# 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
[ show source ]
# File lib/facets/more/openobject.rb, line 69 def to_a ; @table.values ; end
[ show source ]
# File lib/facets/more/openobject.rb, line 70 def to_h ; @table.dup ; end
[ show source ]
# File lib/facets/more/openobject.rb, line 71 def to_hash ; @table.dup ; end
[ show source ]
# File lib/facets/more/openobject.rb, line 67 def to_openobject ; self ; end
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
[ show source ]
# File lib/facets/more/openobject.rb, line 81 def to_proc @table.to_proc end