Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
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
# OpenStruct allows you to create data objects and set arbitrary attributes.
[11] Fix | Delete
# For example:
[12] Fix | Delete
#
[13] Fix | Delete
# require 'ostruct'
[14] Fix | Delete
#
[15] Fix | Delete
# record = OpenStruct.new
[16] Fix | Delete
# record.name = "John Smith"
[17] Fix | Delete
# record.age = 70
[18] Fix | Delete
# record.pension = 300
[19] Fix | Delete
#
[20] Fix | Delete
# puts record.name # -> "John Smith"
[21] Fix | Delete
# puts record.address # -> nil
[22] Fix | Delete
#
[23] Fix | Delete
# It is like a hash with a different way to access the data. In fact, it is
[24] Fix | Delete
# implemented with a hash, and you can initialize it with one.
[25] Fix | Delete
#
[26] Fix | Delete
# hash = { "country" => "Australia", :population => 20_000_000 }
[27] Fix | Delete
# data = OpenStruct.new(hash)
[28] Fix | Delete
#
[29] Fix | Delete
# p data # -> <OpenStruct country="Australia" population=20000000>
[30] Fix | Delete
#
[31] Fix | Delete
class OpenStruct
[32] Fix | Delete
#
[33] Fix | Delete
# Create a new OpenStruct object. The optional +hash+, if given, will
[34] Fix | Delete
# generate attributes and values. For example.
[35] Fix | Delete
#
[36] Fix | Delete
# require 'ostruct'
[37] Fix | Delete
# hash = { "country" => "Australia", :population => 20_000_000 }
[38] Fix | Delete
# data = OpenStruct.new(hash)
[39] Fix | Delete
#
[40] Fix | Delete
# p data # -> <OpenStruct country="Australia" population=20000000>
[41] Fix | Delete
#
[42] Fix | Delete
# By default, the resulting OpenStruct object will have no attributes.
[43] Fix | Delete
#
[44] Fix | Delete
def initialize(hash=nil)
[45] Fix | Delete
@table = {}
[46] Fix | Delete
if hash
[47] Fix | Delete
for k,v in hash
[48] Fix | Delete
@table[k.to_sym] = v
[49] Fix | Delete
new_ostruct_member(k)
[50] Fix | Delete
end
[51] Fix | Delete
end
[52] Fix | Delete
end
[53] Fix | Delete
[54] Fix | Delete
# Duplicate an OpenStruct object members.
[55] Fix | Delete
def initialize_copy(orig)
[56] Fix | Delete
super
[57] Fix | Delete
@table = @table.dup
[58] Fix | Delete
end
[59] Fix | Delete
[60] Fix | Delete
def marshal_dump
[61] Fix | Delete
@table
[62] Fix | Delete
end
[63] Fix | Delete
def marshal_load(x)
[64] Fix | Delete
@table = x
[65] Fix | Delete
@table.each_key{|key| new_ostruct_member(key)}
[66] Fix | Delete
end
[67] Fix | Delete
[68] Fix | Delete
def modifiable
[69] Fix | Delete
if self.frozen?
[70] Fix | Delete
raise TypeError, "can't modify frozen #{self.class}", caller(2)
[71] Fix | Delete
end
[72] Fix | Delete
@table
[73] Fix | Delete
end
[74] Fix | Delete
protected :modifiable
[75] Fix | Delete
[76] Fix | Delete
def new_ostruct_member(name)
[77] Fix | Delete
name = name.to_sym
[78] Fix | Delete
unless self.respond_to?(name)
[79] Fix | Delete
class << self; self; end.class_eval do
[80] Fix | Delete
define_method(name) { @table[name] }
[81] Fix | Delete
define_method("#{name}=") { |x| modifiable[name] = x }
[82] Fix | Delete
end
[83] Fix | Delete
end
[84] Fix | Delete
name
[85] Fix | Delete
end
[86] Fix | Delete
[87] Fix | Delete
def method_missing(mid, *args) # :nodoc:
[88] Fix | Delete
mname = mid.id2name
[89] Fix | Delete
len = args.length
[90] Fix | Delete
if mname.chomp!('=')
[91] Fix | Delete
if len != 1
[92] Fix | Delete
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
[93] Fix | Delete
end
[94] Fix | Delete
modifiable[new_ostruct_member(mname)] = args[0]
[95] Fix | Delete
elsif len == 0
[96] Fix | Delete
@table[mid]
[97] Fix | Delete
else
[98] Fix | Delete
raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
[99] Fix | Delete
end
[100] Fix | Delete
end
[101] Fix | Delete
[102] Fix | Delete
#
[103] Fix | Delete
# Remove the named field from the object.
[104] Fix | Delete
#
[105] Fix | Delete
def delete_field(name)
[106] Fix | Delete
@table.delete name.to_sym
[107] Fix | Delete
end
[108] Fix | Delete
[109] Fix | Delete
InspectKey = :__inspect_key__ # :nodoc:
[110] Fix | Delete
[111] Fix | Delete
#
[112] Fix | Delete
# Returns a string containing a detailed summary of the keys and values.
[113] Fix | Delete
#
[114] Fix | Delete
def inspect
[115] Fix | Delete
str = "#<#{self.class}"
[116] Fix | Delete
[117] Fix | Delete
ids = (Thread.current[InspectKey] ||= [])
[118] Fix | Delete
if ids.include?(object_id)
[119] Fix | Delete
return str << ' ...>'
[120] Fix | Delete
end
[121] Fix | Delete
[122] Fix | Delete
ids << object_id
[123] Fix | Delete
begin
[124] Fix | Delete
first = true
[125] Fix | Delete
for k,v in @table
[126] Fix | Delete
str << "," unless first
[127] Fix | Delete
first = false
[128] Fix | Delete
str << " #{k}=#{v.inspect}"
[129] Fix | Delete
end
[130] Fix | Delete
return str << '>'
[131] Fix | Delete
ensure
[132] Fix | Delete
ids.pop
[133] Fix | Delete
end
[134] Fix | Delete
end
[135] Fix | Delete
alias :to_s :inspect
[136] Fix | Delete
[137] Fix | Delete
attr_reader :table # :nodoc:
[138] Fix | Delete
protected :table
[139] Fix | Delete
[140] Fix | Delete
#
[141] Fix | Delete
# Compare this object and +other+ for equality.
[142] Fix | Delete
#
[143] Fix | Delete
def ==(other)
[144] Fix | Delete
return false unless(other.kind_of?(OpenStruct))
[145] Fix | Delete
return @table == other.table
[146] Fix | Delete
end
[147] Fix | Delete
end
[148] Fix | Delete
[149] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function