Class: FastOpenStruct
- Inherits:
-
Object
show all
- Defined in:
- lib/fast_open_struct.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of FastOpenStruct.
31
32
33
34
35
|
# File 'lib/fast_open_struct.rb', line 31
def initialize(table = {})
table.each_pair do |k, v|
instance_variable_set "@#{k}", v
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
106
107
108
109
110
111
112
113
114
|
# File 'lib/fast_open_struct.rb', line 106
def method_missing(sym, *args)
if sym[-1] == "=" and args.size == 1
self[sym[0...-1]] = args[0]
elsif args.size == 0 and instance_variable_defined?("@#{sym}")
self[sym]
else
super
end
end
|
Class Method Details
.new(table = {}) ⇒ Object
22
23
24
25
26
27
28
29
|
# File 'lib/fast_open_struct.rb', line 22
def self.new(table = {})
keys = table.each_pair.map { |key, _| key.intern }.sort
if cached = @cache[keys]
cached.new table
else
(@cache[keys] = __create_class(keys)).new table
end
end
|
Instance Method Details
#==(other) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/fast_open_struct.rb', line 62
def ==(other)
return false unless other.is_a? FastOpenStruct
ivars = instance_variables
return false if (ivars - other.instance_variables).any?
ivars.all? { |ivar| instance_variable_get(ivar) == other.instance_variable_get(ivar) }
end
|
#[](name) ⇒ Object
37
38
39
|
# File 'lib/fast_open_struct.rb', line 37
def [](name)
instance_variable_get "@#{name}"
end
|
#[]=(name, value) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/fast_open_struct.rb', line 41
def []=(name, value)
instance_variable_set "@#{name}", value
value
rescue RuntimeError
raise TypeError, "can't modify frozen #{__apparent_class__}"
end
|
#delete_field(name) ⇒ Object
48
49
50
51
52
|
# File 'lib/fast_open_struct.rb', line 48
def delete_field(name)
value = self[name]
remove_instance_variable "@#{name}"
value
end
|
#each_pair ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/fast_open_struct.rb', line 54
def each_pair
return to_enum(__method__) unless block_given?
instance_variables.each do |ivar|
yield ivar[1..-1].intern, instance_variable_get(ivar)
end
self
end
|
#eql?(other) ⇒ Boolean
69
70
71
72
73
74
|
# File 'lib/fast_open_struct.rb', line 69
def eql?(other)
return false unless other.is_a? FastOpenStruct
ivars = instance_variables
return false if (ivars - other.instance_variables).any?
ivars.all? { |ivar| instance_variable_get(ivar).eql? other.instance_variable_get(ivar) }
end
|
#hash ⇒ Object
98
99
100
101
102
103
104
|
# File 'lib/fast_open_struct.rb', line 98
def hash
hash = 0x1337
each_pair do |key, value|
hash ^= key.hash ^ value.hash
end
hash
end
|
#inspect ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/fast_open_struct.rb', line 76
def inspect
str = "#<#{__apparent_class__}"
ids = (Thread.current[:__fast_open_struct_inspect_key__] ||= Set.new)
return str << " ...>" if ids.include? object_id
ids << object_id
begin
first = true
instance_variables.each do |ivar|
str << "," unless first
first = false
str << " #{ivar[1..-1]}=#{instance_variable_get(ivar).inspect}"
end
str << ">"
ensure
ids.delete object_id
end
end
|
#respond_to?(sym) ⇒ Boolean
116
117
118
119
120
121
122
123
124
|
# File 'lib/fast_open_struct.rb', line 116
def respond_to?(sym)
if sym[-1] == "="
respond_to?(sym[0...-1])
elsif instance_variable_defined?("@#{sym}")
true
else
super
end
end
|
#to_h ⇒ Object
94
95
96
|
# File 'lib/fast_open_struct.rb', line 94
def to_h
Hash[each_pair.to_a]
end
|