Class OpenObject
In: lib/hashery/openobject.rb
Parent: BasicObject

OpenObject

OpenObject is very similar to Ruby‘s own OpenStruct, but it offers some advantages. With OpenStruct, slots with the same name as predefined Object methods cannot be used. With OpenObject, almost any slot can be defined. OpenObject is a subclass of BasicObject to ensure all method slots, except those that are absolutely essential, are open for use.

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

Methods

<<   ==   []   []   []=   as_hash   default!   each   eql?   fetch   initialize_copy   inspect   is_a?   key?   merge!   method_missing   new   store   to_a   to_h   to_hash   to_openobject   to_proc   update!  

Public Class methods

PUBLIC_METHODS = /(^__|^instance_|^object_|^\W|^as$|^send$|^class$|\?$)/ protected(*public_instance_methods.select{ |m| m !~ PUBLIC_METHODS })

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

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

Public Instance methods

Check equality.

NOT SURE ABOUT THIS

Set the default value.

Iterate over each key-value pair.

Object inspection. TODO: Need to get class and id in hex form.

Is a given key defined?

Convert to an associative array.

Convert to an assignment procedure.

Protected Instance methods

def protect_slot( key )

  (class << self; self; end).class_eval {
    protected key rescue nil
 }

end

[Validate]