attribute_struct 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/attribute_struct/attribute_struct.rb +76 -73
- data/lib/attribute_struct/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 902bf8b18675d998b37c3543ee77d343d7c37f8f
|
4
|
+
data.tar.gz: 29bf97732d4f10a3797a394e0fcc9bbcca228b35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c2f5243ac8e3897820f3e9ed286ab4eca0c1b926c0d10fc05e8302f39f289220a81f5b76c4bc92b45c81f7c231026b286f045d759bfc4c903f5a5676c3c9f98
|
7
|
+
data.tar.gz: 42686aaa093b860aa884c8e1118ebe2841f6367ac96520ad2f966a851007b8c7bda03d7377dfc55ecd76197300e31bd45ec59bdff8f49392d817bfc377d6fc94
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
# v0.3.2
|
2
|
+
* Update variable names within method_missing to prevent data collisions
|
3
|
+
* Use constant for internal nil value
|
4
|
+
* Check for internal nil value on set helper
|
5
|
+
|
1
6
|
# v0.3.0
|
2
7
|
* Only allow forced key processing when camel casing is enabled
|
3
8
|
* Refactored dump implementation to better handle deeply nested structs
|
@@ -82,6 +82,9 @@ class AttributeStruct < BasicObject
|
|
82
82
|
|
83
83
|
end
|
84
84
|
|
85
|
+
# value used to identify unset value
|
86
|
+
UNSET_VALUE = :__unset__
|
87
|
+
|
85
88
|
# Specialized array for collapsing values
|
86
89
|
class CollapseArray < ::Array; end
|
87
90
|
|
@@ -177,8 +180,8 @@ class AttributeStruct < BasicObject
|
|
177
180
|
# @param val [Object]
|
178
181
|
# @yield block to execute within context
|
179
182
|
# @return [Object]
|
180
|
-
def _set(key, val=
|
181
|
-
if(val)
|
183
|
+
def _set(key, val=UNSET_VALUE, &block)
|
184
|
+
if(val != UNSET_VALUE)
|
182
185
|
self.method_missing(key, val, &block)
|
183
186
|
else
|
184
187
|
self.method_missing(key, &block)
|
@@ -193,109 +196,109 @@ class AttributeStruct < BasicObject
|
|
193
196
|
# @yield provided block
|
194
197
|
# @return [Object] existing value or newly set value
|
195
198
|
# @note Dragons and unicorns all over in here
|
196
|
-
def method_missing(
|
197
|
-
if((
|
198
|
-
|
199
|
-
|
199
|
+
def method_missing(_sym, *_args, &_block)
|
200
|
+
if((_s = _sym.to_s).end_with?('='))
|
201
|
+
_s.slice!(-1, _s.length)
|
202
|
+
_sym = _s
|
200
203
|
end
|
201
|
-
|
202
|
-
if(!
|
203
|
-
if(
|
204
|
-
|
205
|
-
if(_state(:value_collapse) && !
|
206
|
-
|
207
|
-
|
204
|
+
_sym = _process_key(_sym)
|
205
|
+
if(!_args.empty? || _block)
|
206
|
+
if(_args.empty? && _block)
|
207
|
+
_base = @table.fetch(_sym, UNSET_VALUE)
|
208
|
+
if(_state(:value_collapse) && !_base.is_a?(self.class!))
|
209
|
+
_orig = _base
|
210
|
+
_base = _klass_new
|
208
211
|
else
|
209
|
-
unless(
|
210
|
-
|
212
|
+
unless(_base.is_a?(self.class!))
|
213
|
+
_base = _klass_new
|
211
214
|
end
|
212
215
|
end
|
213
|
-
@table[
|
214
|
-
if(
|
215
|
-
|
216
|
+
@table[_sym] = _base
|
217
|
+
if(_block.arity == 0)
|
218
|
+
_base.instance_exec(&_block)
|
216
219
|
else
|
217
|
-
|
220
|
+
_base.instance_exec(_base, &_block)
|
218
221
|
end
|
219
|
-
if(
|
220
|
-
@table[
|
222
|
+
if(_orig.is_a?(::NilClass))
|
223
|
+
@table[_sym] = _base
|
221
224
|
else
|
222
|
-
if(
|
223
|
-
@table[
|
225
|
+
if(_orig == UNSET_VALUE)
|
226
|
+
@table[_sym] = _base
|
224
227
|
else
|
225
|
-
unless(
|
226
|
-
|
228
|
+
unless(_orig.is_a?(CollapseArray))
|
229
|
+
_orig = CollapseArray.new.push(_orig)
|
227
230
|
end
|
228
|
-
|
229
|
-
@table[
|
231
|
+
_orig << _base
|
232
|
+
@table[_sym] = _orig
|
230
233
|
end
|
231
234
|
end
|
232
|
-
elsif(!
|
233
|
-
|
234
|
-
@table[
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
unless(
|
239
|
-
|
240
|
-
|
241
|
-
|
235
|
+
elsif(!_args.empty? && _block)
|
236
|
+
_result = _leaf = _base = @table.fetch(_sym, _klass_new)
|
237
|
+
@table[_sym] = _result
|
238
|
+
|
239
|
+
_args.flatten.each do |_arg|
|
240
|
+
_leaf = base[_arg]
|
241
|
+
unless(_leaf.is_a?(_klass))
|
242
|
+
_leaf = _klass_new
|
243
|
+
_base._set(_arg, _leaf)
|
244
|
+
_base = _leaf
|
242
245
|
end
|
243
246
|
end
|
244
|
-
if(!
|
245
|
-
|
246
|
-
|
247
|
+
if(!_leaf.nil? && _state(:value_collapse))
|
248
|
+
_orig = _leaf
|
249
|
+
_leaf = _orig.parent._klass_new
|
247
250
|
end
|
248
|
-
|
249
|
-
if(
|
250
|
-
unless(
|
251
|
-
|
251
|
+
_block.arity == 0 ? _leaf._build(&_block) : _leaf._build(_leaf, &_block)
|
252
|
+
if(_orig)
|
253
|
+
unless(_orig.is_a?(CollapseArray))
|
254
|
+
_orig = CollapseArray.new.push(_orig)
|
252
255
|
end
|
253
|
-
|
256
|
+
_orig << _leaf
|
254
257
|
else
|
255
|
-
|
258
|
+
_orig = _leaf
|
256
259
|
end
|
257
260
|
else
|
258
|
-
if(
|
259
|
-
@table[
|
260
|
-
|
261
|
-
unless(
|
262
|
-
|
261
|
+
if(_args.size > 1 && _args.all?{|_i| _i.is_a?(::String) || _i.is_a?(::Symbol)} && !_state(:value_collapse))
|
262
|
+
@table[_sym] = _klass_new unless @table[_sym].is_a?(_klass)
|
263
|
+
_endpoint = _args.inject(@table[_sym]) do |_memo, _k|
|
264
|
+
unless(_memo[_k].is_a?(_klass))
|
265
|
+
_memo._set(_k, _klass_new)
|
263
266
|
end
|
264
|
-
|
267
|
+
_memo[_k]
|
265
268
|
end
|
266
|
-
return
|
269
|
+
return _endpoint # custom break out
|
267
270
|
else
|
268
|
-
if(
|
269
|
-
|
270
|
-
if(
|
271
|
-
|
272
|
-
|
271
|
+
if(_args.size > 1)
|
272
|
+
_val = _args.map do |_v|
|
273
|
+
if(_v.is_a?(::Hash) && _state(:hash_load_struct))
|
274
|
+
_val = _klass_new
|
275
|
+
_val._load(_v)
|
273
276
|
else
|
274
|
-
|
277
|
+
_v
|
275
278
|
end
|
276
279
|
end
|
277
280
|
else
|
278
|
-
if(
|
279
|
-
|
280
|
-
|
281
|
+
if(_args.first.is_a?(::Hash) && _state(:hash_load_struct))
|
282
|
+
_val = _klass_new
|
283
|
+
_val._load(_args.first)
|
281
284
|
else
|
282
|
-
|
285
|
+
_val = _args.first
|
283
286
|
end
|
284
287
|
end
|
285
|
-
if(_state(:value_collapse) && !(
|
286
|
-
unless(
|
287
|
-
|
288
|
+
if(_state(:value_collapse) && !(_leaf = @table[_sym]).nil?)
|
289
|
+
unless(_leaf.is_a?(CollapseArray))
|
290
|
+
_leaf = CollapseArray.new.push(_leaf)
|
288
291
|
end
|
289
|
-
|
290
|
-
@table[
|
292
|
+
_leaf << _val
|
293
|
+
@table[_sym] = _leaf
|
291
294
|
else
|
292
|
-
@table[
|
295
|
+
@table[_sym] = _val
|
293
296
|
end
|
294
297
|
end
|
295
298
|
end
|
296
299
|
end
|
297
|
-
@table[
|
298
|
-
@table[
|
300
|
+
@table[_sym] = _klass_new if @table[_sym].nil? && !@table[_sym].is_a?(_klass)
|
301
|
+
@table[_sym]
|
299
302
|
end
|
300
303
|
|
301
304
|
# @return [TrueClass, FalseClass] struct is nil (empty data)
|
@@ -359,7 +362,7 @@ class AttributeStruct < BasicObject
|
|
359
362
|
]
|
360
363
|
end
|
361
364
|
elsif(item.is_a?(::AttributeStruct))
|
362
|
-
item.nil? ?
|
365
|
+
item.nil? ? UNSET_VALUE : item._dump
|
363
366
|
else
|
364
367
|
item
|
365
368
|
end
|
@@ -370,7 +373,7 @@ class AttributeStruct < BasicObject
|
|
370
373
|
processed = @table.keys.map do |key|
|
371
374
|
value = @table[key]
|
372
375
|
val = _dump_unpacker(value)
|
373
|
-
[_dump_unpacker(key), val] unless val ==
|
376
|
+
[_dump_unpacker(key), val] unless val == UNSET_VALUE
|
374
377
|
end.compact
|
375
378
|
__hashish[*processed.flatten(1)]
|
376
379
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bogo
|