2017-10-24 14:08:15 -04:00
|
|
|
# frozen_string_literal: true
|
2004-02-19 08:42:30 -05: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 07:19:09 -05:00
|
|
|
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2011-05-22 19: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
|
2011-08-26 18:22:37 -04:00
|
|
|
# accomplished by using Ruby's metaprogramming to define methods on the class
|
2011-05-22 19:46:08 -04:00
|
|
|
# itself.
|
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# == Examples
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# require "ostruct"
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2011-05-22 19:46:08 -04:00
|
|
|
# person = OpenStruct.new
|
2017-03-11 16:32:46 -05:00
|
|
|
# person.name = "John Smith"
|
|
|
|
# person.age = 70
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# person.name # => "John Smith"
|
|
|
|
# person.age # => 70
|
|
|
|
# person.address # => nil
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# An OpenStruct employs a Hash internally to store the attributes and values
|
|
|
|
# and can even be initialized with one:
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# australia = OpenStruct.new(:country => "Australia", :capital => "Canberra")
|
|
|
|
# # => #<OpenStruct country="Australia", capital="Canberra">
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# Hash keys with spaces or characters that could normally not be used for
|
|
|
|
# method calls (e.g. <code>()[]*</code>) will not be immediately available
|
|
|
|
# on the OpenStruct object as a method for retrieval or assignment, but can
|
2020-11-04 16:09:51 -05:00
|
|
|
# still be reached through the Object#send method or using [].
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
|
|
|
# measurements = OpenStruct.new("length (in inches)" => 24)
|
2020-09-14 14:03:56 -04:00
|
|
|
# measurements[:"length (in inches)"] # => 24
|
2020-11-04 16:09:51 -05:00
|
|
|
# measurements.send("length (in inches)") # => 24
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# message = OpenStruct.new(:queued? => true)
|
|
|
|
# message.queued? # => true
|
2020-11-04 16:09:51 -05:00
|
|
|
# message.send("queued?=", false)
|
2017-03-11 16:32:46 -05:00
|
|
|
# message.queued? # => false
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# Removing the presence of an attribute requires the execution of the
|
|
|
|
# delete_field method as setting the property value to +nil+ will not
|
|
|
|
# remove the attribute.
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# first_pet = OpenStruct.new(:name => "Rowdy", :owner => "John Smith")
|
|
|
|
# second_pet = OpenStruct.new(:name => "Rowdy")
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# first_pet.owner = nil
|
|
|
|
# first_pet # => #<OpenStruct name="Rowdy", owner=nil>
|
|
|
|
# first_pet == second_pet # => false
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2011-05-22 19:46:08 -04:00
|
|
|
# first_pet.delete_field(:owner)
|
2017-03-11 16:32:46 -05:00
|
|
|
# first_pet # => #<OpenStruct name="Rowdy">
|
|
|
|
# first_pet == second_pet # => true
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2020-09-25 21:17:06 -04:00
|
|
|
# Ractor compatibility: A frozen OpenStruct with shareable values is itself shareable.
|
|
|
|
#
|
2020-09-14 14:03:56 -04:00
|
|
|
# == Caveats
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2013-02-26 22:36:02 -05:00
|
|
|
# An OpenStruct utilizes Ruby's method lookup structure to find and define the
|
2017-03-11 16:32:46 -05:00
|
|
|
# necessary methods for properties. This is accomplished through the methods
|
|
|
|
# method_missing and define_singleton_method.
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
|
|
|
# This should be a consideration if there is a concern about the performance of
|
2011-08-26 18:22:37 -04:00
|
|
|
# the objects that are created, as there is much more overhead in the setting
|
|
|
|
# of these properties compared to using a Hash or a Struct.
|
2020-09-14 14:03:56 -04:00
|
|
|
# Creating an open struct from a small Hash and accessing a few of the
|
|
|
|
# entries can be 200 times slower than accessing the hash directly.
|
|
|
|
#
|
2020-09-26 01:41:46 -04:00
|
|
|
# This is a potential security issue; building OpenStruct from untrusted user data
|
|
|
|
# (e.g. JSON web request) may be susceptible to a "symbol denial of service" attack
|
|
|
|
# since the keys create methods and names of methods are never garbage collected.
|
|
|
|
#
|
2020-09-14 14:03:56 -04:00
|
|
|
# This may also be the source of incompatibilities between Ruby versions:
|
|
|
|
#
|
|
|
|
# o = OpenStruct.new
|
|
|
|
# o.then # => nil in Ruby < 2.6, enumerator for Ruby >= 2.6
|
|
|
|
#
|
|
|
|
# Builtin methods may be overwritten this way, which may be a source of bugs
|
|
|
|
# or security issues:
|
|
|
|
#
|
|
|
|
# o = OpenStruct.new
|
|
|
|
# o.methods # => [:to_h, :marshal_load, :marshal_dump, :each_pair, ...
|
|
|
|
# o.methods = [:foo, :bar]
|
|
|
|
# o.methods # => [:foo, :bar]
|
|
|
|
#
|
|
|
|
# To help remedy clashes, OpenStruct uses only protected/private methods ending with `!`
|
|
|
|
# and defines aliases for builtin public methods by adding a `!`:
|
|
|
|
#
|
|
|
|
# o = OpenStruct.new(make: 'Bentley', class: :luxury)
|
|
|
|
# o.class # => :luxury
|
|
|
|
# o.class! # => OpenStruct
|
|
|
|
#
|
2020-09-25 20:58:48 -04:00
|
|
|
# It is recommended (but not enforced) to not use fields ending in `!`;
|
|
|
|
# Note that a subclass' methods may not be overwritten, nor can OpenStruct's own methods
|
|
|
|
# ending with `!`.
|
2020-09-14 14:03:56 -04:00
|
|
|
#
|
|
|
|
# For all these reasons, consider not using OpenStruct at all.
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
1998-01-16 07:19:09 -05:00
|
|
|
class OpenStruct
|
2021-10-21 00:54:31 -04:00
|
|
|
VERSION = "0.5.0"
|
2015-12-28 22:48:36 -05:00
|
|
|
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2011-05-22 19:46:08 -04:00
|
|
|
# Creates a new OpenStruct object. By default, the resulting OpenStruct
|
|
|
|
# object will have no attributes.
|
|
|
|
#
|
2012-10-28 17:19:50 -04:00
|
|
|
# The optional +hash+, if given, will generate attributes and values
|
|
|
|
# (can be a Hash, an OpenStruct or a Struct).
|
2011-05-22 19:46:08 -04:00
|
|
|
# For example:
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# require "ostruct"
|
|
|
|
# hash = { "country" => "Australia", :capital => "Canberra" }
|
2004-02-19 08:42:30 -05:00
|
|
|
# data = OpenStruct.new(hash)
|
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# data # => #<OpenStruct country="Australia", capital="Canberra">
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
1998-01-16 07:19:09 -05:00
|
|
|
def initialize(hash=nil)
|
|
|
|
if hash
|
2020-09-25 20:55:38 -04:00
|
|
|
update_to_values!(hash)
|
|
|
|
else
|
|
|
|
@table = {}
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-11 16:32:46 -05:00
|
|
|
# Duplicates an OpenStruct object's Hash table.
|
2020-09-14 15:15:28 -04:00
|
|
|
private def initialize_clone(orig) # :nodoc:
|
|
|
|
super # clones the singleton class for us
|
|
|
|
@table = @table.dup unless @table.frozen?
|
|
|
|
end
|
|
|
|
|
|
|
|
private def initialize_dup(orig) # :nodoc:
|
2004-02-20 05:11:25 -05:00
|
|
|
super
|
2020-09-25 20:55:38 -04:00
|
|
|
update_to_values!(@table)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def update_to_values!(hash) # :nodoc:
|
|
|
|
@table = {}
|
|
|
|
hash.each_pair do |k, v|
|
|
|
|
set_ostruct_member_value!(k, v)
|
|
|
|
end
|
2004-02-20 05:11:25 -05:00
|
|
|
end
|
|
|
|
|
2018-12-22 12:05:03 -05:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# ostruct.to_h -> hash
|
|
|
|
# ostruct.to_h {|name, value| block } -> hash
|
2012-04-23 23:46:55 -04:00
|
|
|
#
|
|
|
|
# Converts the OpenStruct to a hash with keys representing
|
2017-03-11 16:32:46 -05:00
|
|
|
# each attribute (as symbols) and their corresponding values.
|
2012-04-23 23:46:55 -04:00
|
|
|
#
|
2018-12-22 12:05:03 -05:00
|
|
|
# If a block is given, the results of the block on each pair of
|
|
|
|
# the receiver will be used as pairs.
|
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# require "ostruct"
|
|
|
|
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
|
|
|
|
# data.to_h # => {:country => "Australia", :capital => "Canberra" }
|
2018-12-22 12:05:03 -05:00
|
|
|
# data.to_h {|name, value| [name.to_s, value.upcase] }
|
|
|
|
# # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
|
2012-04-23 23:46:55 -04:00
|
|
|
#
|
2020-09-02 16:05:59 -04:00
|
|
|
if {test: :to_h}.to_h{ [:works, true] }[:works] # RUBY_VERSION < 2.6 compatibility
|
|
|
|
def to_h(&block)
|
|
|
|
if block
|
|
|
|
@table.to_h(&block)
|
|
|
|
else
|
|
|
|
@table.dup
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def to_h(&block)
|
|
|
|
if block
|
|
|
|
@table.map(&block).to_h
|
|
|
|
else
|
|
|
|
@table.dup
|
|
|
|
end
|
2018-12-22 12:05:03 -05:00
|
|
|
end
|
2012-04-23 23:46:55 -04:00
|
|
|
end
|
|
|
|
|
2012-10-28 17:18:53 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# :call-seq:
|
|
|
|
# ostruct.each_pair {|name, value| block } -> ostruct
|
|
|
|
# ostruct.each_pair -> Enumerator
|
2012-10-28 17:18:53 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# Yields all attributes (as symbols) along with the corresponding values
|
|
|
|
# or returns an enumerator if no block is given.
|
|
|
|
#
|
|
|
|
# require "ostruct"
|
|
|
|
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
|
|
|
|
# data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
|
2012-10-28 17:18:53 -04:00
|
|
|
#
|
|
|
|
def each_pair
|
2021-06-14 10:07:51 -04:00
|
|
|
return to_enum(__method__) { @table.size } unless block_given!
|
2012-10-28 17:18:53 -04:00
|
|
|
@table.each_pair{|p| yield p}
|
2017-02-03 13:25:28 -05:00
|
|
|
self
|
2012-10-28 17:18:53 -04:00
|
|
|
end
|
|
|
|
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2012-10-28 17:18:11 -04:00
|
|
|
# Provides marshalling support for use by the Marshal library.
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2020-09-26 01:41:46 -04:00
|
|
|
def marshal_dump # :nodoc:
|
2004-12-02 23:56:25 -05:00
|
|
|
@table
|
|
|
|
end
|
2011-05-22 19:46:08 -04:00
|
|
|
|
|
|
|
#
|
2012-10-28 17:18:11 -04:00
|
|
|
# Provides marshalling support for use by the Marshal library.
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2020-10-06 17:10:46 -04:00
|
|
|
alias_method :marshal_load, :update_to_values! # :nodoc:
|
2004-12-02 10:17:35 -05:00
|
|
|
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2012-10-28 17:18:32 -04:00
|
|
|
# Used internally to defined properties on the
|
2011-05-22 19:46:08 -04:00
|
|
|
# OpenStruct. It does this by using the metaprogramming function
|
2012-10-28 17:19:32 -04:00
|
|
|
# define_singleton_method for both the getter method and the setter method.
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2016-03-03 00:09:02 -05:00
|
|
|
def new_ostruct_member!(name) # :nodoc:
|
2020-09-25 20:58:48 -04:00
|
|
|
unless @table.key?(name) || is_method_protected!(name)
|
2021-08-20 08:12:28 -04:00
|
|
|
getter_proc = Proc.new { @table[name] }
|
|
|
|
setter_proc = Proc.new {|x| @table[name] = x}
|
|
|
|
if defined?(::Ractor)
|
|
|
|
::Ractor.make_shareable(getter_proc)
|
|
|
|
::Ractor.make_shareable(setter_proc)
|
|
|
|
end
|
2021-09-28 06:58:13 -04:00
|
|
|
define_singleton_method!(name, &getter_proc)
|
2021-08-20 08:12:28 -04:00
|
|
|
define_singleton_method!("#{name}=", &setter_proc)
|
2004-12-02 10:17:35 -05:00
|
|
|
end
|
|
|
|
end
|
2016-03-03 00:09:02 -05:00
|
|
|
private :new_ostruct_member!
|
|
|
|
|
2020-09-25 20:58:48 -04:00
|
|
|
private def is_method_protected!(name) # :nodoc:
|
|
|
|
if !respond_to?(name, true)
|
|
|
|
false
|
2020-09-30 18:21:27 -04:00
|
|
|
elsif name.match?(/!$/)
|
2020-09-25 20:58:48 -04:00
|
|
|
true
|
|
|
|
else
|
2021-01-12 17:20:50 -05:00
|
|
|
owner = method!(name).owner
|
|
|
|
if owner.class == ::Class
|
|
|
|
owner < ::OpenStruct
|
|
|
|
else
|
|
|
|
self.class.ancestors.any? do |mod|
|
|
|
|
return false if mod == ::OpenStruct
|
|
|
|
mod == owner
|
|
|
|
end
|
|
|
|
end
|
2020-09-25 20:58:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-14 14:03:56 -04:00
|
|
|
def freeze
|
|
|
|
@table.freeze
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2020-09-14 14:06:49 -04:00
|
|
|
private def method_missing(mid, *args) # :nodoc:
|
1998-01-16 07:19:09 -05:00
|
|
|
len = args.length
|
2015-04-13 23:35:18 -04:00
|
|
|
if mname = mid[/.*(?==\z)/m]
|
1998-01-16 07:19:09 -05:00
|
|
|
if len != 1
|
2020-09-25 20:58:48 -04:00
|
|
|
raise! ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2020-09-14 13:48:29 -04:00
|
|
|
set_ostruct_member_value!(mname, args[0])
|
2020-09-08 15:08:50 -04:00
|
|
|
elsif len == 0
|
2021-01-05 18:34:45 -05:00
|
|
|
@table[mid]
|
1998-01-16 07:19:09 -05:00
|
|
|
else
|
2017-03-28 05:21:58 -04:00
|
|
|
begin
|
|
|
|
super
|
|
|
|
rescue NoMethodError => err
|
|
|
|
err.backtrace.shift
|
2020-09-25 20:58:48 -04:00
|
|
|
raise!
|
2017-03-28 05:21:58 -04:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
end
|
2002-11-03 06:04:35 -05:00
|
|
|
|
2012-10-28 17:20:10 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# :call-seq:
|
|
|
|
# ostruct[name] -> object
|
|
|
|
#
|
2020-09-26 01:41:46 -04:00
|
|
|
# Returns the value of an attribute, or `nil` if there is no such attribute.
|
2017-03-11 16:32:46 -05:00
|
|
|
#
|
|
|
|
# require "ostruct"
|
|
|
|
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
|
|
|
|
# person[:age] # => 70, same as person.age
|
2012-10-28 17:20:10 -04:00
|
|
|
#
|
|
|
|
def [](name)
|
|
|
|
@table[name.to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# :call-seq:
|
|
|
|
# ostruct[name] = obj -> obj
|
|
|
|
#
|
|
|
|
# Sets the value of an attribute.
|
2012-10-28 17:20:10 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# require "ostruct"
|
|
|
|
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
|
|
|
|
# person[:age] = 42 # equivalent to person.age = 42
|
|
|
|
# person.age # => 42
|
2012-10-28 17:20:10 -04:00
|
|
|
#
|
|
|
|
def []=(name, value)
|
2020-09-14 14:03:56 -04:00
|
|
|
name = name.to_sym
|
|
|
|
new_ostruct_member!(name)
|
|
|
|
@table[name] = value
|
2012-10-28 17:20:10 -04:00
|
|
|
end
|
2020-09-14 13:48:29 -04:00
|
|
|
alias_method :set_ostruct_member_value!, :[]=
|
|
|
|
private :set_ostruct_member_value!
|
2012-10-28 17:20:10 -04:00
|
|
|
|
2017-03-11 16:32:46 -05:00
|
|
|
# :call-seq:
|
2020-08-13 14:16:27 -04:00
|
|
|
# ostruct.dig(name, *identifiers) -> object
|
2017-03-11 16:32:46 -05:00
|
|
|
#
|
2020-08-13 14:16:27 -04:00
|
|
|
# Finds and returns the object in nested objects
|
|
|
|
# that is specified by +name+ and +identifiers+.
|
|
|
|
# The nested objects may be instances of various classes.
|
2021-09-14 20:39:43 -04:00
|
|
|
# See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
|
2017-03-11 16:32:46 -05:00
|
|
|
#
|
2020-08-13 14:16:27 -04:00
|
|
|
# Examples:
|
2017-03-11 16:32:46 -05:00
|
|
|
# require "ostruct"
|
|
|
|
# address = OpenStruct.new("city" => "Anytown NC", "zip" => 12345)
|
|
|
|
# person = OpenStruct.new("name" => "John Smith", "address" => address)
|
2020-08-13 14:16:27 -04:00
|
|
|
# person.dig(:address, "zip") # => 12345
|
|
|
|
# person.dig(:business_address, "zip") # => nil
|
2015-11-17 00:36:03 -05:00
|
|
|
def dig(name, *names)
|
|
|
|
begin
|
|
|
|
name = name.to_sym
|
|
|
|
rescue NoMethodError
|
2020-09-25 20:58:48 -04:00
|
|
|
raise! TypeError, "#{name} is not a symbol nor a string"
|
2015-11-17 00:36:03 -05:00
|
|
|
end
|
|
|
|
@table.dig(name, *names)
|
|
|
|
end
|
|
|
|
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2021-06-14 09:53:20 -04:00
|
|
|
# Removes the named field from the object and returns the value the field
|
2021-06-14 10:30:31 -04:00
|
|
|
# contained if it was defined. You may optionally provide a block.
|
|
|
|
# If the field is not defined, the result of the block is returned,
|
2021-06-14 09:53:20 -04:00
|
|
|
# or a NameError is raised if no block was given.
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# require "ostruct"
|
|
|
|
#
|
|
|
|
# person = OpenStruct.new(name: "John", age: 70, pension: 300)
|
|
|
|
#
|
2020-09-26 01:41:46 -04:00
|
|
|
# person.delete_field!("age") # => 70
|
2017-03-11 16:32:46 -05:00
|
|
|
# person # => #<OpenStruct name="John", pension=300>
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# Setting the value to +nil+ will not remove the attribute:
|
2011-05-22 19:46:08 -04:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# person.pension = nil
|
|
|
|
# person # => #<OpenStruct name="John", pension=nil>
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2021-06-14 09:53:20 -04:00
|
|
|
# person.delete_field('number') # => NameError
|
|
|
|
#
|
|
|
|
# person.delete_field('number') { 8675_309 } # => 8675309
|
|
|
|
#
|
1998-01-16 07:19:09 -05:00
|
|
|
def delete_field(name)
|
2010-11-03 01:17:25 -04:00
|
|
|
sym = name.to_sym
|
2015-09-26 19:16:37 -04:00
|
|
|
begin
|
2018-07-26 14:46:10 -04:00
|
|
|
singleton_class.remove_method(sym, "#{sym}=")
|
2015-09-26 19:16:37 -04:00
|
|
|
rescue NameError
|
|
|
|
end
|
|
|
|
@table.delete(sym) do
|
2021-06-14 10:07:51 -04:00
|
|
|
return yield if block_given!
|
2020-09-25 20:58:48 -04:00
|
|
|
raise! NameError.new("no field `#{sym}' in #{self}", sym)
|
2015-09-26 19:16:37 -04:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
|
2005-09-05 04:29:52 -04:00
|
|
|
InspectKey = :__inspect_key__ # :nodoc:
|
|
|
|
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
|
|
|
# Returns a string containing a detailed summary of the keys and values.
|
|
|
|
#
|
1998-01-16 07:19:09 -05:00
|
|
|
def inspect
|
2009-01-13 07:52:23 -05:00
|
|
|
ids = (Thread.current[InspectKey] ||= [])
|
|
|
|
if ids.include?(object_id)
|
2017-10-24 14:08:15 -04:00
|
|
|
detail = ' ...'
|
|
|
|
else
|
|
|
|
ids << object_id
|
|
|
|
begin
|
|
|
|
detail = @table.map do |key, value|
|
|
|
|
" #{key}=#{value.inspect}"
|
|
|
|
end.join(',')
|
|
|
|
ensure
|
|
|
|
ids.pop
|
2005-09-05 04:29:52 -04:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2020-09-25 20:58:48 -04:00
|
|
|
['#<', self.class!, detail, '>'].join
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2005-09-05 04:29:52 -04:00
|
|
|
alias :to_s :inspect
|
2003-09-24 20:03:11 -04:00
|
|
|
|
2004-02-19 08:42:30 -05:00
|
|
|
attr_reader :table # :nodoc:
|
2016-03-03 00:09:02 -05:00
|
|
|
alias table! table
|
2020-09-25 20:58:48 -04:00
|
|
|
protected :table!
|
2003-09-24 20:03:11 -04:00
|
|
|
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2011-05-22 19:46:08 -04:00
|
|
|
# Compares this object and +other+ for equality. An OpenStruct is equal to
|
2012-10-28 17:18:11 -04:00
|
|
|
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
|
2011-05-22 19:46:08 -04:00
|
|
|
# equal.
|
2004-02-19 08:42:30 -05:00
|
|
|
#
|
2017-03-11 16:32:46 -05:00
|
|
|
# require "ostruct"
|
|
|
|
# first_pet = OpenStruct.new("name" => "Rowdy")
|
|
|
|
# second_pet = OpenStruct.new(:name => "Rowdy")
|
|
|
|
# third_pet = OpenStruct.new("name" => "Rowdy", :age => nil)
|
|
|
|
#
|
|
|
|
# first_pet == second_pet # => true
|
|
|
|
# first_pet == third_pet # => false
|
|
|
|
#
|
2003-09-24 20:03:11 -04:00
|
|
|
def ==(other)
|
2012-10-28 17:19:15 -04:00
|
|
|
return false unless other.kind_of?(OpenStruct)
|
2016-03-03 00:09:02 -05:00
|
|
|
@table == other.table!
|
2012-10-28 17:19:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Compares this object and +other+ for equality. An OpenStruct is eql? to
|
|
|
|
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
|
|
|
|
# eql?.
|
|
|
|
#
|
|
|
|
def eql?(other)
|
|
|
|
return false unless other.kind_of?(OpenStruct)
|
2016-03-03 00:09:02 -05:00
|
|
|
@table.eql?(other.table!)
|
2012-10-28 17:19:15 -04:00
|
|
|
end
|
|
|
|
|
2017-03-11 16:32:46 -05:00
|
|
|
# Computes a hash code for this OpenStruct.
|
2020-09-26 01:41:46 -04:00
|
|
|
def hash # :nodoc:
|
2012-10-28 17:19:15 -04:00
|
|
|
@table.hash
|
2003-09-24 20:03:11 -04:00
|
|
|
end
|
2020-09-25 20:58:48 -04:00
|
|
|
|
2020-09-26 01:27:23 -04:00
|
|
|
#
|
|
|
|
# Provides marshalling support for use by the YAML library.
|
|
|
|
#
|
|
|
|
def encode_with(coder) # :nodoc:
|
|
|
|
@table.each_pair do |key, value|
|
|
|
|
coder[key.to_s] = value
|
|
|
|
end
|
|
|
|
if @table.size == 1 && @table.key?(:table) # support for legacy format
|
|
|
|
# in the very unlikely case of a single entry called 'table'
|
|
|
|
coder['legacy_support!'] = true # add a bogus second entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Provides marshalling support for use by the YAML library.
|
|
|
|
#
|
|
|
|
def init_with(coder) # :nodoc:
|
|
|
|
h = coder.map
|
|
|
|
if h.size == 1 # support for legacy format
|
|
|
|
key, val = h.first
|
|
|
|
if key == 'table'
|
|
|
|
h = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
update_to_values!(h)
|
|
|
|
end
|
|
|
|
|
2020-09-25 20:58:48 -04:00
|
|
|
# Make all public methods (builtin or our own) accessible with `!`:
|
|
|
|
instance_methods.each do |method|
|
|
|
|
new_name = "#{method}!"
|
|
|
|
alias_method new_name, method
|
|
|
|
end
|
|
|
|
# Other builtin private methods we use:
|
|
|
|
alias_method :raise!, :raise
|
2021-06-14 10:07:51 -04:00
|
|
|
alias_method :block_given!, :block_given?
|
|
|
|
private :raise!, :block_given!
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|