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
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# An OpenStruct is a data structure, similar to a Hash, that allows the
|
|
|
|
# definition of arbitrary attributes with their accompanying values. This is
|
|
|
|
# accomplished by using Ruby's metaporgramming to define methods on the class
|
|
|
|
# itself.
|
|
|
|
#
|
|
|
|
# == Examples:
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# require 'ostruct'
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# person = OpenStruct.new
|
|
|
|
# person.name = "John Smith"
|
|
|
|
# person.age = 70
|
|
|
|
# person.pension = 300
|
|
|
|
#
|
|
|
|
# puts person.name # -> "John Smith"
|
|
|
|
# puts person.age # -> 70
|
|
|
|
# puts person.address # -> nil
|
|
|
|
#
|
|
|
|
# An OpenStruct employs a Hash internally to store the methods and values and
|
|
|
|
# can even be initialized with one:
|
|
|
|
#
|
|
|
|
# country_data = { :country => "Australia", :population => 20_000_000 }
|
|
|
|
# australia = OpenStruct.new(country_data)
|
|
|
|
# p australia # -> <OpenStruct country="Australia" population=20000000>
|
|
|
|
#
|
|
|
|
# You may also define the hash in the initialization call:
|
|
|
|
#
|
|
|
|
# australia = OpenStruct.new(:country => "Australia", :population => 20_000_000)
|
|
|
|
# p australia # -> <OpenStruct country="Australia" population=20000000>
|
|
|
|
#
|
|
|
|
# Hash keys with spaces or characters that would normally not be able to use for
|
|
|
|
# method calls (e.g. ()[]*) will not be immediately available on the
|
|
|
|
# OpenStruct object as a method for retrieval or assignment, but can be still be
|
|
|
|
# reached through the Object#send method.
|
|
|
|
#
|
|
|
|
# measurements = OpenStruct.new("length (in inches)" => 24)
|
|
|
|
# measurements.send("length (in inches)") # -> 24
|
|
|
|
#
|
|
|
|
# data_point = OpenStruct.new(:queued? => true)
|
|
|
|
# data_point.queued? # -> true
|
|
|
|
# data_point.send("queued?=",false)
|
|
|
|
# data_point.queued? # -> false
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# Removing the presence of a method requires the execution the delete_field
|
|
|
|
# method as setting the property value to +nil+ will not remove the method.
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# first_pet = OpenStruct.new(:name => 'Rowdy', :owner => 'John Smith')
|
|
|
|
# first_pet.owner = nil
|
|
|
|
# second_pet = OpenStruct.new(:name => 'Rowdy')
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# first_pet == second_pet # -> false
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# first_pet.delete_field(:owner)
|
|
|
|
# first_pet == second_pet # -> true
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# == Implementation:
|
|
|
|
#
|
|
|
|
# An OpenStruct utilizes Ruby's method lookup structure to and find and define
|
|
|
|
# the necessary methods for properties. This is accomplished through the method
|
|
|
|
# method_missing and define_method.
|
|
|
|
#
|
|
|
|
# This should be a consideration if there is a concern about the performance of
|
|
|
|
# the objects that are created. As there is much more overhead in the setting
|
|
|
|
# of these properties compard to utilizing a Hash or a Struct.
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
class OpenStruct
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# Creates a new OpenStruct object. By default, the resulting OpenStruct
|
|
|
|
# object will have no attributes.
|
|
|
|
#
|
|
|
|
# The optional +hash+, if given, will generate attributes and values.
|
|
|
|
# For example:
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# require 'ostruct'
|
|
|
|
# hash = { "country" => "Australia", :population => 20_000_000 }
|
|
|
|
# data = OpenStruct.new(hash)
|
|
|
|
#
|
|
|
|
# p data # -> <OpenStruct country="Australia" population=20000000>
|
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# You may also define the hash in the initialization call:
|
|
|
|
#
|
|
|
|
# australia = OpenStruct.new(:country => "Australia",
|
|
|
|
# :population => 20_000_000)
|
|
|
|
# p australia # -> <OpenStruct country="Australia" population=20000000>
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
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
|
|
|
|
|
2009-03-06 06:56:38 +03:00
|
|
|
# Duplicate an OpenStruct object members.
|
2004-02-20 13:11:25 +03:00
|
|
|
def initialize_copy(orig)
|
|
|
|
super
|
|
|
|
@table = @table.dup
|
|
|
|
end
|
|
|
|
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
|
|
|
# Provides marshalling support for use by the Marshal library. Returning the
|
|
|
|
# underlying Hash table that contains the functions defined as the keys and
|
|
|
|
# the values assigned to them.
|
|
|
|
#
|
|
|
|
# require 'ostruct'
|
|
|
|
#
|
|
|
|
# person = OpenStruct.new
|
|
|
|
# person.name = 'John Smith'
|
|
|
|
# person.age = 70
|
|
|
|
#
|
|
|
|
# person.marshal_dump # => { :name => 'John Smith', :age => 70 }
|
|
|
|
#
|
2004-12-03 07:56:25 +03:00
|
|
|
def marshal_dump
|
|
|
|
@table
|
|
|
|
end
|
2011-05-23 03:46:08 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Provides marshalling support for use by the Marshal library. Accepting
|
|
|
|
# a Hash of keys and values which will be used to populate the internal table
|
|
|
|
#
|
|
|
|
# require 'ostruct'
|
|
|
|
#
|
|
|
|
# event = OpenStruct.new
|
|
|
|
# hash = { 'time' => Time.now, 'title' => 'Birthday Party' }
|
|
|
|
# event.marshal_load(hash)
|
|
|
|
# event.title # => 'Birthday Party'
|
|
|
|
#
|
2004-12-03 07:56:25 +03:00
|
|
|
def marshal_load(x)
|
|
|
|
@table = x
|
|
|
|
@table.each_key{|key| new_ostruct_member(key)}
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
|
|
|
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
|
|
|
# #modifiable is used internally to check if the OpenStruct is able to be
|
|
|
|
# modified before granting access to the internal Hash table to be augmented.
|
|
|
|
#
|
2009-02-15 15:43:46 +03:00
|
|
|
def modifiable
|
2009-02-27 08:23:10 +03:00
|
|
|
begin
|
|
|
|
@modifiable = true
|
|
|
|
rescue
|
|
|
|
raise TypeError, "can't modify frozen #{self.class}", caller(3)
|
2009-02-15 15:43:46 +03:00
|
|
|
end
|
|
|
|
@table
|
|
|
|
end
|
|
|
|
protected :modifiable
|
|
|
|
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
|
|
|
# new_ostruct_member is used internally to defined properties on the
|
|
|
|
# OpenStruct. It does this by using the metaprogramming function
|
|
|
|
# define_method for both the getter method and the setter method.
|
|
|
|
#
|
2004-12-02 18:17:35 +03:00
|
|
|
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-13 03:09:39 +04:00
|
|
|
class << self; self; end.class_eval do
|
|
|
|
define_method(name) { @table[name] }
|
2009-02-15 15:43:46 +03:00
|
|
|
define_method("#{name}=") { |x| modifiable[name] = x }
|
2005-09-13 03:09:39 +04:00
|
|
|
end
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
2009-02-15 15:43:46 +03:00
|
|
|
name
|
2004-12-02 18:17:35 +03:00
|
|
|
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
|
2011-05-27 19:59:02 +04:00
|
|
|
if mname.chomp!('=') && mid != :[]=
|
1998-01-16 15:19:09 +03:00
|
|
|
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
|
2009-02-15 15:43:46 +03:00
|
|
|
modifiable[new_ostruct_member(mname)] = args[0]
|
2011-05-27 19:59:02 +04:00
|
|
|
elsif len == 0 && mid != :[]
|
2002-11-03 14:04:35 +03:00
|
|
|
@table[mid]
|
1998-01-16 15:19:09 +03:00
|
|
|
else
|
2011-05-27 19:59:02 +04:00
|
|
|
raise NoMethodError, "undefined method `#{mid}' 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
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# Remove the named field from the object. Returning the value that the field
|
|
|
|
# contained if it has defined.
|
|
|
|
#
|
|
|
|
# require 'ostruct'
|
|
|
|
#
|
|
|
|
# person = OpenStruct.new('name' => 'John Smith', 'age' => 70)
|
|
|
|
#
|
|
|
|
# person.delete_field('name') # => 'John Smith'
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def delete_field(name)
|
2010-11-03 08:17:25 +03:00
|
|
|
sym = name.to_sym
|
|
|
|
@table.delete sym
|
|
|
|
singleton_class.__send__(:remove_method, sym, "#{name}=")
|
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}"
|
|
|
|
|
2009-01-13 15:52:23 +03:00
|
|
|
ids = (Thread.current[InspectKey] ||= [])
|
|
|
|
if ids.include?(object_id)
|
|
|
|
return str << ' ...>'
|
|
|
|
end
|
|
|
|
|
|
|
|
ids << object_id
|
|
|
|
begin
|
2005-09-05 12:29:52 +04:00
|
|
|
first = true
|
|
|
|
for k,v in @table
|
|
|
|
str << "," unless first
|
|
|
|
first = false
|
2009-01-13 15:52:23 +03:00
|
|
|
str << " #{k}=#{v.inspect}"
|
2005-09-05 12:29:52 +04:00
|
|
|
end
|
2009-01-13 15:52:23 +03:00
|
|
|
return str << '>'
|
|
|
|
ensure
|
|
|
|
ids.pop
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
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
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# Compares this object and +other+ for equality. An OpenStruct is equal to
|
|
|
|
# +other+ when +other+ is an OpenStruct and the two object's Hash tables are
|
|
|
|
# equal.
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
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
|