Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: ostruct.rb
#
[0] Fix | Delete
# = ostruct.rb: OpenStruct implementation
[1] Fix | Delete
#
[2] Fix | Delete
# Author:: Yukihiro Matsumoto
[3] Fix | Delete
# Documentation:: Gavin Sinclair
[4] Fix | Delete
#
[5] Fix | Delete
# OpenStruct allows the creation of data objects with arbitrary attributes.
[6] Fix | Delete
# See OpenStruct for an example.
[7] Fix | Delete
#
[8] Fix | Delete
[9] Fix | Delete
#
[10] Fix | Delete
# An OpenStruct is a data structure, similar to a Hash, that allows the
[11] Fix | Delete
# definition of arbitrary attributes with their accompanying values. This is
[12] Fix | Delete
# accomplished by using Ruby's metaprogramming to define methods on the class
[13] Fix | Delete
# itself.
[14] Fix | Delete
#
[15] Fix | Delete
# == Examples
[16] Fix | Delete
#
[17] Fix | Delete
# require "ostruct"
[18] Fix | Delete
#
[19] Fix | Delete
# person = OpenStruct.new
[20] Fix | Delete
# person.name = "John Smith"
[21] Fix | Delete
# person.age = 70
[22] Fix | Delete
#
[23] Fix | Delete
# person.name # => "John Smith"
[24] Fix | Delete
# person.age # => 70
[25] Fix | Delete
# person.address # => nil
[26] Fix | Delete
#
[27] Fix | Delete
# An OpenStruct employs a Hash internally to store the attributes and values
[28] Fix | Delete
# and can even be initialized with one:
[29] Fix | Delete
#
[30] Fix | Delete
# australia = OpenStruct.new(:country => "Australia", :capital => "Canberra")
[31] Fix | Delete
# # => #<OpenStruct country="Australia", capital="Canberra">
[32] Fix | Delete
#
[33] Fix | Delete
# Hash keys with spaces or characters that could normally not be used for
[34] Fix | Delete
# method calls (e.g. <code>()[]*</code>) will not be immediately available
[35] Fix | Delete
# on the OpenStruct object as a method for retrieval or assignment, but can
[36] Fix | Delete
# still be reached through the Object#send method.
[37] Fix | Delete
#
[38] Fix | Delete
# measurements = OpenStruct.new("length (in inches)" => 24)
[39] Fix | Delete
# measurements.send("length (in inches)") # => 24
[40] Fix | Delete
#
[41] Fix | Delete
# message = OpenStruct.new(:queued? => true)
[42] Fix | Delete
# message.queued? # => true
[43] Fix | Delete
# message.send("queued?=", false)
[44] Fix | Delete
# message.queued? # => false
[45] Fix | Delete
#
[46] Fix | Delete
# Removing the presence of an attribute requires the execution of the
[47] Fix | Delete
# delete_field method as setting the property value to +nil+ will not
[48] Fix | Delete
# remove the attribute.
[49] Fix | Delete
#
[50] Fix | Delete
# first_pet = OpenStruct.new(:name => "Rowdy", :owner => "John Smith")
[51] Fix | Delete
# second_pet = OpenStruct.new(:name => "Rowdy")
[52] Fix | Delete
#
[53] Fix | Delete
# first_pet.owner = nil
[54] Fix | Delete
# first_pet # => #<OpenStruct name="Rowdy", owner=nil>
[55] Fix | Delete
# first_pet == second_pet # => false
[56] Fix | Delete
#
[57] Fix | Delete
# first_pet.delete_field(:owner)
[58] Fix | Delete
# first_pet # => #<OpenStruct name="Rowdy">
[59] Fix | Delete
# first_pet == second_pet # => true
[60] Fix | Delete
#
[61] Fix | Delete
#
[62] Fix | Delete
# == Implementation
[63] Fix | Delete
#
[64] Fix | Delete
# An OpenStruct utilizes Ruby's method lookup structure to find and define the
[65] Fix | Delete
# necessary methods for properties. This is accomplished through the methods
[66] Fix | Delete
# method_missing and define_singleton_method.
[67] Fix | Delete
#
[68] Fix | Delete
# This should be a consideration if there is a concern about the performance of
[69] Fix | Delete
# the objects that are created, as there is much more overhead in the setting
[70] Fix | Delete
# of these properties compared to using a Hash or a Struct.
[71] Fix | Delete
#
[72] Fix | Delete
class OpenStruct
[73] Fix | Delete
#
[74] Fix | Delete
# Creates a new OpenStruct object. By default, the resulting OpenStruct
[75] Fix | Delete
# object will have no attributes.
[76] Fix | Delete
#
[77] Fix | Delete
# The optional +hash+, if given, will generate attributes and values
[78] Fix | Delete
# (can be a Hash, an OpenStruct or a Struct).
[79] Fix | Delete
# For example:
[80] Fix | Delete
#
[81] Fix | Delete
# require "ostruct"
[82] Fix | Delete
# hash = { "country" => "Australia", :capital => "Canberra" }
[83] Fix | Delete
# data = OpenStruct.new(hash)
[84] Fix | Delete
#
[85] Fix | Delete
# data # => #<OpenStruct country="Australia", capital="Canberra">
[86] Fix | Delete
#
[87] Fix | Delete
def initialize(hash=nil)
[88] Fix | Delete
@table = {}
[89] Fix | Delete
if hash
[90] Fix | Delete
hash.each_pair do |k, v|
[91] Fix | Delete
k = k.to_sym
[92] Fix | Delete
@table[k] = v
[93] Fix | Delete
new_ostruct_member(k)
[94] Fix | Delete
end
[95] Fix | Delete
end
[96] Fix | Delete
end
[97] Fix | Delete
[98] Fix | Delete
# Duplicates an OpenStruct object's Hash table.
[99] Fix | Delete
def initialize_copy(orig) # :nodoc:
[100] Fix | Delete
super
[101] Fix | Delete
@table = @table.dup
[102] Fix | Delete
@table.each_key{|key| new_ostruct_member(key)}
[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
@table.each_key{|key| new_ostruct_member(key)}
[148] Fix | Delete
end
[149] Fix | Delete
[150] Fix | Delete
#
[151] Fix | Delete
# Used internally to check if the OpenStruct is able to be
[152] Fix | Delete
# modified before granting access to the internal Hash table to be modified.
[153] Fix | Delete
#
[154] Fix | Delete
def modifiable
[155] Fix | Delete
begin
[156] Fix | Delete
@modifiable = true
[157] Fix | Delete
rescue
[158] Fix | Delete
raise RuntimeError, "can't modify frozen #{self.class}", caller(3)
[159] Fix | Delete
end
[160] Fix | Delete
@table
[161] Fix | Delete
end
[162] Fix | Delete
protected :modifiable
[163] Fix | Delete
[164] Fix | Delete
#
[165] Fix | Delete
# Used internally to defined properties on the
[166] Fix | Delete
# OpenStruct. It does this by using the metaprogramming function
[167] Fix | Delete
# define_singleton_method for both the getter method and the setter method.
[168] Fix | Delete
#
[169] Fix | Delete
def new_ostruct_member(name)
[170] Fix | Delete
name = name.to_sym
[171] Fix | Delete
unless respond_to?(name)
[172] Fix | Delete
define_singleton_method(name) { @table[name] }
[173] Fix | Delete
define_singleton_method("#{name}=") { |x| modifiable[name] = x }
[174] Fix | Delete
end
[175] Fix | Delete
name
[176] Fix | Delete
end
[177] Fix | Delete
protected :new_ostruct_member
[178] Fix | Delete
[179] Fix | Delete
def method_missing(mid, *args) # :nodoc:
[180] Fix | Delete
mname = mid.id2name
[181] Fix | Delete
len = args.length
[182] Fix | Delete
if mname.chomp!('=')
[183] Fix | Delete
if len != 1
[184] Fix | Delete
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
[185] Fix | Delete
end
[186] Fix | Delete
modifiable[new_ostruct_member(mname)] = args[0]
[187] Fix | Delete
elsif len == 0
[188] Fix | Delete
@table[mid]
[189] Fix | Delete
else
[190] Fix | Delete
err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args
[191] Fix | Delete
err.set_backtrace caller(1)
[192] Fix | Delete
raise err
[193] Fix | Delete
end
[194] Fix | Delete
end
[195] Fix | Delete
[196] Fix | Delete
#
[197] Fix | Delete
# :call-seq:
[198] Fix | Delete
# ostruct[name] -> object
[199] Fix | Delete
#
[200] Fix | Delete
# Returns the value of an attribute.
[201] Fix | Delete
#
[202] Fix | Delete
# require "ostruct"
[203] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
[204] Fix | Delete
# person[:age] # => 70, same as person.age
[205] Fix | Delete
#
[206] Fix | Delete
def [](name)
[207] Fix | Delete
@table[name.to_sym]
[208] Fix | Delete
end
[209] Fix | Delete
[210] Fix | Delete
#
[211] Fix | Delete
# :call-seq:
[212] Fix | Delete
# ostruct[name] = obj -> obj
[213] Fix | Delete
#
[214] Fix | Delete
# Sets the value of an attribute.
[215] Fix | Delete
#
[216] Fix | Delete
# require "ostruct"
[217] Fix | Delete
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
[218] Fix | Delete
# person[:age] = 42 # equivalent to person.age = 42
[219] Fix | Delete
# person.age # => 42
[220] Fix | Delete
#
[221] Fix | Delete
def []=(name, value)
[222] Fix | Delete
modifiable[new_ostruct_member(name)] = value
[223] Fix | Delete
end
[224] Fix | Delete
[225] Fix | Delete
#
[226] Fix | Delete
# Removes the named field from the object. Returns the value that the field
[227] Fix | Delete
# contained if it was defined.
[228] Fix | Delete
#
[229] Fix | Delete
# require "ostruct"
[230] Fix | Delete
#
[231] Fix | Delete
# person = OpenStruct.new(name: "John", age: 70, pension: 300)
[232] Fix | Delete
#
[233] Fix | Delete
# person.delete_field("age") # => 70
[234] Fix | Delete
# person # => #<OpenStruct name="John", pension=300>
[235] Fix | Delete
#
[236] Fix | Delete
# Setting the value to +nil+ will not remove the attribute:
[237] Fix | Delete
#
[238] Fix | Delete
# person.pension = nil
[239] Fix | Delete
# person # => #<OpenStruct name="John", pension=nil>
[240] Fix | Delete
#
[241] Fix | Delete
def delete_field(name)
[242] Fix | Delete
sym = name.to_sym
[243] Fix | Delete
singleton_class.__send__(:remove_method, sym, "#{sym}=")
[244] Fix | Delete
@table.delete sym
[245] Fix | Delete
end
[246] Fix | Delete
[247] Fix | Delete
InspectKey = :__inspect_key__ # :nodoc:
[248] Fix | Delete
[249] Fix | Delete
#
[250] Fix | Delete
# Returns a string containing a detailed summary of the keys and values.
[251] Fix | Delete
#
[252] Fix | Delete
def inspect
[253] Fix | Delete
str = "#<#{self.class}"
[254] Fix | Delete
[255] Fix | Delete
ids = (Thread.current[InspectKey] ||= [])
[256] Fix | Delete
if ids.include?(object_id)
[257] Fix | Delete
return str << ' ...>'
[258] Fix | Delete
end
[259] Fix | Delete
[260] Fix | Delete
ids << object_id
[261] Fix | Delete
begin
[262] Fix | Delete
first = true
[263] Fix | Delete
for k,v in @table
[264] Fix | Delete
str << "," unless first
[265] Fix | Delete
first = false
[266] Fix | Delete
str << " #{k}=#{v.inspect}"
[267] Fix | Delete
end
[268] Fix | Delete
return str << '>'
[269] Fix | Delete
ensure
[270] Fix | Delete
ids.pop
[271] Fix | Delete
end
[272] Fix | Delete
end
[273] Fix | Delete
alias :to_s :inspect
[274] Fix | Delete
[275] Fix | Delete
attr_reader :table # :nodoc:
[276] Fix | Delete
protected :table
[277] Fix | Delete
[278] Fix | Delete
#
[279] Fix | Delete
# Compares this object and +other+ for equality. An OpenStruct is equal to
[280] Fix | Delete
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
[281] Fix | Delete
# equal.
[282] Fix | Delete
#
[283] Fix | Delete
# require "ostruct"
[284] Fix | Delete
# first_pet = OpenStruct.new("name" => "Rowdy")
[285] Fix | Delete
# second_pet = OpenStruct.new(:name => "Rowdy")
[286] Fix | Delete
# third_pet = OpenStruct.new("name" => "Rowdy", :age => nil)
[287] Fix | Delete
#
[288] Fix | Delete
# first_pet == second_pet # => true
[289] Fix | Delete
# first_pet == third_pet # => false
[290] Fix | Delete
#
[291] Fix | Delete
def ==(other)
[292] Fix | Delete
return false unless other.kind_of?(OpenStruct)
[293] Fix | Delete
@table == other.table
[294] Fix | Delete
end
[295] Fix | Delete
[296] Fix | Delete
#
[297] Fix | Delete
# Compares this object and +other+ for equality. An OpenStruct is eql? to
[298] Fix | Delete
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
[299] Fix | Delete
# eql?.
[300] Fix | Delete
#
[301] Fix | Delete
def eql?(other)
[302] Fix | Delete
return false unless other.kind_of?(OpenStruct)
[303] Fix | Delete
@table.eql?(other.table)
[304] Fix | Delete
end
[305] Fix | Delete
[306] Fix | Delete
# Computes a hash code for this OpenStruct.
[307] Fix | Delete
# Two OpenStruct objects with the same content will have the same hash code
[308] Fix | Delete
# (and will compare using #eql?).
[309] Fix | Delete
#
[310] Fix | Delete
# See also Object#hash.
[311] Fix | Delete
def hash
[312] Fix | Delete
@table.hash
[313] Fix | Delete
end
[314] Fix | Delete
end
[315] Fix | Delete
[316] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function