attribute_struct 0.2.4 → 0.2.6

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: 709d30c8e86eba9009d9fca15051da16231cd56a
4
- data.tar.gz: e8d57a6e7b239b494208dd58614502220775ebe6
3
+ metadata.gz: 4b26e0d9ceb950c12c4cc445bb3a64b34ab571ad
4
+ data.tar.gz: bd94f03ac86dec14a60ec5e7b16f77cb592c0853
5
5
  SHA512:
6
- metadata.gz: 061d981d6a1d49882acf7acea0c231b0111e0163e47a448c5bb71b3ebdcec3ac5a077950d9a6237e3eb75050fcdf2883a9d46619a507fc147b9b0be920128b98
7
- data.tar.gz: 9bc6279dc98283f19a27c09fc920ccd28455f34f2629c4573f50fb13d47b8fa2189b4216d3211e355d0c6511d531440a4e6f81232e3357bd0e2dd85555428ebb
6
+ metadata.gz: de3283d55f5e015ea981d797f2f22eb7b4d33ddfa717119d69c4ebbe45d2ce0a2f26dc15815784d9b9be2784c5e8b26d71e45d34ffc80d87490521bb6ebe9e9c
7
+ data.tar.gz: d109ff08acbaca3b6908f193f76b9600cede5442972d5e8e7f221d035069f8e8cbd5450604b11cec6990feeaf3bfe123c878dd65d0dd366dfab8fac99b0565dc
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v0.2.6
2
+ * Vendor `Mash` and expand to provide required deep_merge functionality
3
+ * Remove external dependencies
4
+
1
5
  ## v0.2.4
2
6
  * Revert #class method removal (required by hash helpers when duping)
3
7
  * Set base prior to path walking
data/Gemfile.lock CHANGED
@@ -1,13 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribute_struct (0.2.4)
5
- hashie (>= 2.0.0)
4
+ attribute_struct (0.2.5)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
9
8
  specs:
10
- hashie (2.1.1)
11
9
  minitest (5.0.6)
12
10
 
13
11
  PLATFORMS
@@ -10,6 +10,5 @@ Gem::Specification.new do |s|
10
10
  s.homepage = 'http://github.com/chrisroberts/attribute_struct'
11
11
  s.description = 'Attribute structures'
12
12
  s.require_path = 'lib'
13
- s.add_dependency 'hashie', '>= 2.0.0'
14
13
  s.files = Dir['**/*']
15
14
  end
@@ -1,17 +1,267 @@
1
- require 'hashie/extensions/deep_merge'
2
- require 'hashie/extensions/indifferent_access'
1
+ require 'attribute_struct'
3
2
 
4
3
  class AttributeStruct
5
- class AttributeHash < ::Hash
6
- include ::Hashie::Extensions::DeepMerge
7
- include ::Hashie::Extensions::IndifferentAccess
8
4
 
5
+ # Copyright (c) 2009 Dan Kubb
6
+
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ # ---
27
+ # ---
28
+
29
+ # Some portions of blank.rb and mash.rb are verbatim copies of software
30
+ # licensed under the MIT license. That license is included below:
31
+
32
+ # Copyright (c) 2005-2008 David Heinemeier Hansson
33
+
34
+ # Permission is hereby granted, free of charge, to any person obtaining
35
+ # a copy of this software and associated documentation files (the
36
+ # "Software"), to deal in the Software without restriction, including
37
+ # without limitation the rights to use, copy, modify, merge, publish,
38
+ # distribute, sublicense, and/or sell copies of the Software, and to
39
+ # permit persons to whom the Software is furnished to do so, subject to
40
+ # the following conditions:
41
+
42
+ # The above copyright notice and this permission notice shall be
43
+ # included in all copies or substantial portions of the Software.
44
+
45
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
46
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
48
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
49
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
50
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
51
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52
+
53
+ # This class has dubious semantics and we only have it so that people can write
54
+ # params[:key] instead of params['key'].
55
+ class Mash < ::Hash
56
+
57
+ # @param constructor<Object>
58
+ # The default value for the mash. Defaults to an empty hash.
59
+ #
60
+ # @details [Alternatives]
61
+ # If constructor is a Hash, a new mash will be created based on the keys of
62
+ # the hash and no default value will be set.
63
+ def initialize(constructor = {})
64
+ if constructor.is_a?(Hash)
65
+ super()
66
+ update(constructor)
67
+ else
68
+ super(constructor)
69
+ end
70
+ end
71
+
72
+ # @param orig<Object> Mash being copied
73
+ #
74
+ # @return [Object] A new copied Mash
75
+ def initialize_copy(orig)
76
+ super
77
+ # Handle nested values
78
+ each do |k,v|
79
+ if v.kind_of?(Mash) || v.is_a?(Array)
80
+ self[k] = v.dup
81
+ end
82
+ end
83
+ self
84
+ end
85
+
86
+ # @param key<Object> The default value for the mash. Defaults to nil.
87
+ #
88
+ # @details [Alternatives]
89
+ # If key is a Symbol and it is a key in the mash, then the default value will
90
+ # be set to the value matching the key.
91
+ def default(key = nil)
92
+ if key.is_a?(Symbol) && include?(key = key.to_s)
93
+ self[key]
94
+ else
95
+ super
96
+ end
97
+ end
98
+
99
+ unless method_defined?(:regular_writer)
100
+ alias_method :regular_writer, :[]=
101
+ end
102
+ unless method_defined?(:regular_update)
103
+ alias_method :regular_update, :update
104
+ end
105
+
106
+ # @param key<Object> The key to set.
107
+ # @param value<Object>
108
+ # The value to set the key to.
109
+ #
110
+ # @see Mash#convert_key
111
+ # @see Mash#convert_value
112
+ def []=(key, value)
113
+ regular_writer(convert_key(key), convert_value(value))
114
+ end
115
+
116
+ # @param other_hash<Hash>
117
+ # A hash to update values in the mash with. The keys and the values will be
118
+ # converted to Mash format.
119
+ #
120
+ # @return [Mash] The updated mash.
121
+ def update(other_hash)
122
+ other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
123
+ self
124
+ end
125
+
126
+ alias_method :merge!, :update
127
+
128
+ # @param key<Object> The key to check for. This will be run through convert_key.
129
+ #
130
+ # @return [Boolean] True if the key exists in the mash.
131
+ def key?(key)
132
+ super(convert_key(key))
133
+ end
134
+
135
+ # def include? def has_key? def member?
136
+ alias_method :include?, :key?
137
+ alias_method :has_key?, :key?
138
+ alias_method :member?, :key?
139
+
140
+ # @param key<Object> The key to fetch. This will be run through convert_key.
141
+ # @param *extras<Array> Default value.
142
+ #
143
+ # @return [Object] The value at key or the default value.
144
+ def fetch(key, *extras)
145
+ super(convert_key(key), *extras)
146
+ end
147
+
148
+ # @param *indices<Array>
149
+ # The keys to retrieve values for. These will be run through +convert_key+.
150
+ #
151
+ # @return [Array] The values at each of the provided keys
152
+ def values_at(*indices)
153
+ indices.collect {|key| self[convert_key(key)]}
154
+ end
155
+
156
+ # @param hash<Hash> The hash to merge with the mash.
157
+ #
158
+ # @return [Mash] A new mash with the hash values merged in.
159
+ def merge(hash)
160
+ self.dup.update(hash)
161
+ end
162
+
163
+ # @param key<Object>
164
+ # The key to delete from the mash.\
165
+ def delete(key)
166
+ super(convert_key(key))
167
+ end
168
+
169
+ # @param *rejected<Array[(String, Symbol)] The mash keys to exclude.
170
+ #
171
+ # @return [Mash] A new mash without the selected keys.
172
+ #
173
+ # @example
174
+ # { :one => 1, :two => 2, :three => 3 }.except(:one)
175
+ # #=> { "two" => 2, "three" => 3 }
176
+ def except(*keys)
177
+ super(*keys.map {|k| convert_key(k)})
178
+ end
179
+
180
+ # Used to provide the same interface as Hash.
181
+ #
182
+ # @return [Mash] This mash unchanged.
183
+ def stringify_keys!; self end
184
+
185
+ # @return [Hash] The mash as a Hash with symbolized keys.
186
+ def symbolize_keys
187
+ h = Hash.new(default)
188
+ each { |key, val| h[key.to_sym] = val }
189
+ h
190
+ end
191
+
192
+ # @return [Hash] The mash as a Hash with string keys.
9
193
  def to_hash
10
- ::Hash[
11
- self.map do |k,v|
12
- [k, v.is_a?(::Hash) ? v.to_hash : v]
194
+ Hash.new(default).merge(self)
195
+ end
196
+
197
+ # @return [Mash] Convert a Hash into a Mash
198
+ # The input Hash's default value is maintained
199
+ def self.from_hash(hash)
200
+ mash = Mash.new(hash)
201
+ mash.default = hash.default
202
+ mash
203
+ end
204
+
205
+ protected
206
+ # @param key<Object> The key to convert.
207
+ #
208
+ # @param [Object]
209
+ # The converted key. If the key was a symbol, it will be converted to a
210
+ # string.
211
+ #
212
+ # @api private
213
+ def convert_key(key)
214
+ key.kind_of?(Symbol) ? key.to_s : key
215
+ end
216
+
217
+ # @param value<Object> The value to convert.
218
+ #
219
+ # @return [Object]
220
+ # The converted value. A Hash or an Array of hashes, will be converted to
221
+ # their Mash equivalents.
222
+ #
223
+ # @api private
224
+ def convert_value(value)
225
+ if value.class == Hash
226
+ Mash.from_hash(value)
227
+ elsif value.is_a?(Array)
228
+ value.collect { |e| convert_value(e) }
229
+ else
230
+ value
231
+ end
232
+ end
233
+
234
+ # == add required deep merging support
235
+
236
+ public
237
+
238
+ # Perform deep merge
239
+ #
240
+ # @return [AttributeStruct::Mash] merged hash
241
+ def deep_merge(hash)
242
+ unless(hash.is_a?(Hash))
243
+ raise ArgumentError.new "Expecting `Hash` type. Received: `#{hash.class}`"
244
+ end
245
+ new_self = self.dup
246
+ hash.each do |k,v|
247
+ if(new_self[k].is_a?(Hash) && v.is_a?(Hash))
248
+ new_self[k] = new_self[k].deep_merge(v)
249
+ else
250
+ new_self[k] = v
13
251
  end
14
- ]
252
+ end
253
+ new_self
15
254
  end
255
+
256
+ # Perform deep merge and replace contents of self
257
+ #
258
+ # @return [self]
259
+ def deep_merge!(hash)
260
+ self.replace(self.deep_merge(hash))
261
+ self
262
+ end
263
+
16
264
  end
265
+
266
+ AttributeHash = Mash
17
267
  end
@@ -1,3 +1,4 @@
1
+ require 'attribute_struct'
1
2
  require 'attribute_struct/irb_compat'
2
3
 
3
4
  class AttributeStruct < BasicObject
@@ -6,8 +7,6 @@ class AttributeStruct < BasicObject
6
7
 
7
8
  # @return [Truthy, Falsey] global flag for camel keys
8
9
  attr_reader :camel_keys
9
- # @return [Truthy, Falsey] force chef tooling (Mash)
10
- attr_accessor :force_chef
11
10
 
12
11
  # Automatically converts keys to camel case
13
12
  #
@@ -26,24 +25,9 @@ class AttributeStruct < BasicObject
26
25
  end
27
26
  end
28
27
 
29
- # Determine what hash library to load based on availability
30
- def load_the_hash
31
- unless(@hash_loaded)
32
- if(defined?(Chef) || force_chef)
33
- require 'chef/mash'
34
- require 'chef/mixin/deep_merge'
35
- @hash_loaded = :chef
36
- else
37
- require 'attribute_struct/attribute_hash'
38
- @hash_loaded = :attribute_hash
39
- end
40
- end
41
- end
42
-
43
- # @return [AttributeStruct::AttributeHash, Mash]
28
+ # @return [AttributeStruct::AttributeHash]
44
29
  def hashish
45
- load_the_hash
46
- @hash_loaded == :chef ? ::Mash : ::AttributeStruct::AttributeHash
30
+ ::AttributeStruct::AttributeHash
47
31
  end
48
32
 
49
33
  # Create AttributeStruct instance and dump the resulting hash
@@ -73,7 +57,6 @@ class AttributeStruct < BasicObject
73
57
  # @param init_hash [Hash] hash to initialize struct
74
58
  # @yield block to execute within struct context
75
59
  def initialize(init_hash=nil, &block)
76
- _klass.load_the_hash
77
60
  @_camel_keys = _klass.camel_keys
78
61
  @_arg_state = __hashish.new
79
62
  @table = __hashish.new
@@ -107,7 +90,7 @@ class AttributeStruct < BasicObject
107
90
  # @param traverse [TrueClass, FalseClass] traverse towards root for matching key
108
91
  # @return [Object, NilClass]
109
92
  def _state(key, traverse=true)
110
- if(_arg_state.keys.include?(key))
93
+ if(_arg_state.has_key?(key))
111
94
  _arg_state[key]
112
95
  else
113
96
  if(traverse && _parent)
@@ -326,11 +309,7 @@ class AttributeStruct < BasicObject
326
309
  def _merge(overlay)
327
310
  source = _deep_copy
328
311
  dest = overlay._deep_copy
329
- if(defined?(::Chef))
330
- result = ::Chef::Mixin::DeepMerge.merge(source, dest)
331
- else
332
- result = source.deep_merge(dest)
333
- end
312
+ result = source.deep_merge(dest)
334
313
  _klass.new(result)
335
314
  end
336
315
 
@@ -346,7 +325,7 @@ class AttributeStruct < BasicObject
346
325
 
347
326
  # @return [Class] hashish type available
348
327
  def __hashish
349
- defined?(::Chef) ? ::Mash : ::AttributeStruct::AttributeHash
328
+ ::AttributeStruct::AttributeHash
350
329
  end
351
330
 
352
331
  # Provide dup of instance
@@ -486,3 +465,5 @@ class AttributeStruct < BasicObject
486
465
  end
487
466
 
488
467
  end
468
+
469
+ require 'attribute_struct/attribute_hash'
@@ -1,3 +1,5 @@
1
+ require 'attribute_struct'
2
+
1
3
  module MonkeyCamels
2
4
 
3
5
  class << self
@@ -1,9 +1,6 @@
1
- require 'attribute_struct/attribute_struct'
1
+ require 'attribute_struct'
2
2
 
3
3
  class AttributeStruct
4
- # Custom version container
5
- class Version < ::Gem::Version
6
- end
7
4
  # Current library version
8
- VERSION = Version.new('0.2.4')
5
+ VERSION = ::Gem::Version.new('0.2.6')
9
6
  end
@@ -1,2 +1,2 @@
1
1
  require 'attribute_struct/version'
2
-
2
+ require 'attribute_struct/attribute_struct'
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-04 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: hashie
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 2.0.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: 2.0.0
11
+ date: 2014-08-28 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: Attribute structures
28
14
  email: chrisroberts.code@gmail.com
29
15
  executables: []
@@ -73,3 +59,4 @@ signing_key:
73
59
  specification_version: 4
74
60
  summary: Attribute structures
75
61
  test_files: []
62
+ has_rdoc: