modalsupport 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
1
+ 0.8.0
@@ -27,4 +27,15 @@ class Array
27
27
  end
28
28
  end
29
29
 
30
+ # create a hash from an array of [key,value] tuples
31
+ # you can set default or provide a block just as with Hash::new
32
+ # Note: if you use [key, value1, value2, value#], hash[key] will
33
+ # be [value1, value2, value#]
34
+ def to_h(default=nil, &block)
35
+ # code by Stefan Rusterholz fixed for Ruby 1.9.2; see http://www.ruby-forum.com/topic/138218
36
+ hash = block_given? ? Hash.new(&block) : Hash.new(default)
37
+ each { |(key, *value)| hash[key]=value.size>1 ? value : value.first }
38
+ hash
39
+ end
40
+
30
41
  end
@@ -0,0 +1,29 @@
1
+ module ModalSupport
2
+
3
+ module_function
4
+
5
+ # Map hashes and arrays recursively. The supplied block is used to map all items (values and container).
6
+ #
7
+ # Examples
8
+ # # double numeric values
9
+ # recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3]){|v| v.kind_of?(Numeric) ? v*2 : v}
10
+ # # => {:a=>22, :b=>44, :c=>{:x=>200,:y=>400}, :d=>[2,4,6]}
11
+ #
12
+ # # convert hashes to OpenStructs
13
+ # recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}){|v| v.kind_of?(Hash) ? OpenStruct.new(v) : v}
14
+ # # => #<OpenStruct b=22, a=11, c=#<OpenStruct x=100, y=200>>
15
+ def recursive_map(container, &blk)
16
+ raise ArgumentError, "recursive_map requires a block" if blk.nil?
17
+ case container
18
+ when Hash
19
+ blk[container.map{|k,v|
20
+ [k, recursive_map(v, &blk)]
21
+ }.to_h]
22
+ when Array
23
+ blk[container.map{|v| recursive_map(v, &blk)}]
24
+ else
25
+ blk[container]
26
+ end
27
+ end
28
+
29
+ end
data/lib/modalsupport.rb CHANGED
@@ -10,5 +10,6 @@ require 'modalsupport/array'
10
10
  module ModalSupport
11
11
  end
12
12
 
13
+ require 'modalsupport/recursive_map'
13
14
  require 'modalsupport/mixins/state_equivalent'
14
15
  require 'modalsupport/mixins/bracket_constructor'
data/modalsupport.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{modalsupport}
8
- s.version = "0.7.1"
8
+ s.version = "0.8.0"
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 = %q{2011-02-08}
12
+ s.date = %q{2011-02-21}
13
13
  s.description = %q{additional support extensions to ActiveSupport and HoboSupport}
14
14
  s.email = %q{jgoizueta@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "lib/modalsupport/full.rb",
31
31
  "lib/modalsupport/mixins/bracket_constructor.rb",
32
32
  "lib/modalsupport/mixins/state_equivalent.rb",
33
+ "lib/modalsupport/recursive_map.rb",
33
34
  "lib/modalsupport/regexp.rb",
34
35
  "lib/modalsupport/ruby_engine.rb",
35
36
  "lib/modalsupport/ruby_platform.rb",
@@ -38,6 +39,7 @@ Gem::Specification.new do |s|
38
39
  "modalsupport.gemspec",
39
40
  "test/helper.rb",
40
41
  "test/test_array_index.rb",
42
+ "test/test_array_to_h.rb",
41
43
  "test/test_bracket_constructor.rb",
42
44
  "test/test_cross_each.rb",
43
45
  "test/test_grep.rb",
@@ -45,6 +47,7 @@ Gem::Specification.new do |s|
45
47
  "test/test_match.rb",
46
48
  "test/test_paralell_each.rb",
47
49
  "test/test_product.rb",
50
+ "test/test_recursive_map.rb",
48
51
  "test/test_relative_path.rb",
49
52
  "test/test_rotate.rb",
50
53
  "test/test_slice.rb",
@@ -59,6 +62,7 @@ Gem::Specification.new do |s|
59
62
  s.test_files = [
60
63
  "test/helper.rb",
61
64
  "test/test_array_index.rb",
65
+ "test/test_array_to_h.rb",
62
66
  "test/test_bracket_constructor.rb",
63
67
  "test/test_cross_each.rb",
64
68
  "test/test_grep.rb",
@@ -66,6 +70,7 @@ Gem::Specification.new do |s|
66
70
  "test/test_match.rb",
67
71
  "test/test_paralell_each.rb",
68
72
  "test/test_product.rb",
73
+ "test/test_recursive_map.rb",
69
74
  "test/test_relative_path.rb",
70
75
  "test/test_rotate.rb",
71
76
  "test/test_slice.rb",
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ class TestArrayIndex < Test::Unit::TestCase
4
+
5
+ context "Arrays" do
6
+
7
+ should "have an to_h method converting key-value pairs to a hash" do
8
+ assert_respond_to [], :to_h
9
+ assert_equal({:a=>11, :b=>22}, [[:a,11], [:b,22]].to_h)
10
+ assert_equal({:a=>11, :b=>[22,33]}, [[:a,11], [:b,22,33]].to_h)
11
+ assert_equal({1=>nil, 2=>nil, 3=>nil}, [1,2,3].to_h)
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,76 @@
1
+ require 'helper'
2
+
3
+ class TestRecursiveMap < Test::Unit::TestCase
4
+
5
+ context "recursive_map" do
6
+
7
+ setup do
8
+ @value_doubler = lambda{|v| v.kind_of?(Numeric) ? v*2 : v}
9
+ @hash_to_array = lambda{|v| v.kind_of?(Hash) ? v.to_a : v}
10
+ @array_to_string = lambda{|v| v.kind_of?(Array) ? v.inspect : v}
11
+ end
12
+
13
+ should "process values in containers" do
14
+ assert_equal(
15
+ {:a=>22, :b=>44},
16
+ ModalSupport.recursive_map(:a=>11, :b=>22, &@value_doubler)
17
+ )
18
+ assert_equal(
19
+ [22,44],
20
+ ModalSupport.recursive_map([11,22], &@value_doubler)
21
+ )
22
+ end
23
+
24
+ should "process values in nested containers" do
25
+ assert_equal(
26
+ {:a=>22, :b=>44, :c=>{:x=>200,:y=>400}, :d=>[2,4,6]},
27
+ ModalSupport.recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3], &@value_doubler)
28
+ )
29
+ assert_equal(
30
+ [22, 44, {:c=>{:x=>200,:y=>400}, :d=>[2,4,6]}],
31
+ ModalSupport.recursive_map([11, 22, {:c=>{:x=>100,:y=>200}, :d=>[1,2,3]}], &@value_doubler)
32
+ )
33
+ end
34
+
35
+ should "process values in deeply nested containers" do
36
+ assert_equal(
37
+ {:a=>22, :b=>44, :c=>{:x=>200, :y=>400}, :d=>[2, 4, 6, {:zz=>[2000, 4000, {:yy=>{:xx=>6000}}]}]},
38
+ ModalSupport.recursive_map({:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3,{:zz=>[1000,2000,{:yy=>{:xx=>3000}}]}]}, &@value_doubler)
39
+ )
40
+ assert_equal(
41
+ [22, 44, {:d=>[2, 4, 6, {:zz=>[2000, 4000, {:yy=>{:xx=>6000}}]}], :b=>44, :c=>{:x=>200, :y=>400}, :a=>22}],
42
+ ModalSupport.recursive_map([11,22,{:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3,{:zz=>[1000,2000,{:yy=>{:xx=>3000}}]}]}], &@value_doubler)
43
+ )
44
+ end
45
+
46
+ should "transform containers" do
47
+ assert_same_elements(
48
+ [[:a, 11], [:b, 22], [:d, [1, 2, 3]]],
49
+ ModalSupport.recursive_map(:a=>11, :b=>22, :d=>[1,2,3], &@hash_to_array)
50
+ )
51
+ assert_equal(
52
+ {:a=>11, :b=>22, :d=>[1,2,3].inspect},
53
+ ModalSupport.recursive_map(:a=>11, :b=>22, :d=>[1,2,3], &@array_to_string)
54
+ )
55
+ assert_same_elements(
56
+ [[:a, 11], [:b, 22], [:c, [[:x, 100], [:y, 200]]], [:d, [1, 2, 3]]],
57
+ ModalSupport.recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3], &@hash_to_array)
58
+ )
59
+ assert_equal(
60
+ {:a=>11, :b=>22, :c=>{:x=>100, :y=>200}, :d=>[1,2,3].inspect},
61
+ ModalSupport.recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3], &@array_to_string)
62
+ )
63
+ ruby_19 do
64
+ # we only test this in Ruby 1.9 because prior versions don't keep Hash ordering
65
+ assert_equal(
66
+ "[111, 222, {:a=>11, :b=>22, :c=>{:x=>100, :y=>200}, :d=>\"[1, 2, 3]\"}]",
67
+ ModalSupport.recursive_map([111,222,{:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3]}], &@array_to_string)
68
+ )
69
+ end
70
+ end
71
+
72
+
73
+ end
74
+
75
+ end
76
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modalsupport
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 63
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 7
9
- - 1
10
- version: 0.7.1
8
+ - 8
9
+ - 0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Goizueta
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-08 00:00:00 +01:00
18
+ date: 2011-02-21 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -55,6 +55,7 @@ files:
55
55
  - lib/modalsupport/full.rb
56
56
  - lib/modalsupport/mixins/bracket_constructor.rb
57
57
  - lib/modalsupport/mixins/state_equivalent.rb
58
+ - lib/modalsupport/recursive_map.rb
58
59
  - lib/modalsupport/regexp.rb
59
60
  - lib/modalsupport/ruby_engine.rb
60
61
  - lib/modalsupport/ruby_platform.rb
@@ -63,6 +64,7 @@ files:
63
64
  - modalsupport.gemspec
64
65
  - test/helper.rb
65
66
  - test/test_array_index.rb
67
+ - test/test_array_to_h.rb
66
68
  - test/test_bracket_constructor.rb
67
69
  - test/test_cross_each.rb
68
70
  - test/test_grep.rb
@@ -70,6 +72,7 @@ files:
70
72
  - test/test_match.rb
71
73
  - test/test_paralell_each.rb
72
74
  - test/test_product.rb
75
+ - test/test_recursive_map.rb
73
76
  - test/test_relative_path.rb
74
77
  - test/test_rotate.rb
75
78
  - test/test_slice.rb
@@ -113,6 +116,7 @@ summary: simple extensions to core classes
113
116
  test_files:
114
117
  - test/helper.rb
115
118
  - test/test_array_index.rb
119
+ - test/test_array_to_h.rb
116
120
  - test/test_bracket_constructor.rb
117
121
  - test/test_cross_each.rb
118
122
  - test/test_grep.rb
@@ -120,6 +124,7 @@ test_files:
120
124
  - test/test_match.rb
121
125
  - test/test_paralell_each.rb
122
126
  - test/test_product.rb
127
+ - test/test_recursive_map.rb
123
128
  - test/test_relative_path.rb
124
129
  - test/test_rotate.rb
125
130
  - test/test_slice.rb