Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/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
class OpenStruct
[74] Fix | Delete
[75] Fix | Delete
#
[76] Fix | Delete
# Creates a new OpenStruct object. By default, the resulting OpenStruct
[77] Fix | Delete
# object will have no attributes.
[78] Fix | Delete
#
[79] Fix | Delete
# The optional +hash+, if given, will generate attributes and values
[80] Fix | Delete
# (can be a Hash, an OpenStruct or a Struct).
[81] Fix | Delete
# For example:
[82] Fix | Delete
#
[83] Fix | Delete
# require "ostruct"
[84] Fix | Delete
# hash = { "country" => "Australia", :capital => "Canberra" }
[85] Fix | Delete
# data = OpenStruct.new(hash)
[86] Fix | Delete
#
[87] Fix | Delete
# data # => #<OpenStruct country="Australia", capital="Canberra">
[88] Fix | Delete
#
[89] Fix | Delete
def initialize(hash=nil)
[90] Fix | Delete
@table = {}
[91] Fix | Delete
if hash
[92] Fix | Delete
hash.each_pair do |k, v|
[93] Fix | Delete
k = k.to_sym
[94] Fix | Delete
@table[k] = v
[95] Fix | Delete
end
[96] Fix | Delete
end
[97] Fix | Delete
end
[98] Fix | Delete
[99] Fix | Delete
# Duplicates an OpenStruct object's Hash table.
[100] Fix | Delete
def initialize_copy(orig) # :nodoc:
[101] Fix | Delete
super
[102] Fix | Delete
@table = @table.dup
[103] Fix | Delete
end
[104] Fix | Delete
[105] Fix | Delete
#
[106] Fix | Delete
# Converts the OpenStruct to a hash with keys representing
[107] Fix | Delete
# each attribute (as symbols) and their corresponding values.
[108] Fix | Delete
#
[109] Fix | Delete
# require "ostruct"
[110] Fix | Delete
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
[111] Fix | Delete
# data.to_h # => {:country => "Australia", :capital => "Canberra" }
[112] Fix | Delete
#
[113] Fix | Delete
def to_h
[114] Fix | Delete
@table.dup
[115] Fix | Delete
end
[116] Fix | Delete
[117] Fix | Delete
#
[118] Fix | Delete
# :call-seq:
[119] Fix | Delete
# ostruct.each_pair {|name, value| block } -> ostruct
[120] Fix | Delete
# ostruct.each_pair -> Enumerator
[121] Fix | Delete
#
[122] Fix | Delete
# Yields all attributes (as symbols) along with the corresponding values
[123] Fix | Delete
# or returns an enumerator if no block is given.
[124] Fix | Delete
#
[125] Fix | Delete
# require "ostruct"
[126] Fix | Delete
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
[127] Fix | Delete
# data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
[128] Fix | Delete
#
[129] Fix | Delete
def each_pair
[130] Fix | Delete
return to_enum(__method__) { @table.size } unless block_given?
[131] Fix | Delete
@table.each_pair{|p| yield p}
[132] Fix | Delete
self
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
#
[136] Fix | Delete
# Provides marshalling support for use by the Marshal library.
[137] Fix | Delete
#
[138] Fix | Delete
def marshal_dump
[139] Fix | Delete
@table
[140] Fix | Delete
end
[141] Fix | Delete
[142] Fix | Delete
#
[143] Fix | Delete
# Provides marshalling support for use by the Marshal library.
[144] Fix | Delete
#
[145] Fix | Delete
def marshal_load(x)
[146] Fix | Delete
@table = x
[147] Fix | Delete
end
[148] Fix | Delete
[149] Fix | Delete
#
[150] Fix | Delete
# Used internally to check if the OpenStruct is able to be
[151] Fix | Delete
# modified before granting access to the internal Hash table to be modified.
[152] Fix | Delete
#
[153] Fix | Delete
def modifiable? # :nodoc:
[154] Fix | Delete
begin
[155] Fix | Delete
@modifiable = true
[156] Fix | Delete
rescue
[157] Fix | Delete
raise RuntimeError, "can't modify frozen #{self.class}", caller(3)
[158] Fix | Delete
end
[159] Fix | Delete
@table
[160] Fix | Delete
end
[161] Fix | Delete
private :modifiable?
[162] Fix | Delete
[163] Fix | Delete
# ::Kernel.warn("do not use OpenStruct#modifiable", uplevel: 1)
[164] Fix | Delete
alias modifiable modifiable? # :nodoc:
[165] Fix | Delete
protected :modifiable
[166] Fix | Delete
[167] Fix | Delete
#
[168] Fix | Delete
# Used internally to defined properties on the
[169] Fix | Delete
# OpenStruct. It does this by using the metaprogramming function
[170] Fix | Delete
# define_singleton_method for both the getter method and the setter method.
[171] Fix | Delete
#
[172] Fix | Delete
def new_ostruct_member!(name) # :nodoc:
[173] Fix | Delete
name = name.to_sym
[174] Fix | Delete
unless singleton_class.method_defined?(name)
[175] Fix | Delete
define_singleton_method(name) { @table[name] }
[176] Fix | Delete
define_singleton_method("#{name}=") {|x| modifiable?[name] = x}
[177] Fix | Delete
end
[178] Fix | Delete
name
[179] Fix | Delete
end
[180] Fix | Delete
private :new_ostruct_member!
[181] Fix | Delete
[182] Fix | Delete
# ::Kernel.warn("do not use OpenStruct#new_ostruct_member", uplevel: 1)
[183] Fix | Delete
alias new_ostruct_member new_ostruct_member! # :nodoc:
[184] Fix | Delete
protected :new_ostruct_member
[185] Fix | Delete
[186] Fix | Delete
def freeze
[187] Fix | Delete
@table.each_key {|key| new_ostruct_member!(key)}
[188] Fix | Delete
super
[189] Fix | Delete
end
[190] Fix | Delete
[191] Fix | Delete
def respond_to_missing?(mid, include_private = false) # :nodoc:
[192] Fix | Delete
mname = mid.to_s.chomp("=").to_sym
[193] Fix | Delete
@table&.key?(mname) || super
[194] Fix | Delete
end
[195] Fix | Delete
[196] Fix | Delete
def method_missing(mid, *args) # :nodoc:
[197] Fix | Delete
len = args.length
[198] Fix | Delete
if mname = mid[/.*(?==\z)/m]
[199] Fix | Delete
if len != 1
[200] Fix | Delete
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
[201] Fix | Delete
end
[202] Fix | Delete
modifiable?[new_ostruct_member!(mname)] = args[0]
[203] Fix | Delete
elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
[204] Fix | Delete
if @table.key?(mid)
[205] Fix | Delete
new_ostruct_member!(mid) unless frozen?
[206] Fix | Delete
@table[mid]
[207] Fix | Delete
end
[208] Fix | Delete
else
[209] Fix | Delete
begin
[210] Fix | Delete
super
[211] Fix | Delete
rescue NoMethodError => err
[212] Fix | Delete
err.backtrace.shift
[213] Fix | Delete
raise
[214] Fix | Delete
end
[215] Fix | Delete
end
[216] Fix | Delete
end
[217] Fix | Delete
[218] Fix | Delete
#
[219] Fix | Delete
# :call-seq:
[220] Fix | Delete
# ostruct[name] -> object
[221] Fix | Delete
#
[222] Fix | Delete
# Returns the value of an attribute.
[223] Fix | Delete
#
[224] Fix | Delete
# require "ostruct"
[225] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
[226] Fix | Delete
# person[:age] # => 70, same as person.age
[227] Fix | Delete
#
[228] Fix | Delete
def [](name)
[229] Fix | Delete
@table[name.to_sym]
[230] Fix | Delete
end
[231] Fix | Delete
[232] Fix | Delete
#
[233] Fix | Delete
# :call-seq:
[234] Fix | Delete
# ostruct[name] = obj -> obj
[235] Fix | Delete
#
[236] Fix | Delete
# Sets the value of an attribute.
[237] Fix | Delete
#
[238] Fix | Delete
# require "ostruct"
[239] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
[240] Fix | Delete
# person[:age] = 42 # equivalent to person.age = 42
[241] Fix | Delete
# person.age # => 42
[242] Fix | Delete
#
[243] Fix | Delete
def []=(name, value)
[244] Fix | Delete
modifiable?[new_ostruct_member!(name)] = value
[245] Fix | Delete
end
[246] Fix | Delete
[247] Fix | Delete
#
[248] Fix | Delete
# :call-seq:
[249] Fix | Delete
# ostruct.dig(name, ...) -> object
[250] Fix | Delete
#
[251] Fix | Delete
# Extracts the nested value specified by the sequence of +name+
[252] Fix | Delete
# objects by calling +dig+ at each step, returning +nil+ if any
[253] Fix | Delete
# intermediate step is +nil+.
[254] Fix | Delete
#
[255] Fix | Delete
# require "ostruct"
[256] Fix | Delete
# address = OpenStruct.new("city" => "Anytown NC", "zip" => 12345)
[257] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "address" => address)
[258] Fix | Delete
#
[259] Fix | Delete
# person.dig(:address, "zip") # => 12345
[260] Fix | Delete
# person.dig(:business_address, "zip") # => nil
[261] Fix | Delete
#
[262] Fix | Delete
# data = OpenStruct.new(:array => [1, [2, 3]])
[263] Fix | Delete
#
[264] Fix | Delete
# data.dig(:array, 1, 0) # => 2
[265] Fix | Delete
# data.dig(:array, 0, 0) # TypeError: Integer does not have #dig method
[266] Fix | Delete
#
[267] Fix | Delete
def dig(name, *names)
[268] Fix | Delete
begin
[269] Fix | Delete
name = name.to_sym
[270] Fix | Delete
rescue NoMethodError
[271] Fix | Delete
raise TypeError, "#{name} is not a symbol nor a string"
[272] Fix | Delete
end
[273] Fix | Delete
@table.dig(name, *names)
[274] Fix | Delete
end
[275] Fix | Delete
[276] Fix | Delete
#
[277] Fix | Delete
# Removes the named field from the object. Returns the value that the field
[278] Fix | Delete
# contained if it was defined.
[279] Fix | Delete
#
[280] Fix | Delete
# require "ostruct"
[281] Fix | Delete
#
[282] Fix | Delete
# person = OpenStruct.new(name: "John", age: 70, pension: 300)
[283] Fix | Delete
#
[284] Fix | Delete
# person.delete_field("age") # => 70
[285] Fix | Delete
# person # => #<OpenStruct name="John", pension=300>
[286] Fix | Delete
#
[287] Fix | Delete
# Setting the value to +nil+ will not remove the attribute:
[288] Fix | Delete
#
[289] Fix | Delete
# person.pension = nil
[290] Fix | Delete
# person # => #<OpenStruct name="John", pension=nil>
[291] Fix | Delete
#
[292] Fix | Delete
def delete_field(name)
[293] Fix | Delete
sym = name.to_sym
[294] Fix | Delete
begin
[295] Fix | Delete
singleton_class.__send__(:remove_method, sym, "#{sym}=")
[296] Fix | Delete
rescue NameError
[297] Fix | Delete
end
[298] Fix | Delete
@table.delete(sym) do
[299] Fix | Delete
raise NameError.new("no field `#{sym}' in #{self}", sym)
[300] Fix | Delete
end
[301] Fix | Delete
end
[302] Fix | Delete
[303] Fix | Delete
InspectKey = :__inspect_key__ # :nodoc:
[304] Fix | Delete
[305] Fix | Delete
#
[306] Fix | Delete
# Returns a string containing a detailed summary of the keys and values.
[307] Fix | Delete
#
[308] Fix | Delete
def inspect
[309] Fix | Delete
ids = (Thread.current[InspectKey] ||= [])
[310] Fix | Delete
if ids.include?(object_id)
[311] Fix | Delete
detail = ' ...'
[312] Fix | Delete
else
[313] Fix | Delete
ids << object_id
[314] Fix | Delete
begin
[315] Fix | Delete
detail = @table.map do |key, value|
[316] Fix | Delete
" #{key}=#{value.inspect}"
[317] Fix | Delete
end.join(',')
[318] Fix | Delete
ensure
[319] Fix | Delete
ids.pop
[320] Fix | Delete
end
[321] Fix | Delete
end
[322] Fix | Delete
['#<', self.class, detail, '>'].join
[323] Fix | Delete
end
[324] Fix | Delete
alias :to_s :inspect
[325] Fix | Delete
[326] Fix | Delete
attr_reader :table # :nodoc:
[327] Fix | Delete
protected :table
[328] Fix | Delete
alias table! table
[329] Fix | Delete
[330] Fix | Delete
#
[331] Fix | Delete
# Compares this object and +other+ for equality. An OpenStruct is equal to
[332] Fix | Delete
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
[333] Fix | Delete
# equal.
[334] Fix | Delete
#
[335] Fix | Delete
# require "ostruct"
[336] Fix | Delete
# first_pet = OpenStruct.new("name" => "Rowdy")
[337] Fix | Delete
# second_pet = OpenStruct.new(:name => "Rowdy")
[338] Fix | Delete
# third_pet = OpenStruct.new("name" => "Rowdy", :age => nil)
[339] Fix | Delete
#
[340] Fix | Delete
# first_pet == second_pet # => true
[341] Fix | Delete
# first_pet == third_pet # => false
[342] Fix | Delete
#
[343] Fix | Delete
def ==(other)
[344] Fix | Delete
return false unless other.kind_of?(OpenStruct)
[345] Fix | Delete
@table == other.table!
[346] Fix | Delete
end
[347] Fix | Delete
[348] Fix | Delete
#
[349] Fix | Delete
# Compares this object and +other+ for equality. An OpenStruct is eql? to
[350] Fix | Delete
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
[351] Fix | Delete
# eql?.
[352] Fix | Delete
#
[353] Fix | Delete
def eql?(other)
[354] Fix | Delete
return false unless other.kind_of?(OpenStruct)
[355] Fix | Delete
@table.eql?(other.table!)
[356] Fix | Delete
end
[357] Fix | Delete
[358] Fix | Delete
# Computes a hash code for this OpenStruct.
[359] Fix | Delete
# Two OpenStruct objects with the same content will have the same hash code
[360] Fix | Delete
# (and will compare using #eql?).
[361] Fix | Delete
#
[362] Fix | Delete
# See also Object#hash.
[363] Fix | Delete
def hash
[364] Fix | Delete
@table.hash
[365] Fix | Delete
end
[366] Fix | Delete
end
[367] Fix | Delete
[368] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function