Files

OpenStructable

OpensStructable is a mixin module which can provide OpenStruct behavior to any class or object. OpenStructable allows extention of data objects with arbitrary attributes.

Usage

require 'ostructable'

class Record
  include OpenStructable
end

record = Record.new
record.name    = "John Smith"
record.age     = 70
record.pension = 300

puts record.name     # -> "John Smith"
puts record.address  # -> nil

Author(s)

Public Class Methods

include(base) click to toggle source
# File lib/hashery/ostructable.rb, line 52
def self.include(base)
  if Hash > base
    base.module_eval do
      define_method(:__table__) do
        self
      end
    end
    protected :__table__
  end
end
new(hash=nil) click to toggle source
# File lib/hashery/ostructable.rb, line 63
def initialize(hash=nil)
  @__table__ = {}
  if hash
    for k,v in hash
      __table__[k.to_sym] = v
      new_ostruct_member(k)
    end
  end
end

Public Instance Methods

==(other) click to toggle source

Compare this object and other for equality.

# File lib/hashery/ostructable.rb, line 162
def ==(other)
  case other
  when OpenStructable
    __table__ == other.__table__
  #when OpenStruct
  #  __table__ == other.__table__
  when Hash
    __table__ == other
  else
    false
  end
end
delete_field(name) click to toggle source

Remove the named field from the object.

# File lib/hashery/ostructable.rb, line 142
def delete_field(name)
  #@__table__ ||= {}
  __table__.delete(name.to_sym)
end
initialize_copy(orig) click to toggle source

duplicate an OpenStruct object members.

# File lib/hashery/ostructable.rb, line 80
def initialize_copy(orig)
  super
  __table__.replace(__table__.dup)
end
inspect() click to toggle source

Returns a string containing a detailed summary of the keys and values.

# File lib/hashery/ostructable.rb, line 150
def inspect
  str = "<#{self.class}"
  for k,v in (@__table__ ||= {})
    str << " #{k}=#{v.inspect}"
  end
  str << ">"
end
marshal_dump() click to toggle source
# File lib/hashery/ostructable.rb, line 85
def marshal_dump
  __table__
end
marshal_load(hash) click to toggle source
# File lib/hashery/ostructable.rb, line 89
def marshal_load(hash)
  __table__.replace(hash)
  __table__.each_key{|key| new_ostruct_member(key)}
end
new_ostruct_member(name) click to toggle source
# File lib/hashery/ostructable.rb, line 94
def new_ostruct_member(name)
  unless self.respond_to?(name)
    self.instance_eval %{
      def #{name}; __table__[:#{name}]; end
      def #{name}=(x); __table__[:#{name}] = x; end
    }
  end
end
update(hash) click to toggle source

Generate additional attributes and values.

# File lib/hashery/ostructable.rb, line 106
def update(hash)
  #__table__ ||= {}
  if hash
    for k,v in hash
      __table__[k.to_sym] = v
      new_ostruct_member(k)
    end
  end
end

Protected Instance Methods

__table__() click to toggle source
# File lib/hashery/ostructable.rb, line 74
def __table__
  @__table__ ||= {}
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.