Class: JsonParse
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/json_parse.rb,
lib/json_parse/version.rb
Constant Summary
collapse
- VERSION =
"0.1.0"
Instance Method Summary
collapse
-
#[](index) ⇒ Object
-
#_check_for_keyword(keyword) ⇒ Object
-
#_parse_json(jsonString) ⇒ Hash
-
#_symbolize(h) ⇒ Object
-
#_value(key) ⇒ Object
-
#add(key, value) ⇒ Object
-
#contains_key(key = nil) ⇒ Object
-
#count ⇒ Object
-
#count! ⇒ Object
-
#each(&block) ⇒ Object
-
#each!(&block) ⇒ Object
-
#get_by_key(symbol) ⇒ JsonParse
-
#hash ⇒ Object
-
#hash! ⇒ Object
-
#initialize(val, rootkey = nil, throwmissing = false) ⇒ JsonParse
constructor
, jsonstring = ‘{“name”:“Josh Kramer”, “age”:60, “children”: [{“name”: “jack”, “age”: 21,“sally”,“age”:14], “pets”: [“ruby”,“buddy”,“fluffy”], “address”: : “Bainbridge Island”, “state” : “WA”}}’).
-
#json_string ⇒ Object
-
#json_string! ⇒ Object
-
#keys ⇒ Object
-
#keys! ⇒ Object
-
#length ⇒ Object
-
#length! ⇒ Object
-
#method_missing(m, *args) ⇒ Object
-
#rootkey ⇒ Object
-
#rootkey! ⇒ Object
-
#source ⇒ Object
-
#source! ⇒ Object
Constructor Details
#initialize(val, rootkey = nil, throwmissing = false) ⇒ JsonParse
, jsonstring = ‘{“name”:“Josh Kramer”, “age”:60, “children”: [{“name”: “jack”, “age”: 21,“sally”,“age”:14], “pets”: [“ruby”,“buddy”,“fluffy”], “address”: : “Bainbridge Island”, “state” : “WA”}}’)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/json_parse.rb', line 80
def initialize(val, rootkey = nil, throwmissing = false) @source = val
if val.nil?
@h = {}
@rootkey = nil
return
end
@throwmissing = throwmissing
@rootkey = rootkey
if val.is_a?(JsonParse)
@h = val.hash
@rootkey = val.rootkey
end
if val.is_a?(Array)
retval = []
@rootkey = rootkey
val.each do |item|
if item.is_a?(Hash) || item.is_a?(Array)
retval.push(JsonParse.new(item))
else
retval.push(item);
end
end
@h = retval
end
if val.is_a?(Hash)
@h = val.symbolize_keys!
_discard_root end
if val.is_a?(String) _load(val)
@jsonstring = val
end
if @h.nil?
raise "an unusable object was provided to the initializer"
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/json_parse.rb', line 13
def method_missing(m, *args)
a = args
if !(self.keys!.include?(m))
if @throwmissing
super
else
return nil
end
end
self._value(m)
end
|
Instance Method Details
#[](index) ⇒ Object
158
159
160
161
162
163
164
|
# File 'lib/json_parse.rb', line 158
def [](index)
if @h.is_a?(Array)
return @h[index]
end
nil
end
|
#_check_for_keyword(keyword) ⇒ Object
202
203
204
205
206
207
208
|
# File 'lib/json_parse.rb', line 202
def _check_for_keyword(keyword)
mykeys = []
if @h.is_a?(Hash)
mykeys = @h.keys
end
mykeys.include?(keyword.to_sym)
end
|
#_parse_json(jsonString) ⇒ Hash
213
214
215
|
# File 'lib/json_parse.rb', line 213
def _parse_json(jsonString)
v = ActiveSupport::JSON.decode(jsonString)
end
|
#_symbolize(h) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/json_parse.rb', line 186
def _symbolize(h)
if self._check_for_keyword("_symbolize")
return _value("_symbolize")
end
h.symbolize_keys!
h.each do |k, v|
if v.is_a?(Hash)
self._symbolize(v)
end
end
end
|
#_value(key) ⇒ Object
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/json_parse.rb', line 217
def _value(key)
if @h.is_a?(Array)
return @h[key]
end
if @h.key?(key.to_sym)
if @h[key].is_a?(Hash) || @h[key].is_a?(Array)
return JsonParse.new(@h[key], key)
else
retval = @h[key]
return retval
end
end
end
|
#add(key, value) ⇒ Object
76
77
78
|
# File 'lib/json_parse.rb', line 76
def add(key, value)
@h[key.to_sym] = value
end
|
#contains_key(key = nil) ⇒ Object
148
149
150
151
152
153
|
# File 'lib/json_parse.rb', line 148
def contains_key(key = nil)
if key == nil && self._check_for_keyword("contains_key")
return _value(:contains_key)
end
@h.keys.include?(key)
end
|
#count ⇒ Object
169
170
171
172
173
174
|
# File 'lib/json_parse.rb', line 169
def count
if self._check_for_keyword("hash")
return _value(:hash)
end
_count
end
|
#count! ⇒ Object
166
167
168
|
# File 'lib/json_parse.rb', line 166
def count!
_count
end
|
#each(&block) ⇒ Object
33
34
35
36
37
38
|
# File 'lib/json_parse.rb', line 33
def each(&block)
if self._check_for_keyword("each")
return _value(:each)
end
_each(&block)
end
|
#each!(&block) ⇒ Object
40
41
42
|
# File 'lib/json_parse.rb', line 40
def each!(&block)
_each(&block)
end
|
#get_by_key(symbol) ⇒ JsonParse
29
30
31
|
# File 'lib/json_parse.rb', line 29
def get_by_key(symbol)
method_missing(symbol.to_sym, nil)
end
|
#hash ⇒ Object
179
180
181
182
183
184
|
# File 'lib/json_parse.rb', line 179
def hash
if self._check_for_keyword("hash")
return _value(:hash)
end
_hash
end
|
#hash! ⇒ Object
176
177
178
|
# File 'lib/json_parse.rb', line 176
def hash!
_hash
end
|
#json_string ⇒ Object
135
136
137
138
139
140
|
# File 'lib/json_parse.rb', line 135
def json_string
if self._check_for_keyword("json_string")
return _value(:json_string)
end
@h.to_json
end
|
#json_string! ⇒ Object
142
143
144
|
# File 'lib/json_parse.rb', line 142
def json_string!
_json_string
end
|
#keys ⇒ Object
59
60
61
62
63
64
|
# File 'lib/json_parse.rb', line 59
def keys
if self._check_for_keyword("keys")
return self._value(:keys)
end
_keys
end
|
#keys! ⇒ Object
55
56
57
|
# File 'lib/json_parse.rb', line 55
def keys!
_keys
end
|
#length ⇒ Object
44
45
46
47
48
49
|
# File 'lib/json_parse.rb', line 44
def length
if self._check_for_keyword("length")
return _value(:length)
end
@h.length
end
|
#length! ⇒ Object
51
52
53
|
# File 'lib/json_parse.rb', line 51
def length!
return _length
end
|
#rootkey ⇒ Object
124
125
126
127
128
129
|
# File 'lib/json_parse.rb', line 124
def rootkey
if self._check_for_keyword("rootkey")
return _value(:rootkey)
end
_rootkey
end
|
#rootkey! ⇒ Object
130
131
132
|
# File 'lib/json_parse.rb', line 130
def rootkey!
_rootkey
end
|
#source ⇒ Object
69
70
71
72
73
74
|
# File 'lib/json_parse.rb', line 69
def source
if self._check_for_keyword("keys")
return self._value(:keys)
end
_source
end
|
#source! ⇒ Object
66
67
68
|
# File 'lib/json_parse.rb', line 66
def source!
_source
end
|