motion-support 0.2.5 → 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.
@@ -8,6 +8,7 @@ files = [
8
8
  'core_ext/hash/keys',
9
9
  'core_ext/hash/reverse_merge',
10
10
  'core_ext/hash/slice',
11
+ 'core_ext/hash/deep_delete_if',
11
12
  'hash_with_indifferent_access',
12
13
 
13
14
  'core_ext/module/delegation'
@@ -0,0 +1,23 @@
1
+ class Hash
2
+ # Returns a new hash with keys deleted if they match a criteria
3
+ # h1 = { x: { y: [ { z: 4, y: 1 }, 5, 6] }, a: { b: 2 } }
4
+ #
5
+ # h1.deep_delete { |k,v| k == :z } #=> { x: { y: [ { y: 1 }, 5, 6] }, a: { b: 2 } }
6
+ # h1.deep_delete { |k,v| k == :y } #=> { x: {}, a: { b: 2 } }
7
+ def deep_delete_if(&block)
8
+ result = {}
9
+ each do |key, value|
10
+ next if block.call(key, value)
11
+
12
+ result[key] = if value.is_a?(Hash)
13
+ value.deep_delete_if(&block)
14
+ elsif value.is_a?(Array)
15
+ value.map { |v| v.is_a?(Hash) ? v.deep_delete_if(&block) : v }
16
+ else
17
+ value
18
+ end
19
+ end
20
+
21
+ result
22
+ end
23
+ end
@@ -82,7 +82,13 @@ class Hash
82
82
  def deep_transform_keys(&block)
83
83
  result = {}
84
84
  each do |key, value|
85
- result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value
85
+ result[yield(key)] = if value.is_a?(Hash)
86
+ value.deep_transform_keys(&block)
87
+ elsif value.is_a?(Array)
88
+ value.map { |v| v.is_a?(Hash) ? v.deep_transform_keys(&block) : v }
89
+ else
90
+ value
91
+ end
86
92
  end
87
93
  result
88
94
  end
@@ -93,7 +99,13 @@ class Hash
93
99
  def deep_transform_keys!(&block)
94
100
  keys.each do |key|
95
101
  value = delete(key)
96
- self[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys!(&block) : value
102
+ self[yield(key)] = if value.is_a?(Hash)
103
+ value.deep_transform_keys(&block)
104
+ elsif value.is_a?(Array)
105
+ value.map { |v| v.is_a?(Hash) ? v.deep_transform_keys(&block) : v }
106
+ else
107
+ value
108
+ end
97
109
  end
98
110
  self
99
111
  end
@@ -1,3 +1,3 @@
1
1
  module MotionSupport
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -0,0 +1,19 @@
1
+ describe "hash" do
2
+ before do
3
+ @hash = { :a => 1, :b => [ { :c => 2, :d => 3 }, :b ], :d => { :f => 4 } }
4
+ end
5
+
6
+ describe "deep_delete_if" do
7
+ it "should delete a top level key recursively" do
8
+ @hash.deep_delete_if { |k,v| k == :a }.should == { :b => [ { :c => 2, :d => 3 }, :b ], :d => { :f => 4 } }
9
+ end
10
+
11
+ it "should delete a key within an array recursively" do
12
+ @hash.deep_delete_if { |k,v| v == 2 }.should == { :a => 1, :b => [ { :d => 3 }, :b ], :d => { :f => 4 } }
13
+ end
14
+
15
+ it "should delete a key within a hash recursively" do
16
+ @hash.deep_delete_if { |k,v| v == 4 }.should == { :a => 1, :b => [ { :c => 2, :d => 3 }, :b ], :d => {} }
17
+ end
18
+ end
19
+ end
@@ -13,6 +13,8 @@ describe "Hash" do
13
13
  @nested_illegal_symbols = { [] => { [] => 3} }
14
14
  @upcase_strings = { 'A' => 1, 'B' => 2 }
15
15
  @nested_upcase_strings = { 'A' => { 'B' => { 'C' => 3 } } }
16
+ @nested_array_symbols = { :a => [ { :a => 1 }, { :b => 2 } ] }
17
+ @nested_array_strings = { 'a' => [ { 'a' => 1 }, { 'b' => 2 } ] }
16
18
  end
17
19
 
18
20
  it "should have all key methods defined on literal hash" do
@@ -120,6 +122,7 @@ describe "Hash" do
120
122
  @nested_symbols.deep_symbolize_keys.should == @nested_symbols
121
123
  @nested_strings.deep_symbolize_keys.should == @nested_symbols
122
124
  @nested_mixed.deep_symbolize_keys.should == @nested_symbols
125
+ @nested_array_strings.deep_symbolize_keys.should == @nested_array_symbols
123
126
  end
124
127
 
125
128
  it "should not mutate" do
@@ -159,6 +162,7 @@ describe "Hash" do
159
162
  @nested_symbols.deep_dup.deep_symbolize_keys!.should == @nested_symbols
160
163
  @nested_strings.deep_dup.deep_symbolize_keys!.should == @nested_symbols
161
164
  @nested_mixed.deep_dup.deep_symbolize_keys!.should == @nested_symbols
165
+ @nested_array_strings.deep_symbolize_keys!.should == @nested_array_symbols
162
166
  end
163
167
 
164
168
  it "should mutate" do
@@ -188,6 +192,7 @@ describe "Hash" do
188
192
  @nested_symbols.deep_stringify_keys.should == @nested_strings
189
193
  @nested_strings.deep_stringify_keys.should == @nested_strings
190
194
  @nested_mixed.deep_stringify_keys.should == @nested_strings
195
+ @nested_array_symbols.deep_stringify_keys.should == @nested_array_strings
191
196
  end
192
197
 
193
198
  it "should not mutate" do
@@ -217,6 +222,7 @@ describe "Hash" do
217
222
  @nested_symbols.deep_dup.deep_stringify_keys!.should == @nested_strings
218
223
  @nested_strings.deep_dup.deep_stringify_keys!.should == @nested_strings
219
224
  @nested_mixed.deep_dup.deep_stringify_keys!.should == @nested_strings
225
+ @nested_array_symbols.deep_stringify_keys!.should == @nested_array_strings
220
226
  end
221
227
 
222
228
  it "should mutate" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: motion-support
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.5
5
+ version: 0.2.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Thomas Kadauke
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-10-02 00:00:00 Z
13
+ date: 2013-12-28 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: motion-require
@@ -98,6 +98,7 @@ files:
98
98
  - motion/core_ext/date/conversions.rb
99
99
  - motion/core_ext/date_and_time/calculations.rb
100
100
  - motion/core_ext/enumerable.rb
101
+ - motion/core_ext/hash/deep_delete_if.rb
101
102
  - motion/core_ext/hash/deep_merge.rb
102
103
  - motion/core_ext/hash/except.rb
103
104
  - motion/core_ext/hash/indifferent_access.rb
@@ -172,6 +173,7 @@ files:
172
173
  - spec/motion-support/core_ext/date/conversion_spec.rb
173
174
  - spec/motion-support/core_ext/date_and_time/calculation_spec.rb
174
175
  - spec/motion-support/core_ext/enumerable_spec.rb
176
+ - spec/motion-support/core_ext/hash/deep_delete_if_spec.rb
175
177
  - spec/motion-support/core_ext/hash/deep_merge_spec.rb
176
178
  - spec/motion-support/core_ext/hash/except_spec.rb
177
179
  - spec/motion-support/core_ext/hash/key_spec.rb
@@ -235,7 +237,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
235
237
  requirements:
236
238
  - - ">="
237
239
  - !ruby/object:Gem::Version
238
- hash: 3988570019635634202
240
+ hash: -1590752612782680441
239
241
  segments:
240
242
  - 0
241
243
  version: "0"
@@ -244,7 +246,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
246
  requirements:
245
247
  - - ">="
246
248
  - !ruby/object:Gem::Version
247
- hash: 3988570019635634202
249
+ hash: -1590752612782680441
248
250
  segments:
249
251
  - 0
250
252
  version: "0"
@@ -274,6 +276,7 @@ test_files:
274
276
  - spec/motion-support/core_ext/date/conversion_spec.rb
275
277
  - spec/motion-support/core_ext/date_and_time/calculation_spec.rb
276
278
  - spec/motion-support/core_ext/enumerable_spec.rb
279
+ - spec/motion-support/core_ext/hash/deep_delete_if_spec.rb
277
280
  - spec/motion-support/core_ext/hash/deep_merge_spec.rb
278
281
  - spec/motion-support/core_ext/hash/except_spec.rb
279
282
  - spec/motion-support/core_ext/hash/key_spec.rb