Class: OpenObject
- Inherits:
-
Hash
show all
- Defined in:
- lib/ruby_ext/core/open_object.rb
Constant Summary
collapse
- PUBLIC_METHODS =
%w(
as send each each_pair size is_a? clone dup empty? blank? present? merge merge! stringify_keys stringify_keys! symbolize_keys symbolize_keys! to_query
).collect{|m| m.to_sym}
- PUBLIC_METHODS_RE =
/(^__|^object_|^must|^stub)/
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Hash
#oo_eql, #subset, #validate_options!
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, arg = nil, &block) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/ruby_ext/core/open_object.rb', line 129
def method_missing m, arg = nil, &block
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
|
Class Method Details
.initialize_from(hash, deep = false) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/ruby_ext/core/open_object.rb', line 112
def self.initialize_from hash, deep = false
unless deep
::OpenObject.new.update hash
else
r = ::OpenObject.new
hash.each do |k, v|
r[k] = if v.is_a? Hash
v.to_openobject
else
v
end
end
r
end
end
|
Instance Method Details
#==(other) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/ruby_ext/core/open_object.rb', line 63
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
|
75
76
77
|
# File 'lib/ruby_ext/core/open_object.rb', line 75
def [] k
super k.to_sym
end
|
#[]=(k, v) ⇒ Object
71
72
73
|
# File 'lib/ruby_ext/core/open_object.rb', line 71
def []= k, v
super k.to_sym, v
end
|
#delete(key) ⇒ Object
59
60
61
|
# File 'lib/ruby_ext/core/open_object.rb', line 59
def delete key
super key.to_sym
end
|
#each(&block) ⇒ Object
39
|
# File 'lib/ruby_ext/core/open_object.rb', line 39
def each █ super(&block) end
|
#include?(k) ⇒ Boolean
79
80
81
|
# File 'lib/ruby_ext/core/open_object.rb', line 79
def include? k
super k.to_sym
end
|
13
14
15
16
|
# File 'lib/ruby_ext/core/open_object.rb', line 13
def inspect
"<#{super}>"
end
|
#merge(other) ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'lib/ruby_ext/core/open_object.rb', line 41
def merge other
d = dup
other.each{|k, v| d[k] = v}
d
end
|
#merge!(other) ⇒ Object
50
51
52
|
# File 'lib/ruby_ext/core/open_object.rb', line 50
def merge! other
other.each{|k, v| self[k] = v}
end
|
#respond_to?(m) ⇒ Boolean
108
109
110
|
# File 'lib/ruby_ext/core/open_object.rb', line 108
def respond_to? m
true
end
|
hack to works well with RSpec
105
|
# File 'lib/ruby_ext/core/open_object.rb', line 105
def should; super end
|
#should_not ⇒ Object
106
|
# File 'lib/ruby_ext/core/open_object.rb', line 106
def should_not; super end
|
18
|
# File 'lib/ruby_ext/core/open_object.rb', line 18
def to_a; super end
|
#to_hash(deep = false) ⇒ Object
Also known as:
to_h
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/ruby_ext/core/open_object.rb', line 83
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
100
101
102
|
# File 'lib/ruby_ext/core/open_object.rb', line 100
def to_json *args
to_hash.to_json *args
end
|
#to_openobject(deep = false) ⇒ Object
Also known as:
to_oo
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/ruby_ext/core/open_object.rb', line 22
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
|
20
|
# File 'lib/ruby_ext/core/open_object.rb', line 20
def to_proc; super end
|
#update(other) ⇒ Object
54
55
56
57
|
# File 'lib/ruby_ext/core/open_object.rb', line 54
def update other
other.to_hash.each{|k,v| self[k.to_sym] = v}
self
end
|