Class: OpenObject
- Inherits:
-
Hash
show all
- Defined in:
- lib/ruby_ext/open_object.rb
Constant Summary
collapse
- PUBLIC_METHODS =
%w(as send each size is_a? clone dup empty? blank? present? merge merge! stringify_keys stringify_keys! symbolize_keys symbolize_keys! to_query)
- PUBLIC_METHODS_RE =
/(^__|^object_)/
Instance Method Summary
collapse
Methods inherited from Hash
#oo_eql, #subset
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, arg = nil, &block) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/ruby_ext/open_object.rb', line 108
def method_missing m, arg = nil, &block
m = m.to_s
type = m[-1,1]
if type == '='
self[m[0..-2]] = arg
elsif type == '!'
self[m[0..-2]]
elsif type == '?'
!self[m[0..-2]].blank?
else
self[m]
end
end
|
Instance Method Details
#==(other) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/ruby_ext/open_object.rb', line 58
def == other
return false unless other.respond_to?(:each) and other.respond_to?(:size) and other.respond_to?(:[]) and self.size == other.size
other.each do |k, v|
return false unless self[k] == v
end
true
end
|
70
71
72
|
# File 'lib/ruby_ext/open_object.rb', line 70
def [] k
super k.to_s
end
|
#[]=(k, v) ⇒ Object
66
67
68
|
# File 'lib/ruby_ext/open_object.rb', line 66
def []= k, v
super k.to_s, v
end
|
#delete(key) ⇒ Object
54
55
56
|
# File 'lib/ruby_ext/open_object.rb', line 54
def delete key
super key.to_s
end
|
#each(&block) ⇒ Object
34
|
# File 'lib/ruby_ext/open_object.rb', line 34
def each █ super(&block) end
|
#include?(k) ⇒ Boolean
74
75
76
|
# File 'lib/ruby_ext/open_object.rb', line 74
def include? k
super k.to_s
end
|
10
11
12
|
# File 'lib/ruby_ext/open_object.rb', line 10
def inspect
"#<#{self.class}:#{self.object_id} #{super}>"
end
|
#merge(other) ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/ruby_ext/open_object.rb', line 36
def merge other
d = dup
other.each{|k, v| d[k] = v}
d
end
|
#merge!(other) ⇒ Object
45
46
47
|
# File 'lib/ruby_ext/open_object.rb', line 45
def merge! other
other.each{|k, v| self[k] = v}
end
|
#respond_to?(m) ⇒ Boolean
103
104
105
|
# File 'lib/ruby_ext/open_object.rb', line 103
def respond_to? m
true
end
|
hack to works well with RSpec
100
|
# File 'lib/ruby_ext/open_object.rb', line 100
def should; super end
|
#should_not ⇒ Object
101
|
# File 'lib/ruby_ext/open_object.rb', line 101
def should_not; super end
|
14
|
# File 'lib/ruby_ext/open_object.rb', line 14
def to_a; super end
|
#to_hash(deep = false) ⇒ Object
Also known as:
to_h
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/ruby_ext/open_object.rb', line 78
def to_hash deep = false
unless deep
{}.update(self)
else
h = {}
each do |k, v|
if v.is_a? OpenObject
h[k] = v.to_hash(true)
else
h[k] = v
end
end
h
end
end
|
#to_json(*args) ⇒ Object
95
96
97
|
# File 'lib/ruby_ext/open_object.rb', line 95
def to_json *args
to_hash.to_json *args
end
|
#to_openobject(deep = false) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/ruby_ext/open_object.rb', line 18
def to_openobject deep = false
unless deep
self
else
r = OpenObject.new
each do |k, v|
r[k] = if v.is_a? Hash
v.to_openobject
else
v
end
end
r
end
end
|
16
|
# File 'lib/ruby_ext/open_object.rb', line 16
def to_proc; super end
|
#update(other) ⇒ Object
49
50
51
52
|
# File 'lib/ruby_ext/open_object.rb', line 49
def update other
other.to_hash.each{|k,v| self[k.to_s] = v}
self
end
|