map_by_method 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/map_by_method/version.rb +2 -2
- data/lib/map_by_method.rb +7 -1
- data/test/multiple_methods_test.rb +23 -0
- metadata +5 -4
data/lib/map_by_method.rb
CHANGED
@@ -11,7 +11,13 @@ module MapByMethod
|
|
11
11
|
re = /(map|collect|select|each|reject)(_by)?_([\\w\\_]+\\??)/
|
12
12
|
if (match = method.to_s.match(re))
|
13
13
|
iterator, callmethod = match[1], match[3]
|
14
|
-
|
14
|
+
callmethods = callmethod.split('_and_')
|
15
|
+
result = callmethods.collect do |callmethod|
|
16
|
+
self.send(iterator) {|item| item.send callmethod}
|
17
|
+
end
|
18
|
+
return callmethods.length == 1 ?
|
19
|
+
result.first :
|
20
|
+
result.transpose
|
15
21
|
end
|
16
22
|
return self.map {|item| item.send method.to_s.singularize.to_sym}
|
17
23
|
rescue NoMethodError
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
class MapByMethodTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@data = [
|
8
|
+
OpenStruct.new(:name => 'Dr Nic', :amount => 300),
|
9
|
+
OpenStruct.new(:name => 'Banjo', :amount => 500)
|
10
|
+
]
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_map_normal
|
14
|
+
assert_equal ['Dr Nic','Banjo'], @data.map_name
|
15
|
+
assert_equal [300,500], @data.map_amount
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_map_ands
|
19
|
+
assert_equal [['Dr Nic',300],['Banjo',500]], @data.map_name_and_amount
|
20
|
+
assert_equal [[300,'Dr Nic'],[500,'Banjo']], @data.map_amount_and_name
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: map_by_method
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2006-10-
|
6
|
+
version: 0.5.0
|
7
|
+
date: 2006-10-26 00:00:00 +02:00
|
8
8
|
summary: Replacement for Symbol.to_proc which is much cleaner and prettier
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -32,10 +32,11 @@ files:
|
|
32
32
|
- CHANGELOG
|
33
33
|
- Rakefile
|
34
34
|
- test/all_tests.rb
|
35
|
-
- test/test_helper.rb
|
36
35
|
- test/map_by_method_test.rb
|
37
|
-
-
|
36
|
+
- test/multiple_methods_test.rb
|
37
|
+
- test/test_helper.rb
|
38
38
|
- lib/map_by_method
|
39
|
+
- lib/map_by_method.rb
|
39
40
|
- lib/map_by_method/version.rb
|
40
41
|
test_files: []
|
41
42
|
|