enumerable_mapper 0.5.0 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/enumerable_mapper/version.rb +1 -1
- data/lib/enumerable_mapper.rb +12 -0
- data/test/test_grouped_by.rb +42 -0
- metadata +3 -2
data/lib/enumerable_mapper.rb
CHANGED
@@ -23,4 +23,16 @@ module Enumerable
|
|
23
23
|
Mapper.new( self )
|
24
24
|
end
|
25
25
|
alias_method :collected, :mapped
|
26
|
+
|
27
|
+
# Group collection according to the method name or block given.
|
28
|
+
#
|
29
|
+
# Examples:
|
30
|
+
# Person.find( :all ).grouped_by( :company )
|
31
|
+
# Person.find( :all ).grouped_by{ |p| p.born_on.year }
|
32
|
+
def grouped_by( method_id=nil )
|
33
|
+
raise "No method or block given" unless method_id or block_given?
|
34
|
+
groupings = self.map{ |i| ( block_given? ) ? yield( i ) : i.send( method_id ) }.uniq
|
35
|
+
groupings.map{ |g| [ g, self.select{ |i| ( ( block_given? ) ? yield( i ) : i.send( method_id ) ) == g } ] }
|
36
|
+
end
|
37
|
+
|
26
38
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class Foo
|
4
|
+
attr_accessor :name
|
5
|
+
attr_accessor :type
|
6
|
+
def initialize( name, type )
|
7
|
+
@name, @type = name, type
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
class TestGroupedBy < Test::Unit::TestCase
|
13
|
+
|
14
|
+
def setup
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_array
|
18
|
+
[ Foo.new( 'inge', 'person' ), Foo.new( 'charlie', 'person' ), Foo.new( 'charlie', 'dog' ), Foo.new( 'oscar', 'dog' ) ]
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_by_method_id
|
22
|
+
test_array = create_array
|
23
|
+
assert_equal 2, test_array.grouped_by( :type ).length
|
24
|
+
assert_equal 3, test_array.grouped_by( :name ).length
|
25
|
+
assert_equal 'person', test_array.grouped_by( :type )[0].first
|
26
|
+
assert_equal 'dog', test_array.grouped_by( :type )[1].first
|
27
|
+
assert_equal 2, test_array.grouped_by( :type )[0].last.length
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_by_block
|
31
|
+
test_array = create_array
|
32
|
+
assert_equal 2, test_array.grouped_by{ |i| i.type }.length
|
33
|
+
assert_equal 3, test_array.grouped_by{ |i| i.name }.length
|
34
|
+
assert_equal 'PERSON', test_array.grouped_by{ |i| i.type.upcase }[0].first
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_error
|
38
|
+
test_array = create_array
|
39
|
+
assert_raises( RuntimeError ) { test_array.grouped_by }
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: enumerable_mapper
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.5.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.5.5
|
7
|
+
date: 2007-12-05 00:00:00 +01:00
|
8
8
|
summary: This gem provides a shorthand for simple .map/.collect calls on collections.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- test/test_helper.rb
|
50
50
|
test_files:
|
51
51
|
- test/test_enumerable_mapper.rb
|
52
|
+
- test/test_grouped_by.rb
|
52
53
|
- test/test_helper.rb
|
53
54
|
rdoc_options:
|
54
55
|
- --main
|