modalsupport 0.9.0 → 0.9.1
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.
- data/VERSION +1 -1
- data/lib/modalsupport/recursive_map.rb +21 -10
- data/modalsupport.gemspec +2 -2
- data/test/test_recursive_map.rb +30 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.1
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module ModalSupport
|
2
|
-
|
2
|
+
|
3
3
|
module_function
|
4
|
-
|
4
|
+
|
5
5
|
# Map hashes and arrays recursively. The supplied block is used to map all items (values and container).
|
6
6
|
#
|
7
7
|
# Examples
|
@@ -12,23 +12,34 @@ module ModalSupport
|
|
12
12
|
# # convert hashes to OpenStructs
|
13
13
|
# recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}){|v| v.kind_of?(Hash) ? OpenStruct.new(v) : v}
|
14
14
|
# # => #<OpenStruct b=22, a=11, c=#<OpenStruct x=100, y=200>>
|
15
|
-
def recursive_map(container, &blk)
|
15
|
+
def recursive_map(container, key=nil, &blk)
|
16
16
|
raise ArgumentError, "recursive_map requires a block" if blk.nil?
|
17
17
|
case container
|
18
18
|
when Hash
|
19
|
-
blk
|
20
|
-
[k,
|
21
|
-
|
19
|
+
if blk.arity==2
|
20
|
+
blk[key, container.map{|k,v|
|
21
|
+
[k, recursive_map(v, k, &blk)]
|
22
|
+
}.to_h]
|
23
|
+
else
|
24
|
+
blk[container.map{|k,v|
|
25
|
+
[k, recursive_map(v, &blk)]
|
26
|
+
}.to_h]
|
27
|
+
end
|
22
28
|
when Array
|
23
|
-
blk
|
29
|
+
if blk.arity==2
|
30
|
+
blk[key, container.map_with_index{|v, i| recursive_map(v, i, &blk)}]
|
31
|
+
else
|
32
|
+
blk[container.map{|v| recursive_map(v, &blk)}]
|
33
|
+
end
|
24
34
|
else
|
25
|
-
|
35
|
+
mapped = blk.arity==2 ? blk[key, container] : blk[container]
|
36
|
+
case mapped
|
26
37
|
when Hash, Array
|
27
|
-
recursive_map(mapped, &blk)
|
38
|
+
recursive_map(mapped, key, &blk)
|
28
39
|
else
|
29
40
|
mapped
|
30
41
|
end
|
31
42
|
end
|
32
43
|
end
|
33
|
-
|
44
|
+
|
34
45
|
end
|
data/modalsupport.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "modalsupport"
|
8
|
-
s.version = "0.9.
|
8
|
+
s.version = "0.9.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Javier Goizueta"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-05-14"
|
13
13
|
s.description = "additional support extensions to ActiveSupport and HoboSupport"
|
14
14
|
s.email = "jgoizueta@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/test/test_recursive_map.rb
CHANGED
@@ -32,6 +32,21 @@ class TestRecursiveMap < Test::Unit::TestCase
|
|
32
32
|
)
|
33
33
|
end
|
34
34
|
|
35
|
+
should "process values with indices in containers" do
|
36
|
+
assert_equal(
|
37
|
+
{:a=>11, :b=>44},
|
38
|
+
ModalSupport.recursive_map(:a=>11, :b=>22){|i,v|
|
39
|
+
i==:b ? v*2 : v
|
40
|
+
}
|
41
|
+
)
|
42
|
+
assert_equal(
|
43
|
+
[11,44],
|
44
|
+
ModalSupport.recursive_map([11,22]){|i,v|
|
45
|
+
i.nil? ? v : (i+1)*v
|
46
|
+
}
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
35
50
|
should "process values in nested containers" do
|
36
51
|
assert_equal(
|
37
52
|
{:a=>22, :b=>44, :c=>{:x=>200,:y=>400}, :d=>[2,4,6]},
|
@@ -43,6 +58,21 @@ class TestRecursiveMap < Test::Unit::TestCase
|
|
43
58
|
)
|
44
59
|
end
|
45
60
|
|
61
|
+
should "process values with indices in nested containers" do
|
62
|
+
assert_equal(
|
63
|
+
{:a=>11, :b=>44, :c=>{:x=>200,:y=>200}, :d=>[1,4,3]},
|
64
|
+
ModalSupport.recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3]){|i,v|
|
65
|
+
[:x,:b,1].include?(i) ? v*2 : v
|
66
|
+
}
|
67
|
+
)
|
68
|
+
assert_equal(
|
69
|
+
[0, 22, {:c=>{:x=>100,:y=>200}, :d=>[0,2,6]}],
|
70
|
+
ModalSupport.recursive_map([11, 22, {:c=>{:x=>100,:y=>200}, :d=>[1,2,3]}]){|i,v|
|
71
|
+
(i.kind_of?(Numeric) && v.kind_of?(Numeric)) ? i*v : v
|
72
|
+
}
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
46
76
|
should "process values in deeply nested containers" do
|
47
77
|
assert_equal(
|
48
78
|
{:a=>22, :b=>44, :c=>{:x=>200, :y=>400}, :d=>[2, 4, 6, {:zz=>[2000, 4000, {:yy=>{:xx=>6000}}]}]},
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modalsupport
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: shoulda
|