Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/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 or using [].
[38] Fix | Delete
#
[39] Fix | Delete
# measurements = OpenStruct.new("length (in inches)" => 24)
[40] Fix | Delete
# measurements[:"length (in inches)"] # => 24
[41] Fix | Delete
# measurements.send("length (in inches)") # => 24
[42] Fix | Delete
#
[43] Fix | Delete
# message = OpenStruct.new(:queued? => true)
[44] Fix | Delete
# message.queued? # => true
[45] Fix | Delete
# message.send("queued?=", false)
[46] Fix | Delete
# message.queued? # => false
[47] Fix | Delete
#
[48] Fix | Delete
# Removing the presence of an attribute requires the execution of the
[49] Fix | Delete
# delete_field method as setting the property value to +nil+ will not
[50] Fix | Delete
# remove the attribute.
[51] Fix | Delete
#
[52] Fix | Delete
# first_pet = OpenStruct.new(:name => "Rowdy", :owner => "John Smith")
[53] Fix | Delete
# second_pet = OpenStruct.new(:name => "Rowdy")
[54] Fix | Delete
#
[55] Fix | Delete
# first_pet.owner = nil
[56] Fix | Delete
# first_pet # => #<OpenStruct name="Rowdy", owner=nil>
[57] Fix | Delete
# first_pet == second_pet # => false
[58] Fix | Delete
#
[59] Fix | Delete
# first_pet.delete_field(:owner)
[60] Fix | Delete
# first_pet # => #<OpenStruct name="Rowdy">
[61] Fix | Delete
# first_pet == second_pet # => true
[62] Fix | Delete
#
[63] Fix | Delete
# Ractor compatibility: A frozen OpenStruct with shareable values is itself shareable.
[64] Fix | Delete
#
[65] Fix | Delete
# == Caveats
[66] Fix | Delete
#
[67] Fix | Delete
# An OpenStruct utilizes Ruby's method lookup structure to find and define the
[68] Fix | Delete
# necessary methods for properties. This is accomplished through the methods
[69] Fix | Delete
# method_missing and define_singleton_method.
[70] Fix | Delete
#
[71] Fix | Delete
# This should be a consideration if there is a concern about the performance of
[72] Fix | Delete
# the objects that are created, as there is much more overhead in the setting
[73] Fix | Delete
# of these properties compared to using a Hash or a Struct.
[74] Fix | Delete
# Creating an open struct from a small Hash and accessing a few of the
[75] Fix | Delete
# entries can be 200 times slower than accessing the hash directly.
[76] Fix | Delete
#
[77] Fix | Delete
# This is a potential security issue; building OpenStruct from untrusted user data
[78] Fix | Delete
# (e.g. JSON web request) may be susceptible to a "symbol denial of service" attack
[79] Fix | Delete
# since the keys create methods and names of methods are never garbage collected.
[80] Fix | Delete
#
[81] Fix | Delete
# This may also be the source of incompatibilities between Ruby versions:
[82] Fix | Delete
#
[83] Fix | Delete
# o = OpenStruct.new
[84] Fix | Delete
# o.then # => nil in Ruby < 2.6, enumerator for Ruby >= 2.6
[85] Fix | Delete
#
[86] Fix | Delete
# Builtin methods may be overwritten this way, which may be a source of bugs
[87] Fix | Delete
# or security issues:
[88] Fix | Delete
#
[89] Fix | Delete
# o = OpenStruct.new
[90] Fix | Delete
# o.methods # => [:to_h, :marshal_load, :marshal_dump, :each_pair, ...
[91] Fix | Delete
# o.methods = [:foo, :bar]
[92] Fix | Delete
# o.methods # => [:foo, :bar]
[93] Fix | Delete
#
[94] Fix | Delete
# To help remedy clashes, OpenStruct uses only protected/private methods ending with `!`
[95] Fix | Delete
# and defines aliases for builtin public methods by adding a `!`:
[96] Fix | Delete
#
[97] Fix | Delete
# o = OpenStruct.new(make: 'Bentley', class: :luxury)
[98] Fix | Delete
# o.class # => :luxury
[99] Fix | Delete
# o.class! # => OpenStruct
[100] Fix | Delete
#
[101] Fix | Delete
# It is recommended (but not enforced) to not use fields ending in `!`;
[102] Fix | Delete
# Note that a subclass' methods may not be overwritten, nor can OpenStruct's own methods
[103] Fix | Delete
# ending with `!`.
[104] Fix | Delete
#
[105] Fix | Delete
# For all these reasons, consider not using OpenStruct at all.
[106] Fix | Delete
#
[107] Fix | Delete
class OpenStruct
[108] Fix | Delete
VERSION = "0.3.1"
[109] Fix | Delete
[110] Fix | Delete
#
[111] Fix | Delete
# Creates a new OpenStruct object. By default, the resulting OpenStruct
[112] Fix | Delete
# object will have no attributes.
[113] Fix | Delete
#
[114] Fix | Delete
# The optional +hash+, if given, will generate attributes and values
[115] Fix | Delete
# (can be a Hash, an OpenStruct or a Struct).
[116] Fix | Delete
# For example:
[117] Fix | Delete
#
[118] Fix | Delete
# require "ostruct"
[119] Fix | Delete
# hash = { "country" => "Australia", :capital => "Canberra" }
[120] Fix | Delete
# data = OpenStruct.new(hash)
[121] Fix | Delete
#
[122] Fix | Delete
# data # => #<OpenStruct country="Australia", capital="Canberra">
[123] Fix | Delete
#
[124] Fix | Delete
def initialize(hash=nil)
[125] Fix | Delete
if hash
[126] Fix | Delete
update_to_values!(hash)
[127] Fix | Delete
else
[128] Fix | Delete
@table = {}
[129] Fix | Delete
end
[130] Fix | Delete
end
[131] Fix | Delete
[132] Fix | Delete
# Duplicates an OpenStruct object's Hash table.
[133] Fix | Delete
private def initialize_clone(orig) # :nodoc:
[134] Fix | Delete
super # clones the singleton class for us
[135] Fix | Delete
@table = @table.dup unless @table.frozen?
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
private def initialize_dup(orig) # :nodoc:
[139] Fix | Delete
super
[140] Fix | Delete
update_to_values!(@table)
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
private def update_to_values!(hash) # :nodoc:
[144] Fix | Delete
@table = {}
[145] Fix | Delete
hash.each_pair do |k, v|
[146] Fix | Delete
set_ostruct_member_value!(k, v)
[147] Fix | Delete
end
[148] Fix | Delete
end
[149] Fix | Delete
[150] Fix | Delete
#
[151] Fix | Delete
# call-seq:
[152] Fix | Delete
# ostruct.to_h -> hash
[153] Fix | Delete
# ostruct.to_h {|name, value| block } -> hash
[154] Fix | Delete
#
[155] Fix | Delete
# Converts the OpenStruct to a hash with keys representing
[156] Fix | Delete
# each attribute (as symbols) and their corresponding values.
[157] Fix | Delete
#
[158] Fix | Delete
# If a block is given, the results of the block on each pair of
[159] Fix | Delete
# the receiver will be used as pairs.
[160] Fix | Delete
#
[161] Fix | Delete
# require "ostruct"
[162] Fix | Delete
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
[163] Fix | Delete
# data.to_h # => {:country => "Australia", :capital => "Canberra" }
[164] Fix | Delete
# data.to_h {|name, value| [name.to_s, value.upcase] }
[165] Fix | Delete
# # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
[166] Fix | Delete
#
[167] Fix | Delete
def to_h(&block)
[168] Fix | Delete
if block
[169] Fix | Delete
@table.to_h(&block)
[170] Fix | Delete
else
[171] Fix | Delete
@table.dup
[172] Fix | Delete
end
[173] Fix | Delete
end
[174] Fix | Delete
[175] Fix | Delete
#
[176] Fix | Delete
# :call-seq:
[177] Fix | Delete
# ostruct.each_pair {|name, value| block } -> ostruct
[178] Fix | Delete
# ostruct.each_pair -> Enumerator
[179] Fix | Delete
#
[180] Fix | Delete
# Yields all attributes (as symbols) along with the corresponding values
[181] Fix | Delete
# or returns an enumerator if no block is given.
[182] Fix | Delete
#
[183] Fix | Delete
# require "ostruct"
[184] Fix | Delete
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
[185] Fix | Delete
# data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
[186] Fix | Delete
#
[187] Fix | Delete
def each_pair
[188] Fix | Delete
return to_enum(__method__) { @table.size } unless block_given?
[189] Fix | Delete
@table.each_pair{|p| yield p}
[190] Fix | Delete
self
[191] Fix | Delete
end
[192] Fix | Delete
[193] Fix | Delete
#
[194] Fix | Delete
# Provides marshalling support for use by the Marshal library.
[195] Fix | Delete
#
[196] Fix | Delete
def marshal_dump # :nodoc:
[197] Fix | Delete
@table
[198] Fix | Delete
end
[199] Fix | Delete
[200] Fix | Delete
#
[201] Fix | Delete
# Provides marshalling support for use by the Marshal library.
[202] Fix | Delete
#
[203] Fix | Delete
alias_method :marshal_load, :update_to_values! # :nodoc:
[204] Fix | Delete
[205] Fix | Delete
#
[206] Fix | Delete
# Used internally to defined properties on the
[207] Fix | Delete
# OpenStruct. It does this by using the metaprogramming function
[208] Fix | Delete
# define_singleton_method for both the getter method and the setter method.
[209] Fix | Delete
#
[210] Fix | Delete
def new_ostruct_member!(name) # :nodoc:
[211] Fix | Delete
unless @table.key?(name) || is_method_protected!(name)
[212] Fix | Delete
define_singleton_method!(name) { @table[name] }
[213] Fix | Delete
define_singleton_method!("#{name}=") {|x| @table[name] = x}
[214] Fix | Delete
end
[215] Fix | Delete
end
[216] Fix | Delete
private :new_ostruct_member!
[217] Fix | Delete
[218] Fix | Delete
private def is_method_protected!(name) # :nodoc:
[219] Fix | Delete
if !respond_to?(name, true)
[220] Fix | Delete
false
[221] Fix | Delete
elsif name.end_with?('!')
[222] Fix | Delete
true
[223] Fix | Delete
else
[224] Fix | Delete
method!(name).owner < OpenStruct
[225] Fix | Delete
end
[226] Fix | Delete
end
[227] Fix | Delete
[228] Fix | Delete
def freeze
[229] Fix | Delete
@table.freeze
[230] Fix | Delete
super
[231] Fix | Delete
end
[232] Fix | Delete
[233] Fix | Delete
private def method_missing(mid, *args) # :nodoc:
[234] Fix | Delete
len = args.length
[235] Fix | Delete
if mname = mid[/.*(?==\z)/m]
[236] Fix | Delete
if len != 1
[237] Fix | Delete
raise! ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
[238] Fix | Delete
end
[239] Fix | Delete
set_ostruct_member_value!(mname, args[0])
[240] Fix | Delete
elsif len == 0
[241] Fix | Delete
else
[242] Fix | Delete
begin
[243] Fix | Delete
super
[244] Fix | Delete
rescue NoMethodError => err
[245] Fix | Delete
err.backtrace.shift
[246] Fix | Delete
raise!
[247] Fix | Delete
end
[248] Fix | Delete
end
[249] Fix | Delete
end
[250] Fix | Delete
[251] Fix | Delete
#
[252] Fix | Delete
# :call-seq:
[253] Fix | Delete
# ostruct[name] -> object
[254] Fix | Delete
#
[255] Fix | Delete
# Returns the value of an attribute, or `nil` if there is no such attribute.
[256] Fix | Delete
#
[257] Fix | Delete
# require "ostruct"
[258] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
[259] Fix | Delete
# person[:age] # => 70, same as person.age
[260] Fix | Delete
#
[261] Fix | Delete
def [](name)
[262] Fix | Delete
@table[name.to_sym]
[263] Fix | Delete
end
[264] Fix | Delete
[265] Fix | Delete
#
[266] Fix | Delete
# :call-seq:
[267] Fix | Delete
# ostruct[name] = obj -> obj
[268] Fix | Delete
#
[269] Fix | Delete
# Sets the value of an attribute.
[270] Fix | Delete
#
[271] Fix | Delete
# require "ostruct"
[272] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
[273] Fix | Delete
# person[:age] = 42 # equivalent to person.age = 42
[274] Fix | Delete
# person.age # => 42
[275] Fix | Delete
#
[276] Fix | Delete
def []=(name, value)
[277] Fix | Delete
name = name.to_sym
[278] Fix | Delete
new_ostruct_member!(name)
[279] Fix | Delete
@table[name] = value
[280] Fix | Delete
end
[281] Fix | Delete
alias_method :set_ostruct_member_value!, :[]=
[282] Fix | Delete
private :set_ostruct_member_value!
[283] Fix | Delete
[284] Fix | Delete
# :call-seq:
[285] Fix | Delete
# ostruct.dig(name, *identifiers) -> object
[286] Fix | Delete
#
[287] Fix | Delete
# Finds and returns the object in nested objects
[288] Fix | Delete
# that is specified by +name+ and +identifiers+.
[289] Fix | Delete
# The nested objects may be instances of various classes.
[290] Fix | Delete
# See {Dig Methods}[rdoc-ref:doc/dig_methods.rdoc].
[291] Fix | Delete
#
[292] Fix | Delete
# Examples:
[293] Fix | Delete
# require "ostruct"
[294] Fix | Delete
# address = OpenStruct.new("city" => "Anytown NC", "zip" => 12345)
[295] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "address" => address)
[296] Fix | Delete
# person.dig(:address, "zip") # => 12345
[297] Fix | Delete
# person.dig(:business_address, "zip") # => nil
[298] Fix | Delete
def dig(name, *names)
[299] Fix | Delete
begin
[300] Fix | Delete
name = name.to_sym
[301] Fix | Delete
rescue NoMethodError
[302] Fix | Delete
raise! TypeError, "#{name} is not a symbol nor a string"
[303] Fix | Delete
end
[304] Fix | Delete
@table.dig(name, *names)
[305] Fix | Delete
end
[306] Fix | Delete
[307] Fix | Delete
#
[308] Fix | Delete
# Removes the named field from the object. Returns the value that the field
[309] Fix | Delete
# contained if it was defined.
[310] Fix | Delete
#
[311] Fix | Delete
# require "ostruct"
[312] Fix | Delete
#
[313] Fix | Delete
# person = OpenStruct.new(name: "John", age: 70, pension: 300)
[314] Fix | Delete
#
[315] Fix | Delete
# person.delete_field!("age") # => 70
[316] Fix | Delete
# person # => #<OpenStruct name="John", pension=300>
[317] Fix | Delete
#
[318] Fix | Delete
# Setting the value to +nil+ will not remove the attribute:
[319] Fix | Delete
#
[320] Fix | Delete
# person.pension = nil
[321] Fix | Delete
# person # => #<OpenStruct name="John", pension=nil>
[322] Fix | Delete
#
[323] Fix | Delete
def delete_field(name)
[324] Fix | Delete
sym = name.to_sym
[325] Fix | Delete
begin
[326] Fix | Delete
singleton_class.remove_method(sym, "#{sym}=")
[327] Fix | Delete
rescue NameError
[328] Fix | Delete
end
[329] Fix | Delete
@table.delete(sym) do
[330] Fix | Delete
raise! NameError.new("no field `#{sym}' in #{self}", sym)
[331] Fix | Delete
end
[332] Fix | Delete
end
[333] Fix | Delete
[334] Fix | Delete
InspectKey = :__inspect_key__ # :nodoc:
[335] Fix | Delete
[336] Fix | Delete
#
[337] Fix | Delete
# Returns a string containing a detailed summary of the keys and values.
[338] Fix | Delete
#
[339] Fix | Delete
def inspect
[340] Fix | Delete
ids = (Thread.current[InspectKey] ||= [])
[341] Fix | Delete
if ids.include?(object_id)
[342] Fix | Delete
detail = ' ...'
[343] Fix | Delete
else
[344] Fix | Delete
ids << object_id
[345] Fix | Delete
begin
[346] Fix | Delete
detail = @table.map do |key, value|
[347] Fix | Delete
" #{key}=#{value.inspect}"
[348] Fix | Delete
end.join(',')
[349] Fix | Delete
ensure
[350] Fix | Delete
ids.pop
[351] Fix | Delete
end
[352] Fix | Delete
end
[353] Fix | Delete
['#<', self.class!, detail, '>'].join
[354] Fix | Delete
end
[355] Fix | Delete
alias :to_s :inspect
[356] Fix | Delete
[357] Fix | Delete
attr_reader :table # :nodoc:
[358] Fix | Delete
alias table! table
[359] Fix | Delete
protected :table!
[360] Fix | Delete
[361] Fix | Delete
#
[362] Fix | Delete
# Compares this object and +other+ for equality. An OpenStruct is equal to
[363] Fix | Delete
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
[364] Fix | Delete
# equal.
[365] Fix | Delete
#
[366] Fix | Delete
# require "ostruct"
[367] Fix | Delete
# first_pet = OpenStruct.new("name" => "Rowdy")
[368] Fix | Delete
# second_pet = OpenStruct.new(:name => "Rowdy")
[369] Fix | Delete
# third_pet = OpenStruct.new("name" => "Rowdy", :age => nil)
[370] Fix | Delete
#
[371] Fix | Delete
# first_pet == second_pet # => true
[372] Fix | Delete
# first_pet == third_pet # => false
[373] Fix | Delete
#
[374] Fix | Delete
def ==(other)
[375] Fix | Delete
return false unless other.kind_of?(OpenStruct)
[376] Fix | Delete
@table == other.table!
[377] Fix | Delete
end
[378] Fix | Delete
[379] Fix | Delete
#
[380] Fix | Delete
# Compares this object and +other+ for equality. An OpenStruct is eql? to
[381] Fix | Delete
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
[382] Fix | Delete
# eql?.
[383] Fix | Delete
#
[384] Fix | Delete
def eql?(other)
[385] Fix | Delete
return false unless other.kind_of?(OpenStruct)
[386] Fix | Delete
@table.eql?(other.table!)
[387] Fix | Delete
end
[388] Fix | Delete
[389] Fix | Delete
# Computes a hash code for this OpenStruct.
[390] Fix | Delete
def hash # :nodoc:
[391] Fix | Delete
@table.hash
[392] Fix | Delete
end
[393] Fix | Delete
[394] Fix | Delete
#
[395] Fix | Delete
# Provides marshalling support for use by the YAML library.
[396] Fix | Delete
#
[397] Fix | Delete
def encode_with(coder) # :nodoc:
[398] Fix | Delete
@table.each_pair do |key, value|
[399] Fix | Delete
coder[key.to_s] = value
[400] Fix | Delete
end
[401] Fix | Delete
if @table.size == 1 && @table.key?(:table) # support for legacy format
[402] Fix | Delete
# in the very unlikely case of a single entry called 'table'
[403] Fix | Delete
coder['legacy_support!'] = true # add a bogus second entry
[404] Fix | Delete
end
[405] Fix | Delete
end
[406] Fix | Delete
[407] Fix | Delete
#
[408] Fix | Delete
# Provides marshalling support for use by the YAML library.
[409] Fix | Delete
#
[410] Fix | Delete
def init_with(coder) # :nodoc:
[411] Fix | Delete
h = coder.map
[412] Fix | Delete
if h.size == 1 # support for legacy format
[413] Fix | Delete
key, val = h.first
[414] Fix | Delete
if key == 'table'
[415] Fix | Delete
h = val
[416] Fix | Delete
end
[417] Fix | Delete
end
[418] Fix | Delete
update_to_values!(h)
[419] Fix | Delete
end
[420] Fix | Delete
[421] Fix | Delete
# Make all public methods (builtin or our own) accessible with `!`:
[422] Fix | Delete
instance_methods.each do |method|
[423] Fix | Delete
new_name = "#{method}!"
[424] Fix | Delete
alias_method new_name, method
[425] Fix | Delete
end
[426] Fix | Delete
# Other builtin private methods we use:
[427] Fix | Delete
alias_method :raise!, :raise
[428] Fix | Delete
private :raise!
[429] Fix | Delete
end
[430] Fix | Delete
[431] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function