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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8589e76432a36f05a1450f97e802269ef8edb8df
4
- data.tar.gz: 6364945c9d3ccb645eb54e2a419675a7d4f2ad61
3
+ metadata.gz: 902bf8b18675d998b37c3543ee77d343d7c37f8f
4
+ data.tar.gz: 29bf97732d4f10a3797a394e0fcc9bbcca228b35
5
5
  SHA512:
6
- metadata.gz: 1bf46e3d433e2718593b6981303a88226b38aff6b421028d1b23f79fb7bdf765a7f800a2c0d35a4d33323f17f3a2a33f73b201d8aee88fec0b68aa1b4757d8b6
7
- data.tar.gz: e26b766049f6bd19ba1b9e54bae280ae97372d5b438ee15bd1c44f97d8d20b2c802461a9f14721a3eaa98ee65f83f9a4a412fbe2faee590b4b980ff97aed05e1
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=nil, &block)
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(sym, *args, &block)
197
- if((s = sym.to_s).end_with?('='))
198
- s.slice!(-1, s.length)
199
- sym = s
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
- sym = _process_key(sym)
202
- if(!args.empty? || block)
203
- if(args.empty? && block)
204
- base = @table.fetch(sym, :__unset__)
205
- if(_state(:value_collapse) && !base.is_a?(self.class!))
206
- orig = base
207
- base = _klass_new
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(base.is_a?(self.class!))
210
- base = _klass_new
212
+ unless(_base.is_a?(self.class!))
213
+ _base = _klass_new
211
214
  end
212
215
  end
213
- @table[sym] = base
214
- if(block.arity == 0)
215
- base.instance_exec(&block)
216
+ @table[_sym] = _base
217
+ if(_block.arity == 0)
218
+ _base.instance_exec(&_block)
216
219
  else
217
- base.instance_exec(base, &block)
220
+ _base.instance_exec(_base, &_block)
218
221
  end
219
- if(orig.is_a?(::NilClass))
220
- @table[sym] = base
222
+ if(_orig.is_a?(::NilClass))
223
+ @table[_sym] = _base
221
224
  else
222
- if(orig == :__unset__)
223
- @table[sym] = base
225
+ if(_orig == UNSET_VALUE)
226
+ @table[_sym] = _base
224
227
  else
225
- unless(orig.is_a?(CollapseArray))
226
- orig = CollapseArray.new.push(orig)
228
+ unless(_orig.is_a?(CollapseArray))
229
+ _orig = CollapseArray.new.push(_orig)
227
230
  end
228
- orig << base
229
- @table[sym] = orig
231
+ _orig << _base
232
+ @table[_sym] = _orig
230
233
  end
231
234
  end
232
- elsif(!args.empty? && block)
233
- result = leaf = base = @table.fetch(sym, _klass_new)
234
- @table[sym] = result
235
-
236
- args.flatten.each do |arg|
237
- leaf = base[arg]
238
- unless(leaf.is_a?(_klass))
239
- leaf = _klass_new
240
- base._set(arg, leaf)
241
- base = leaf
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(!leaf.nil? && _state(:value_collapse))
245
- orig = leaf
246
- leaf = orig.parent._klass_new
247
+ if(!_leaf.nil? && _state(:value_collapse))
248
+ _orig = _leaf
249
+ _leaf = _orig.parent._klass_new
247
250
  end
248
- block.arity == 0 ? leaf._build(&block) : leaf._build(leaf, &block)
249
- if(orig)
250
- unless(orig.is_a?(CollapseArray))
251
- orig = CollapseArray.new.push(orig)
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
- orig << leaf
256
+ _orig << _leaf
254
257
  else
255
- orig = leaf
258
+ _orig = _leaf
256
259
  end
257
260
  else
258
- if(args.size > 1 && args.all?{|i| i.is_a?(::String) || i.is_a?(::Symbol)} && !_state(:value_collapse))
259
- @table[sym] = _klass_new unless @table[sym].is_a?(_klass)
260
- endpoint = args.inject(@table[sym]) do |memo, k|
261
- unless(memo[k].is_a?(_klass))
262
- memo._set(k, _klass_new)
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
- memo[k]
267
+ _memo[_k]
265
268
  end
266
- return endpoint # custom break out
269
+ return _endpoint # custom break out
267
270
  else
268
- if(args.size > 1)
269
- val = args.map do |v|
270
- if(v.is_a?(::Hash) && _state(:hash_load_struct))
271
- val = _klass_new
272
- val._load(v)
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
- v
277
+ _v
275
278
  end
276
279
  end
277
280
  else
278
- if(args.first.is_a?(::Hash) && _state(:hash_load_struct))
279
- val = _klass_new
280
- val._load(args.first)
281
+ if(_args.first.is_a?(::Hash) && _state(:hash_load_struct))
282
+ _val = _klass_new
283
+ _val._load(_args.first)
281
284
  else
282
- val = args.first
285
+ _val = _args.first
283
286
  end
284
287
  end
285
- if(_state(:value_collapse) && !(leaf = @table[sym]).nil?)
286
- unless(leaf.is_a?(CollapseArray))
287
- leaf = CollapseArray.new.push(leaf)
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
- leaf << val
290
- @table[sym] = leaf
292
+ _leaf << _val
293
+ @table[_sym] = _leaf
291
294
  else
292
- @table[sym] = val
295
+ @table[_sym] = _val
293
296
  end
294
297
  end
295
298
  end
296
299
  end
297
- @table[sym] = _klass_new if @table[sym].nil? && !@table[sym].is_a?(_klass)
298
- @table[sym]
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? ? :__unset__ : item._dump
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 == :__unset__
376
+ [_dump_unpacker(key), val] unless val == UNSET_VALUE
374
377
  end.compact
375
378
  __hashish[*processed.flatten(1)]
376
379
  end
@@ -1,4 +1,4 @@
1
1
  class AttributeStruct < BasicObject
2
2
  # Current library version
3
- VERSION = ::Gem::Version.new('0.3.0')
3
+ VERSION = ::Gem::Version.new('0.3.2')
4
4
  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.0
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-01-19 00:00:00.000000000 Z
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bogo