hash-joiner 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hash-joiner.rb +42 -0
  3. metadata +2 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 759dc6bcf51ede5903167adf68613554adcec044
4
- data.tar.gz: 3fd0a5e98271be1025d65b5c9610f7d73b127c63
3
+ metadata.gz: 9cbaeb4ca23cb015638cf7307b80bc430c071b8d
4
+ data.tar.gz: fcdb64ee681d08e378ed49a6d206a939ffa80b9c
5
5
  SHA512:
6
- metadata.gz: 429156c991502011b29fc54d6aff1b1495b0eeb80f19b55b44394f3bdfd65be180b31407b74ded53a0005c16aad947a72edbfb711066b1e173c7822dc61694fe
7
- data.tar.gz: 2658c4cebb926998d05c3add6830c6f55c65324ffede7021e40ddadad4f3df57c4e4efbbd5c8b92f887e977e011db5367e2c73393edb00120cd01f43a6627923
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.3
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-01-13 00:00:00.000000000 Z
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: