2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# = ostruct.rb: OpenStruct implementation
|
|
|
|
#
|
|
|
|
# Author:: Yukihiro Matsumoto
|
|
|
|
# Documentation:: Gavin Sinclair
|
|
|
|
#
|
|
|
|
# OpenStruct allows the creation of data objects with arbitrary attributes.
|
|
|
|
# See OpenStruct for an example.
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# OpenStruct allows you to create data objects and set arbitrary attributes.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# require 'ostruct'
|
|
|
|
#
|
|
|
|
# record = OpenStruct.new
|
|
|
|
# record.name = "John Smith"
|
|
|
|
# record.age = 70
|
|
|
|
# record.pension = 300
|
|
|
|
#
|
|
|
|
# puts record.name # -> "John Smith"
|
|
|
|
# puts record.address # -> nil
|
|
|
|
#
|
|
|
|
# It is like a hash with a different way to access the data. In fact, it is
|
|
|
|
# implemented with a hash, and you can initialize it with one.
|
|
|
|
#
|
|
|
|
# hash = { "country" => "Australia", :population => 20_000_000 }
|
|
|
|
# data = OpenStruct.new(hash)
|
|
|
|
#
|
|
|
|
# p data # -> <OpenStruct country="Australia" population=20000000>
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
class OpenStruct
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# Create a new OpenStruct object. The optional +hash+, if given, will
|
|
|
|
# generate attributes and values. For example.
|
|
|
|
#
|
|
|
|
# require 'ostruct'
|
|
|
|
# hash = { "country" => "Australia", :population => 20_000_000 }
|
|
|
|
# data = OpenStruct.new(hash)
|
|
|
|
#
|
|
|
|
# p data # -> <OpenStruct country="Australia" population=20000000>
|
|
|
|
#
|
|
|
|
# By default, the resulting OpenStruct object will have no attributes.
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def initialize(hash=nil)
|
|
|
|
@table = {}
|
|
|
|
if hash
|
|
|
|
for k,v in hash
|
2005-09-05 12:29:52 +04:00
|
|
|
@table[k.to_sym] = v
|
2004-12-02 18:17:35 +03:00
|
|
|
new_ostruct_member(k)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-20 13:11:25 +03:00
|
|
|
# Duplicate an OpenStruct object members.
|
|
|
|
def initialize_copy(orig)
|
|
|
|
super
|
|
|
|
@table = @table.dup
|
|
|
|
end
|
|
|
|
|
2004-12-03 07:56:25 +03:00
|
|
|
def marshal_dump
|
|
|
|
@table
|
|
|
|
end
|
|
|
|
def marshal_load(x)
|
|
|
|
@table = x
|
|
|
|
@table.each_key{|key| new_ostruct_member(key)}
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_ostruct_member(name)
|
2005-09-05 12:29:52 +04:00
|
|
|
name = name.to_sym
|
2004-12-02 18:17:35 +03:00
|
|
|
unless self.respond_to?(name)
|
2005-09-05 12:29:52 +04:00
|
|
|
meta = class << self; self; end
|
|
|
|
meta.send(:define_method, name) { @table[name] }
|
|
|
|
meta.send(:define_method, :"#{name}=") { |x| @table[name] = x }
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
def method_missing(mid, *args) # :nodoc:
|
1998-01-16 15:19:09 +03:00
|
|
|
mname = mid.id2name
|
|
|
|
len = args.length
|
|
|
|
if mname =~ /=$/
|
|
|
|
if len != 1
|
2005-09-05 12:29:52 +04:00
|
|
|
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2003-08-26 18:54:51 +04:00
|
|
|
if self.frozen?
|
2005-09-05 12:29:52 +04:00
|
|
|
raise TypeError, "can't modify frozen #{self.class}", caller(1)
|
2003-08-26 18:54:51 +04:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
mname.chop!
|
2004-12-02 18:17:35 +03:00
|
|
|
self.new_ostruct_member(mname)
|
2005-09-05 12:29:52 +04:00
|
|
|
@table[mname.intern] = args[0]
|
2002-11-18 23:09:46 +03:00
|
|
|
elsif len == 0
|
2002-11-03 14:04:35 +03:00
|
|
|
@table[mid]
|
1998-01-16 15:19:09 +03:00
|
|
|
else
|
2002-11-15 19:12:43 +03:00
|
|
|
raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
2002-11-03 14:04:35 +03:00
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# Remove the named field from the object.
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def delete_field(name)
|
2002-11-03 14:04:35 +03:00
|
|
|
@table.delete name.to_sym
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
2005-09-05 12:29:52 +04:00
|
|
|
InspectKey = :__inspect_key__ # :nodoc:
|
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# Returns a string containing a detailed summary of the keys and values.
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def inspect
|
2005-09-05 12:29:52 +04:00
|
|
|
str = "#<#{self.class}"
|
|
|
|
|
|
|
|
Thread.current[InspectKey] ||= []
|
|
|
|
if Thread.current[InspectKey].include?(self) then
|
|
|
|
str << " ..."
|
|
|
|
else
|
|
|
|
first = true
|
|
|
|
for k,v in @table
|
|
|
|
str << "," unless first
|
|
|
|
first = false
|
|
|
|
|
|
|
|
Thread.current[InspectKey] << v
|
|
|
|
begin
|
|
|
|
str << " #{k}=#{v.inspect}"
|
|
|
|
ensure
|
|
|
|
Thread.current[InspectKey].pop
|
|
|
|
end
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2005-09-05 12:29:52 +04:00
|
|
|
|
2002-11-18 23:09:46 +03:00
|
|
|
str << ">"
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2005-09-05 12:29:52 +04:00
|
|
|
alias :to_s :inspect
|
2003-09-25 04:03:11 +04:00
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
attr_reader :table # :nodoc:
|
2003-09-25 04:03:11 +04:00
|
|
|
protected :table
|
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# Compare this object and +other+ for equality.
|
|
|
|
#
|
2003-09-25 04:03:11 +04:00
|
|
|
def ==(other)
|
|
|
|
return false unless(other.kind_of?(OpenStruct))
|
|
|
|
return @table == other.table
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|