Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/share/ruby
File: ostruct.rb
# frozen_string_literal: true
[0] Fix | Delete
#
[1] Fix | Delete
# = ostruct.rb: OpenStruct implementation
[2] Fix | Delete
#
[3] Fix | Delete
# Author:: Yukihiro Matsumoto
[4] Fix | Delete
# Documentation:: Gavin Sinclair
[5] Fix | Delete
#
[6] Fix | Delete
# OpenStruct allows the creation of data objects with arbitrary attributes.
[7] Fix | Delete
# See OpenStruct for an example.
[8] Fix | Delete
#
[9] Fix | Delete
[10] Fix | Delete
#
[11] Fix | Delete
# An OpenStruct is a data structure, similar to a Hash, that allows the
[12] Fix | Delete
# definition of arbitrary attributes with their accompanying values. This is
[13] Fix | Delete
# accomplished by using Ruby's metaprogramming to define methods on the class
[14] Fix | Delete
# itself.
[15] Fix | Delete
#
[16] Fix | Delete
# == Examples
[17] Fix | Delete
#
[18] Fix | Delete
# require "ostruct"
[19] Fix | Delete
#
[20] Fix | Delete
# person = OpenStruct.new
[21] Fix | Delete
# person.name = "John Smith"
[22] Fix | Delete
# person.age = 70
[23] Fix | Delete
#
[24] Fix | Delete
# person.name # => "John Smith"
[25] Fix | Delete
# person.age # => 70
[26] Fix | Delete
# person.address # => nil
[27] Fix | Delete
#
[28] Fix | Delete
# An OpenStruct employs a Hash internally to store the attributes and values
[29] Fix | Delete
# and can even be initialized with one:
[30] Fix | Delete
#
[31] Fix | Delete
# australia = OpenStruct.new(:country => "Australia", :capital => "Canberra")
[32] Fix | Delete
# # => #<OpenStruct country="Australia", capital="Canberra">
[33] Fix | Delete
#
[34] Fix | Delete
# Hash keys with spaces or characters that could normally not be used for
[35] Fix | Delete
# method calls (e.g. <code>()[]*</code>) will not be immediately available
[36] Fix | Delete
# on the OpenStruct object as a method for retrieval or assignment, but can
[37] Fix | Delete
# still be reached through the Object#send method.
[38] Fix | Delete
#
[39] Fix | Delete
# measurements = OpenStruct.new("length (in inches)" => 24)
[40] Fix | Delete
# measurements.send("length (in inches)") # => 24
[41] Fix | Delete
#
[42] Fix | Delete
# message = OpenStruct.new(:queued? => true)
[43] Fix | Delete
# message.queued? # => true
[44] Fix | Delete
# message.send("queued?=", false)
[45] Fix | Delete
# message.queued? # => false
[46] Fix | Delete
#
[47] Fix | Delete
# Removing the presence of an attribute requires the execution of the
[48] Fix | Delete
# delete_field method as setting the property value to +nil+ will not
[49] Fix | Delete
# remove the attribute.
[50] Fix | Delete
#
[51] Fix | Delete
# first_pet = OpenStruct.new(:name => "Rowdy", :owner => "John Smith")
[52] Fix | Delete
# second_pet = OpenStruct.new(:name => "Rowdy")
[53] Fix | Delete
#
[54] Fix | Delete
# first_pet.owner = nil
[55] Fix | Delete
# first_pet # => #<OpenStruct name="Rowdy", owner=nil>
[56] Fix | Delete
# first_pet == second_pet # => false
[57] Fix | Delete
#
[58] Fix | Delete
# first_pet.delete_field(:owner)
[59] Fix | Delete
# first_pet # => #<OpenStruct name="Rowdy">
[60] Fix | Delete
# first_pet == second_pet # => true
[61] Fix | Delete
#
[62] Fix | Delete
#
[63] Fix | Delete
# == Implementation
[64] Fix | Delete
#
[65] Fix | Delete
# An OpenStruct utilizes Ruby's method lookup structure to find and define the
[66] Fix | Delete
# necessary methods for properties. This is accomplished through the methods
[67] Fix | Delete
# method_missing and define_singleton_method.
[68] Fix | Delete
#
[69] Fix | Delete
# This should be a consideration if there is a concern about the performance of
[70] Fix | Delete
# the objects that are created, as there is much more overhead in the setting
[71] Fix | Delete
# of these properties compared to using a Hash or a Struct.
[72] Fix | Delete
#
[73] Fix | Delete
[74] Fix | Delete
require_relative 'ostruct/version'
[75] Fix | Delete
[76] Fix | Delete
class OpenStruct
[77] Fix | Delete
[78] Fix | Delete
#
[79] Fix | Delete
# Creates a new OpenStruct object. By default, the resulting OpenStruct
[80] Fix | Delete
# object will have no attributes.
[81] Fix | Delete
#
[82] Fix | Delete
# The optional +hash+, if given, will generate attributes and values
[83] Fix | Delete
# (can be a Hash, an OpenStruct or a Struct).
[84] Fix | Delete
# For example:
[85] Fix | Delete
#
[86] Fix | Delete
# require "ostruct"
[87] Fix | Delete
# hash = { "country" => "Australia", :capital => "Canberra" }
[88] Fix | Delete
# data = OpenStruct.new(hash)
[89] Fix | Delete
#
[90] Fix | Delete
# data # => #<OpenStruct country="Australia", capital="Canberra">
[91] Fix | Delete
#
[92] Fix | Delete
def initialize(hash=nil)
[93] Fix | Delete
@table = {}
[94] Fix | Delete
if hash
[95] Fix | Delete
hash.each_pair do |k, v|
[96] Fix | Delete
k = k.to_sym
[97] Fix | Delete
@table[k] = v
[98] Fix | Delete
end
[99] Fix | Delete
end
[100] Fix | Delete
end
[101] Fix | Delete
[102] Fix | Delete
# Duplicates an OpenStruct object's Hash table.
[103] Fix | Delete
def initialize_copy(orig) # :nodoc:
[104] Fix | Delete
super
[105] Fix | Delete
@table = @table.dup
[106] Fix | Delete
end
[107] Fix | Delete
[108] Fix | Delete
#
[109] Fix | Delete
# call-seq:
[110] Fix | Delete
# ostruct.to_h -> hash
[111] Fix | Delete
# ostruct.to_h {|name, value| block } -> hash
[112] Fix | Delete
#
[113] Fix | Delete
# Converts the OpenStruct to a hash with keys representing
[114] Fix | Delete
# each attribute (as symbols) and their corresponding values.
[115] Fix | Delete
#
[116] Fix | Delete
# If a block is given, the results of the block on each pair of
[117] Fix | Delete
# the receiver will be used as pairs.
[118] Fix | Delete
#
[119] Fix | Delete
# require "ostruct"
[120] Fix | Delete
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
[121] Fix | Delete
# data.to_h # => {:country => "Australia", :capital => "Canberra" }
[122] Fix | Delete
# data.to_h {|name, value| [name.to_s, value.upcase] }
[123] Fix | Delete
# # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
[124] Fix | Delete
#
[125] Fix | Delete
def to_h(&block)
[126] Fix | Delete
if block_given?
[127] Fix | Delete
@table.to_h(&block)
[128] Fix | Delete
else
[129] Fix | Delete
@table.dup
[130] Fix | Delete
end
[131] Fix | Delete
end
[132] Fix | Delete
[133] Fix | Delete
#
[134] Fix | Delete
# :call-seq:
[135] Fix | Delete
# ostruct.each_pair {|name, value| block } -> ostruct
[136] Fix | Delete
# ostruct.each_pair -> Enumerator
[137] Fix | Delete
#
[138] Fix | Delete
# Yields all attributes (as symbols) along with the corresponding values
[139] Fix | Delete
# or returns an enumerator if no block is given.
[140] Fix | Delete
#
[141] Fix | Delete
# require "ostruct"
[142] Fix | Delete
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
[143] Fix | Delete
# data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
[144] Fix | Delete
#
[145] Fix | Delete
def each_pair
[146] Fix | Delete
return to_enum(__method__) { @table.size } unless block_given?
[147] Fix | Delete
@table.each_pair{|p| yield p}
[148] Fix | Delete
self
[149] Fix | Delete
end
[150] Fix | Delete
[151] Fix | Delete
#
[152] Fix | Delete
# Provides marshalling support for use by the Marshal library.
[153] Fix | Delete
#
[154] Fix | Delete
def marshal_dump
[155] Fix | Delete
@table
[156] Fix | Delete
end
[157] Fix | Delete
[158] Fix | Delete
#
[159] Fix | Delete
# Provides marshalling support for use by the Marshal library.
[160] Fix | Delete
#
[161] Fix | Delete
def marshal_load(x)
[162] Fix | Delete
@table = x
[163] Fix | Delete
end
[164] Fix | Delete
[165] Fix | Delete
#
[166] Fix | Delete
# Used internally to check if the OpenStruct is able to be
[167] Fix | Delete
# modified before granting access to the internal Hash table to be modified.
[168] Fix | Delete
#
[169] Fix | Delete
def modifiable? # :nodoc:
[170] Fix | Delete
begin
[171] Fix | Delete
@modifiable = true
[172] Fix | Delete
rescue
[173] Fix | Delete
exception_class = defined?(FrozenError) ? FrozenError : RuntimeError
[174] Fix | Delete
raise exception_class, "can't modify frozen #{self.class}", caller(3)
[175] Fix | Delete
end
[176] Fix | Delete
@table
[177] Fix | Delete
end
[178] Fix | Delete
private :modifiable?
[179] Fix | Delete
[180] Fix | Delete
#
[181] Fix | Delete
# Used internally to defined properties on the
[182] Fix | Delete
# OpenStruct. It does this by using the metaprogramming function
[183] Fix | Delete
# define_singleton_method for both the getter method and the setter method.
[184] Fix | Delete
#
[185] Fix | Delete
def new_ostruct_member!(name) # :nodoc:
[186] Fix | Delete
name = name.to_sym
[187] Fix | Delete
unless singleton_class.method_defined?(name)
[188] Fix | Delete
define_singleton_method(name) { @table[name] }
[189] Fix | Delete
define_singleton_method("#{name}=") {|x| modifiable?[name] = x}
[190] Fix | Delete
end
[191] Fix | Delete
name
[192] Fix | Delete
end
[193] Fix | Delete
private :new_ostruct_member!
[194] Fix | Delete
[195] Fix | Delete
def freeze
[196] Fix | Delete
@table.each_key {|key| new_ostruct_member!(key)}
[197] Fix | Delete
super
[198] Fix | Delete
end
[199] Fix | Delete
[200] Fix | Delete
def respond_to_missing?(mid, include_private = false) # :nodoc:
[201] Fix | Delete
mname = mid.to_s.chomp("=").to_sym
[202] Fix | Delete
defined?(@table) && @table.key?(mname) || super
[203] Fix | Delete
end
[204] Fix | Delete
[205] Fix | Delete
def method_missing(mid, *args) # :nodoc:
[206] Fix | Delete
len = args.length
[207] Fix | Delete
if mname = mid[/.*(?==\z)/m]
[208] Fix | Delete
if len != 1
[209] Fix | Delete
raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
[210] Fix | Delete
end
[211] Fix | Delete
modifiable?[new_ostruct_member!(mname)] = args[0]
[212] Fix | Delete
elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
[213] Fix | Delete
if @table.key?(mid)
[214] Fix | Delete
new_ostruct_member!(mid) unless frozen?
[215] Fix | Delete
@table[mid]
[216] Fix | Delete
end
[217] Fix | Delete
elsif @table.key?(mid)
[218] Fix | Delete
raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)"
[219] Fix | Delete
else
[220] Fix | Delete
begin
[221] Fix | Delete
super
[222] Fix | Delete
rescue NoMethodError => err
[223] Fix | Delete
err.backtrace.shift
[224] Fix | Delete
raise
[225] Fix | Delete
end
[226] Fix | Delete
end
[227] Fix | Delete
end
[228] Fix | Delete
[229] Fix | Delete
#
[230] Fix | Delete
# :call-seq:
[231] Fix | Delete
# ostruct[name] -> object
[232] Fix | Delete
#
[233] Fix | Delete
# Returns the value of an attribute.
[234] Fix | Delete
#
[235] Fix | Delete
# require "ostruct"
[236] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
[237] Fix | Delete
# person[:age] # => 70, same as person.age
[238] Fix | Delete
#
[239] Fix | Delete
def [](name)
[240] Fix | Delete
@table[name.to_sym]
[241] Fix | Delete
end
[242] Fix | Delete
[243] Fix | Delete
#
[244] Fix | Delete
# :call-seq:
[245] Fix | Delete
# ostruct[name] = obj -> obj
[246] Fix | Delete
#
[247] Fix | Delete
# Sets the value of an attribute.
[248] Fix | Delete
#
[249] Fix | Delete
# require "ostruct"
[250] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
[251] Fix | Delete
# person[:age] = 42 # equivalent to person.age = 42
[252] Fix | Delete
# person.age # => 42
[253] Fix | Delete
#
[254] Fix | Delete
def []=(name, value)
[255] Fix | Delete
modifiable?[new_ostruct_member!(name)] = value
[256] Fix | Delete
end
[257] Fix | Delete
[258] Fix | Delete
#
[259] Fix | Delete
# :call-seq:
[260] Fix | Delete
# ostruct.dig(name, ...) -> object
[261] Fix | Delete
#
[262] Fix | Delete
# Extracts the nested value specified by the sequence of +name+
[263] Fix | Delete
# objects by calling +dig+ at each step, returning +nil+ if any
[264] Fix | Delete
# intermediate step is +nil+.
[265] Fix | Delete
#
[266] Fix | Delete
# require "ostruct"
[267] Fix | Delete
# address = OpenStruct.new("city" => "Anytown NC", "zip" => 12345)
[268] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "address" => address)
[269] Fix | Delete
#
[270] Fix | Delete
# person.dig(:address, "zip") # => 12345
[271] Fix | Delete
# person.dig(:business_address, "zip") # => nil
[272] Fix | Delete
#
[273] Fix | Delete
# data = OpenStruct.new(:array => [1, [2, 3]])
[274] Fix | Delete
#
[275] Fix | Delete
# data.dig(:array, 1, 0) # => 2
[276] Fix | Delete
# data.dig(:array, 0, 0) # TypeError: Integer does not have #dig method
[277] Fix | Delete
#
[278] Fix | Delete
def dig(name, *names)
[279] Fix | Delete
begin
[280] Fix | Delete
name = name.to_sym
[281] Fix | Delete
rescue NoMethodError
[282] Fix | Delete
raise TypeError, "#{name} is not a symbol nor a string"
[283] Fix | Delete
end
[284] Fix | Delete
@table.dig(name, *names)
[285] Fix | Delete
end
[286] Fix | Delete
[287] Fix | Delete
#
[288] Fix | Delete
# Removes the named field from the object. Returns the value that the field
[289] Fix | Delete
# contained if it was defined.
[290] Fix | Delete
#
[291] Fix | Delete
# require "ostruct"
[292] Fix | Delete
#
[293] Fix | Delete
# person = OpenStruct.new(name: "John", age: 70, pension: 300)
[294] Fix | Delete
#
[295] Fix | Delete
# person.delete_field("age") # => 70
[296] Fix | Delete
# person # => #<OpenStruct name="John", pension=300>
[297] Fix | Delete
#
[298] Fix | Delete
# Setting the value to +nil+ will not remove the attribute:
[299] Fix | Delete
#
[300] Fix | Delete
# person.pension = nil
[301] Fix | Delete
# person # => #<OpenStruct name="John", pension=nil>
[302] Fix | Delete
#
[303] Fix | Delete
def delete_field(name)
[304] Fix | Delete
sym = name.to_sym
[305] Fix | Delete
begin
[306] Fix | Delete
singleton_class.remove_method(sym, "#{sym}=")
[307] Fix | Delete
rescue NameError
[308] Fix | Delete
end
[309] Fix | Delete
@table.delete(sym) do
[310] Fix | Delete
raise NameError.new("no field `#{sym}' in #{self}", sym)
[311] Fix | Delete
end
[312] Fix | Delete
end
[313] Fix | Delete
[314] Fix | Delete
InspectKey = :__inspect_key__ # :nodoc:
[315] Fix | Delete
[316] Fix | Delete
#
[317] Fix | Delete
# Returns a string containing a detailed summary of the keys and values.
[318] Fix | Delete
#
[319] Fix | Delete
def inspect
[320] Fix | Delete
ids = (Thread.current[InspectKey] ||= [])
[321] Fix | Delete
if ids.include?(object_id)
[322] Fix | Delete
detail = ' ...'
[323] Fix | Delete
else
[324] Fix | Delete
ids << object_id
[325] Fix | Delete
begin
[326] Fix | Delete
detail = @table.map do |key, value|
[327] Fix | Delete
" #{key}=#{value.inspect}"
[328] Fix | Delete
end.join(',')
[329] Fix | Delete
ensure
[330] Fix | Delete
ids.pop
[331] Fix | Delete
end
[332] Fix | Delete
end
[333] Fix | Delete
['#<', self.class, detail, '>'].join
[334] Fix | Delete
end
[335] Fix | Delete
alias :to_s :inspect
[336] Fix | Delete
[337] Fix | Delete
attr_reader :table # :nodoc:
[338] Fix | Delete
protected :table
[339] Fix | Delete
alias table! table
[340] Fix | Delete
[341] Fix | Delete
#
[342] Fix | Delete
# Compares this object and +other+ for equality. An OpenStruct is equal to
[343] Fix | Delete
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
[344] Fix | Delete
# equal.
[345] Fix | Delete
#
[346] Fix | Delete
# require "ostruct"
[347] Fix | Delete
# first_pet = OpenStruct.new("name" => "Rowdy")
[348] Fix | Delete
# second_pet = OpenStruct.new(:name => "Rowdy")
[349] Fix | Delete
# third_pet = OpenStruct.new("name" => "Rowdy", :age => nil)
[350] Fix | Delete
#
[351] Fix | Delete
# first_pet == second_pet # => true
[352] Fix | Delete
# first_pet == third_pet # => false
[353] Fix | Delete
#
[354] Fix | Delete
def ==(other)
[355] Fix | Delete
return false unless other.kind_of?(OpenStruct)
[356] Fix | Delete
@table == other.table!
[357] Fix | Delete
end
[358] Fix | Delete
[359] Fix | Delete
#
[360] Fix | Delete
# Compares this object and +other+ for equality. An OpenStruct is eql? to
[361] Fix | Delete
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
[362] Fix | Delete
# eql?.
[363] Fix | Delete
#
[364] Fix | Delete
def eql?(other)
[365] Fix | Delete
return false unless other.kind_of?(OpenStruct)
[366] Fix | Delete
@table.eql?(other.table!)
[367] Fix | Delete
end
[368] Fix | Delete
[369] Fix | Delete
# Computes a hash code for this OpenStruct.
[370] Fix | Delete
# Two OpenStruct objects with the same content will have the same hash code
[371] Fix | Delete
# (and will compare using #eql?).
[372] Fix | Delete
#
[373] Fix | Delete
# See also Object#hash.
[374] Fix | Delete
def hash
[375] Fix | Delete
@table.hash
[376] Fix | Delete
end
[377] Fix | Delete
end
[378] Fix | Delete
[379] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function