Class | OpenObject
In: | lib/facets/more/openobject.rb
Parent: | Hash

# OpenObject

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

OpenObject is a subclass of Hash and can do just about everything a Hash can do, except that most public methods have been made protected and thus only available internally or via send. A small number, like each are still exposed publically though b/c of their importance.

OpenObject will also clobber any method for which a slot is defined. Even generally very important methods can be clobbered, like instance_eval. So be careful. OpenObject should be used in highly controlled scenarios. If you want to pass one off to an "unknown" rountine, it is best to convert it to a Hash first and convert it back to an OpenObject when finished. To facilitate this the method as_hash! is provided.

      o = OpenObject.new(:a=>1,:b=>2)
      o.as_hash!{ |h| h.update(:a=>6) }
      o #=> #<OpenObject {:a=>6,:b=>2}>

Finally, unlike a regular Hash, all OpenObject‘s keys are symbols and all keys are converted to such using to_sym on the fly.

### Methods

== [] [] []= as_hash! default! define_slot delete each fetch initialize_copy inspect merge method_missing new protect_slot store to_a to_h to_hash to_openobject to_proc update

### Constants

PUBLIC_METHODS | = | /(^__|^instance_|^object_|^\W|^as$|^send$|^class$|\?$)/

### Public Class methods

[](hash=nil)
new( hash=nil, &yld )

Inititalizer for OpenObject is slightly differnt than that of Hash. It does not take a default parameter, but an initial priming Hash as with OpenStruct. The initializer can still take a default block however. To set the degault value use ++default!(value)++.

      OpenObject(:a=>1).default!(0)

### Public Instance methods

==( other )

Check equality. (Should equal be true for Hash too?)

[](k)
[]=(k,v)
as_hash!(&yld)

Preform inplace action on OpenObject as if it were a regular Hash.

default!(default)

Set the default value.

delete(key)
each(&yld)

Iterate over each key-value pair. (Careful, this can be clobbered!)

initialize_copy( orig )
inspect()

Object inspection. (Careful, this can be clobbered!)

merge( other )

Merge one OpenObject with another creating a new OpenObject.

to_a()

Conversion methods. (Careful, these can be clobbered!)

to_h()
to_hash()
to_openobject()
to_proc()
update( other )

Update this OpenObject with another.

### Protected Instance methods

define_slot( key, value=nil )
fetch(k,*d,&b)
method_missing( sym, arg=nil, &blk)
protect_slot( key )
store(k,v)

[Validate]