attribute_struct 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v0.1.8
2
+ * Basic (optional) auto camel detection on import
3
+ * Add hash helper methods out to class
4
+
1
5
  ## v0.1.6
2
6
  * Add helpers for walking up the tree
3
7
  * Update hashie dependency restriction
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribute_struct (0.1.6)
4
+ attribute_struct (0.1.7)
5
5
  hashie (>= 2.0.0)
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  class AttributeStruct < BasicObject
2
-
2
+
3
3
  class << self
4
4
 
5
5
  # Global flag for camel cased keys
@@ -28,18 +28,24 @@ class AttributeStruct < BasicObject
28
28
  if(defined?(Chef) || force_chef)
29
29
  require 'chef/mash'
30
30
  require 'chef/mixin/deep_merge'
31
+ @hash_loaded = :chef
31
32
  else
32
33
  require 'attribute_struct/attribute_hash'
34
+ @hash_loaded = :attribute_hash
33
35
  end
34
- @hash_loaded = true
35
36
  end
36
37
  end
37
38
 
39
+ def hashish
40
+ load_the_hash
41
+ @hash_loaded == :chef ? ::Mash : ::AttributeStruct::AttributeHash
42
+ end
43
+
38
44
  end
39
45
 
40
46
  # Flag for camel cased keys
41
47
  attr_reader :_camel_keys
42
-
48
+
43
49
  def initialize(*args, &block)
44
50
  _klass.load_the_hash
45
51
  @_camel_keys = _klass.camel_keys
@@ -182,7 +188,16 @@ class AttributeStruct < BasicObject
182
188
  # Clears current instance data and replaces with provided hash
183
189
  def _load(hashish)
184
190
  @table.clear
191
+ if(_root._camel_keys_action == :auto_discovery)
192
+ starts = hashish.keys.map{|k|k[0,1]}
193
+ unless(starts.detect{|k| k =~ /[A-Z]/})
194
+ _camel_keys_set(:auto_disable)
195
+ else
196
+ _camel_keys_set(:auto_enable) unless _parent.nil?
197
+ end
198
+ end
185
199
  hashish.each do |key, value|
200
+ key = key.dup
186
201
  if(value.is_a?(::Enumerable))
187
202
  flat = value.map do |v|
188
203
  v.is_a?(::Hash) ? _klass.new(v) : v
@@ -203,7 +218,7 @@ class AttributeStruct < BasicObject
203
218
  def _merge(target)
204
219
  source = _deep_copy
205
220
  dest = target._deep_copy
206
- if(defined?(::Mash))
221
+ if(defined?(::Chef))
207
222
  result = ::Chef::Mixin::DeepMerge.merge(source, dest)
208
223
  else
209
224
  result = source.deep_merge(dest)
@@ -222,7 +237,7 @@ class AttributeStruct < BasicObject
222
237
 
223
238
  # Returns a new Hash type instance based on what is available
224
239
  def __hashish
225
- defined?(::Mash) ? ::Mash : ::AttributeStruct::AttributeHash
240
+ defined?(::Chef) ? ::Mash : ::AttributeStruct::AttributeHash
226
241
  end
227
242
 
228
243
  # Returns dup of value. Converts Symbol objects to strings
@@ -283,7 +298,10 @@ class AttributeStruct < BasicObject
283
298
  # Helper to return new instance of current instance type
284
299
  def _klass_new
285
300
  n = _klass.new
286
- n._camel_keys_set(_camel_keys_action)
301
+ unless(_camel_keys_action == :auto_discovery)
302
+ n._camel_keys_set(_camel_keys_action)
303
+ end
304
+ n._camel_keys = _camel_keys
287
305
  n._parent(self)
288
306
  n
289
307
  end
@@ -331,5 +349,5 @@ class AttributeStruct < BasicObject
331
349
  end
332
350
  end
333
351
  end
334
-
352
+
335
353
  end
@@ -4,5 +4,5 @@ class AttributeStruct
4
4
  class Version < ::Gem::Version
5
5
  end
6
6
 
7
- VERSION = Version.new('0.1.6')
7
+ VERSION = Version.new('0.1.8')
8
8
  end
data/test/specs/camel.rb CHANGED
@@ -40,4 +40,41 @@ describe AttributeStruct do
40
40
  dump['ValueTwo']['Nesting'].must_equal true
41
41
  end
42
42
  end
43
+ describe 'Camel enabled imports' do
44
+ before do
45
+ @struct = AttributeStruct.new
46
+ @struct._camel_keys = true
47
+ @struct._camel_keys_set(:auto_discovery)
48
+ @struct._load(
49
+ {
50
+ 'Fubar' => {
51
+ 'FooBar' => true,
52
+ 'FeeBar' => {
53
+ 'FauxBar' => 'done'
54
+ },
55
+ 'FooDar' => {
56
+ 'snake_case' => {
57
+ 'still_snake' => {
58
+ 'NowCamel' => {
59
+ 'CamelCamel' => 'yep, a camel'
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }
66
+ )
67
+ @struct._camel_keys_set(nil)
68
+ end
69
+
70
+ it 'should properly export keys after discovery' do
71
+ @struct.fubar.new_bar.halt 'new_value'
72
+ @struct.fubar.foo_dar.snake_case.new_snake 'snake!'
73
+ @struct.fubar.foo_dar.snake_case.still_snake.now_camel.new_camel 'a camel!'
74
+ hash = @struct._dump
75
+ hash['Fubar']['NewBar']['Halt'].must_equal 'new_value'
76
+ hash['Fubar']['FooDar']['snake_case']['new_snake'].must_equal 'snake!'
77
+ hash['Fubar']['FooDar']['snake_case']['still_snake']['NowCamel']['NewCamel'].must_equal 'a camel!'
78
+ end
79
+ end
43
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-02 00:00:00.000000000 Z
12
+ date: 2013-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie
@@ -44,7 +44,6 @@ files:
44
44
  - test/specs/basic.rb
45
45
  - Gemfile
46
46
  - README.md
47
- - attribute_struct-0.1.4.gem
48
47
  - attribute_struct.gemspec
49
48
  - CHANGELOG.md
50
49
  - Gemfile.lock
Binary file