class OpenStruct
def initialize(hash=nil)
@table = {}
if hash
for k,v in hash
@table[k.to_sym] = v
new_ostruct_member(k)
end
end
end
def initialize_copy(orig)
super
@table = @table.dup
end
def marshal_dump
@table
end
def marshal_load(x)
@table = x
@table.each_key{|key| new_ostruct_member(key)}
end
def modifiable
if self.frozen?
raise TypeError, "can't modify frozen #{self.class}", caller(2)
end
@table
end
protected :modifiable
def new_ostruct_member(name)
name = name.to_sym
unless self.respond_to?(name)
class << self; self; end.class_eval do
define_method(name) { @table[name] }
define_method("#{name}=") { |x| modifiable[name] = x }
end
end
name
end
def method_missing(mid, *args) mname = mid.id2name
len = args.length
if mname.chomp!('=')
if len != 1
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
end
modifiable[new_ostruct_member(mname)] = args[0]
elsif len == 0
@table[mid]
else
raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
end
end
def delete_field(name)
@table.delete name.to_sym
end
InspectKey = :__inspect_key__
def inspect
str = "#<#{self.class}"
ids = (Thread.current[InspectKey] ||= [])
if ids.include?(object_id)
return str << ' ...>'
end
ids << object_id
begin
first = true
for k,v in @table
str << "," unless first
first = false
str << " #{k}=#{v.inspect}"
end
return str << '>'
ensure
ids.pop
end
end
alias :to_s :inspect
attr_reader :table protected :table
def ==(other)
return false unless(other.kind_of?(OpenStruct))
return @table == other.table
end
end