Class: PersistentOpenStruct
- Inherits:
-
Object
- Object
- PersistentOpenStruct
show all
- Defined in:
- lib/persistent_open_struct.rb,
lib/persistent_open_struct/version.rb
Constant Summary
collapse
- InspectKey =
:__inspect_key__
- VERSION =
"0.0.3"
Instance Method Summary
collapse
Constructor Details
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/persistent_open_struct.rb', line 23
def initialize(hash=nil)
@table = {}
if hash
hash.each_pair do |k, v|
k = k.to_sym
@table[k] = v
new_ostruct_member(k)
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mid, *args) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/persistent_open_struct.rb', line 68
def method_missing(mid, *args) mname = mid.id2name
len = args.length
if mname.chomp!('=')
if len != 1
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
end
modifiable[new_ostruct_member(mname)] = args[0]
elsif len == 0
@table[mid]
else
err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args
err.set_backtrace caller(1)
raise err
end
end
|
Instance Method Details
#==(other) ⇒ Object
121
122
123
124
|
# File 'lib/persistent_open_struct.rb', line 121
def ==(other)
return false unless other.class == self.class
@table == other.table
end
|
#[](name) ⇒ Object
85
86
87
|
# File 'lib/persistent_open_struct.rb', line 85
def [](name)
@table[name.to_sym]
end
|
#[]=(name, value) ⇒ Object
89
90
91
|
# File 'lib/persistent_open_struct.rb', line 89
def []=(name, value)
modifiable[new_ostruct_member(name)] = value
end
|
#delete_field(name) ⇒ Object
16
17
18
|
# File 'lib/persistent_open_struct.rb', line 16
def delete_field(name)
@table.delete(name)
end
|
#each_pair ⇒ Object
44
45
46
47
|
# File 'lib/persistent_open_struct.rb', line 44
def each_pair
return to_enum(__method__) { @table.size } unless block_given?
@table.each_pair{|p| yield p}
end
|
#eql?(other) ⇒ Boolean
126
127
128
129
|
# File 'lib/persistent_open_struct.rb', line 126
def eql?(other)
return false unless other.class == self.class
@table.eql?(other.table)
end
|
#hash ⇒ Object
131
132
133
|
# File 'lib/persistent_open_struct.rb', line 131
def hash
@table.hash
end
|
#initialize_copy(orig) ⇒ Object
34
35
36
37
38
|
# File 'lib/persistent_open_struct.rb', line 34
def initialize_copy(orig)
super
@table = @table.dup
@table.each_key{|key| new_ostruct_member(key)}
end
|
#inspect ⇒ Object
Also known as:
to_s
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/persistent_open_struct.rb', line 95
def inspect
str = "#<#{self.class}"
ids = (Thread.current[InspectKey] ||= [])
if ids.include?(object_id)
return str << ' ...>'
end
ids << object_id
begin
first = true
for k,v in @table
str << "," unless first
first = false
str << " #{k}=#{v.inspect}"
end
return str << '>'
ensure
ids.pop
end
end
|
#marshal_dump ⇒ Object
49
50
51
|
# File 'lib/persistent_open_struct.rb', line 49
def marshal_dump
@table
end
|
#marshal_load(x) ⇒ Object
53
54
55
56
|
# File 'lib/persistent_open_struct.rb', line 53
def marshal_load(x)
@table = x
@table.each_key{|key| new_ostruct_member(key)}
end
|
#to_h ⇒ Object
40
41
42
|
# File 'lib/persistent_open_struct.rb', line 40
def to_h
@table.dup
end
|