Module: OpenStructable

Defined in:
lib/mega/ostructable.rb

Overview

:title: 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 'mega/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)

  • Thomas Sawyer

  • Yukihiro Matsumoto

  • Gavin Sinclair (Documentation)

Constant Summary collapse

VERSION =
'0.9.0'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object

:nodoc:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mega/ostructable.rb', line 91

def method_missing(mid, *args) # :nodoc:
  mname = mid.id2name
  len = args.length
  if mname =~ /=$/
    if len != 1
      raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
    end
    if self.frozen?
      raise TypeError, "can't modify frozen #{self.class}", caller(1)
    end
    mname.chop!
    @__table__ ||= {}
    @__table__[mname.intern] = args[0]
    self.new_ostruct_member(mname)
  elsif len == 0
    @__table__ ||= {}
    @__table__[mid]
  else
    raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
  end
end

Instance Method Details

#==(other) ⇒ Object

Compare this object and other for equality.



140
141
142
143
# File 'lib/mega/ostructable.rb', line 140

def ==(other)
  return false unless(other.kind_of?(OpenStruct))
  return @__table__ == other.table
end

#delete_field(name) ⇒ Object

Remove the named field from the object.



116
117
118
119
# File 'lib/mega/ostructable.rb', line 116

def delete_field(name)
  @__table__ ||= {}
  @__table__.delete name.to_sym
end

#initialize_copy(orig) ⇒ Object

Duplicate an OpenStruct object members.



66
67
68
69
# File 'lib/mega/ostructable.rb', line 66

def initialize_copy(orig)
  super
  @__table__ = @__table__.dup
end

#inspectObject

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



124
125
126
127
128
129
130
# File 'lib/mega/ostructable.rb', line 124

def inspect
  str = "<#{self.class}"
  for k,v in (@__table__ ||= {})
    str << " #{k}=#{v.inspect}"
  end
  str << ">"
end

#new_ostruct_member(name) ⇒ Object



71
72
73
74
75
76
# File 'lib/mega/ostructable.rb', line 71

def new_ostruct_member(name)
  self.instance_eval %{
    def #{name}; @__table__[:#{name}]; end
    def #{name}=(x); @__table__[:#{name}] = x; end
  }
end

#update(hash) ⇒ Object

Generate additional attributes and values.



81
82
83
84
85
86
87
88
89
# File 'lib/mega/ostructable.rb', line 81

def update(hash)
  @__table__ ||= {}
  if hash
    for k,v in hash
      @__table__[k.to_sym] = v
      new_ostruct_member(k)
    end
  end
end