attribute_struct 0.2.4 → 0.2.6
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -3
- data/attribute_struct.gemspec +0 -1
- data/lib/attribute_struct/attribute_hash.rb +259 -9
- data/lib/attribute_struct/attribute_struct.rb +8 -27
- data/lib/attribute_struct/monkey_camels.rb +2 -0
- data/lib/attribute_struct/version.rb +2 -5
- data/lib/attribute_struct.rb +1 -1
- metadata +4 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b26e0d9ceb950c12c4cc445bb3a64b34ab571ad
|
4
|
+
data.tar.gz: bd94f03ac86dec14a60ec5e7b16f77cb592c0853
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de3283d55f5e015ea981d797f2f22eb7b4d33ddfa717119d69c4ebbe45d2ce0a2f26dc15815784d9b9be2784c5e8b26d71e45d34ffc80d87490521bb6ebe9e9c
|
7
|
+
data.tar.gz: d109ff08acbaca3b6908f193f76b9600cede5442972d5e8e7f221d035069f8e8cbd5450604b11cec6990feeaf3bfe123c878dd65d0dd366dfab8fac99b0565dc
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/attribute_struct.gemspec
CHANGED
@@ -1,17 +1,267 @@
|
|
1
|
-
require '
|
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
|
-
|
11
|
-
|
12
|
-
|
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
|
-
#
|
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
|
-
|
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.
|
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
|
-
|
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
|
-
|
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,9 +1,6 @@
|
|
1
|
-
require '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.
|
5
|
+
VERSION = ::Gem::Version.new('0.2.6')
|
9
6
|
end
|
data/lib/attribute_struct.rb
CHANGED
@@ -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
|
+
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-
|
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:
|