hash-joiner 0.0.3 → 0.0.4
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/lib/hash-joiner.rb +42 -0
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9cbaeb4ca23cb015638cf7307b80bc430c071b8d
|
4
|
+
data.tar.gz: fcdb64ee681d08e378ed49a6d206a939ffa80b9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7212cdd3cd7f4f65949667176403f898d0ca0bb1d399370a2ca4977c85093c03409fa77cace2fcd6b0bfd5582ad099748251bcafd30800dc60400d7f8c6cd3b
|
7
|
+
data.tar.gz: 226291140aafa34dbfae80132eb05ee0f60c8dd454ee6eb25728c5ef087d9ef7de8d77fabc7033cbbff723317d2b14f2981945e2cd269e5e31ba32b5b58dbbad
|
data/lib/hash-joiner.rb
CHANGED
@@ -257,4 +257,46 @@ module HashJoiner
|
|
257
257
|
end
|
258
258
|
lhs
|
259
259
|
end
|
260
|
+
|
261
|
+
# Given a collection, initialize any missing properties to empty values.
|
262
|
+
# @param collection [Hash<String>,Array<Hash<String>>] collection to update
|
263
|
+
# @param array_properties [Array<String>] list of properties to initialize
|
264
|
+
# with an empty Array
|
265
|
+
# @param hash_properties [Array<String>] list of properties to initialize
|
266
|
+
# with an empty Hash
|
267
|
+
# @param string_properties [Array<String>] list of properties to initialize
|
268
|
+
# with an empty String
|
269
|
+
# @return collection
|
270
|
+
def self.assign_empty_defaults(collection, array_properties, hash_properties,
|
271
|
+
string_properties)
|
272
|
+
if collection.instance_of? ::Hash
|
273
|
+
array_properties.each {|i| collection[i] ||= Array.new}
|
274
|
+
hash_properties.each {|i| collection[i] ||= Hash.new}
|
275
|
+
string_properties.each {|i| collection[i] ||= String.new}
|
276
|
+
elsif collection.instance_of? ::Array
|
277
|
+
collection.each do |i|
|
278
|
+
assign_empty_defaults(i,
|
279
|
+
array_properties, hash_properties, string_properties)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
collection
|
283
|
+
end
|
284
|
+
|
285
|
+
# Recursively prunes all empty properties from every element of a
|
286
|
+
# collection.
|
287
|
+
# @param collection [Hash<String>,Array<Hash<String>>] collection to update
|
288
|
+
# @param array_properties [Array<String>] list of properties to initialize
|
289
|
+
def self.prune_empty_properties(collection)
|
290
|
+
if collection.instance_of? ::Hash
|
291
|
+
collection.each_value {|i| prune_empty_properties i}
|
292
|
+
collection.delete_if do |unused_key, value|
|
293
|
+
(value.instance_of? ::Hash or value.instance_of? ::Array or
|
294
|
+
value.instance_of? ::String) and value.empty?
|
295
|
+
end
|
296
|
+
elsif collection.instance_of? ::Array
|
297
|
+
collection.each {|i| prune_empty_properties i}
|
298
|
+
collection.delete_if {|i| i.empty?}
|
299
|
+
end
|
300
|
+
collection
|
301
|
+
end
|
260
302
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-joiner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Bland
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: safe_yaml
|
@@ -117,4 +117,3 @@ signing_key:
|
|
117
117
|
specification_version: 4
|
118
118
|
summary: Module for pruning, promoting, deep-merging, and joining Hash data
|
119
119
|
test_files: []
|
120
|
-
has_rdoc:
|