map_by_method 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/lib/map_by_method.rb +22 -34
- data/lib/map_by_method/version.rb +1 -1
- data/test/test_helper.rb +8 -0
- data/test/test_map_by_method.rb +28 -0
- data/website/index.html +1 -1
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 0.8.0 2007-09-05
|
2
|
+
|
3
|
+
* Works in Rails - respond_to? must return true or false, or rather, cannot return MatchData [thx Mislav Marohnić]
|
4
|
+
* Additional mapper methods: sort_by, and group_by, index_by (in activesupport gem)
|
5
|
+
|
1
6
|
== 0.7.0 2007-08-11
|
2
7
|
|
3
8
|
* ActiveRecord AssociationProxy support (company.users.map_by_name now works)
|
data/lib/map_by_method.rb
CHANGED
@@ -1,50 +1,38 @@
|
|
1
1
|
module MapByMethod
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
MAP_BY_METHOD_FORMAT = /^(map|collect|select|each|reject|sort_by|group_by|index_by)(?:_by)?_(\w+[?!]?)$/
|
3
|
+
|
4
|
+
module InstanceMethods
|
5
|
+
alias_method :respond_to_before_map_by_method?, :respond_to?
|
6
|
+
|
7
|
+
def respond_to?(method)
|
8
|
+
respond_to_before_map_by_method?(method) or
|
9
|
+
!!method.to_s.match(MAP_BY_METHOD_FORMAT)
|
5
10
|
end
|
6
|
-
end
|
7
11
|
|
8
|
-
|
12
|
+
protected
|
13
|
+
|
9
14
|
alias_method :method_missing_before_map_by_method, :method_missing
|
10
15
|
|
11
16
|
def method_missing(method, *args, &block)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
iterator
|
18
|
-
callmethods = callmethod.split('_and_')
|
19
|
-
result = callmethods.collect do |callmethod|
|
20
|
-
self.send(iterator) {|item| item.send callmethod}
|
21
|
-
end
|
22
|
-
return callmethods.length == 1 ?
|
23
|
-
result.first :
|
24
|
-
result.transpose
|
17
|
+
if !respond_to_before_map_by_method?(method) and
|
18
|
+
(matches = method.to_s.match(MAP_BY_METHOD_FORMAT))
|
19
|
+
iterator, callmethod = matches[1..2]
|
20
|
+
# map by multiple stuff
|
21
|
+
result = callmethod.split('_and_').collect do |method|
|
22
|
+
self.send(iterator) { |item| item.send method }
|
25
23
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
nil
|
24
|
+
|
25
|
+
return result.length > 1 ? result.transpose : result.first
|
26
|
+
else
|
27
|
+
method_missing_before_map_by_method(method, *args, &block)
|
31
28
|
end
|
32
|
-
raise error
|
33
|
-
end
|
34
|
-
|
35
|
-
alias_method :"respond_to_before_map_by_method?", :"respond_to?"
|
36
|
-
def respond_to?(method)
|
37
|
-
return true if respond_to_before_map_by_method?(method)
|
38
|
-
method.to_s.match(self.class.map_by_method_regex)
|
39
29
|
end
|
40
30
|
end
|
41
31
|
|
42
32
|
def self.included(base)
|
43
33
|
super
|
44
|
-
base.
|
45
|
-
base.send(:include, InstanceMethods)
|
34
|
+
base.send :include, InstanceMethods
|
46
35
|
end
|
47
|
-
|
48
36
|
end
|
49
37
|
|
50
|
-
Array.send :include, MapByMethod
|
38
|
+
Array.send :include, MapByMethod
|
data/test/test_helper.rb
CHANGED
data/test/test_map_by_method.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
4
|
class TestMapByMethod < Test::Unit::TestCase
|
4
5
|
|
@@ -27,4 +28,31 @@ class TestMapByMethod < Test::Unit::TestCase
|
|
27
28
|
assert_equal ['1','3'], ['1','','3'].select_any?
|
28
29
|
assert_equal ['1','3'], ['1','','3'].select_by_any?
|
29
30
|
end
|
31
|
+
|
32
|
+
def test_sort_by
|
33
|
+
original = [OpenStruct.new(:name => "ZZZ"), OpenStruct.new(:name => "AAA")]
|
34
|
+
expected = [OpenStruct.new(:name => "AAA"), OpenStruct.new(:name => "ZZZ")]
|
35
|
+
assert_equal expected, original.sort_by_name
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_group_by
|
39
|
+
original = [
|
40
|
+
OpenStruct.new(:name => "ZZZ", :group => "letters"),
|
41
|
+
OpenStruct.new(:name => "AAA", :group => "letters")
|
42
|
+
]
|
43
|
+
expected = {"letters" => original.clone}
|
44
|
+
assert_equal expected, original.group_by_group
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_index_by
|
48
|
+
original = [
|
49
|
+
OpenStruct.new(:name => "ZZZ"),
|
50
|
+
OpenStruct.new(:name => "AAA")
|
51
|
+
]
|
52
|
+
expected = {
|
53
|
+
"ZZZ" => OpenStruct.new(:name => "ZZZ"),
|
54
|
+
"AAA" => OpenStruct.new(:name => "AAA")
|
55
|
+
}
|
56
|
+
assert_equal expected, original.index_by_name
|
57
|
+
end
|
30
58
|
end
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1 class=primary>Map By Method</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/drnicutilities"; return false'>
|
35
35
|
Get Version
|
36
|
-
<a href="http://rubyforge.org/projects/drnicutilities" class="numbers">0.
|
36
|
+
<a href="http://rubyforge.org/projects/drnicutilities" class="numbers">0.8.0</a>
|
37
37
|
</div>
|
38
38
|
<h1><code>["1","2","3"]. map_by_to_i => [1,2,3]</code></h1>
|
39
39
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4.3
|
|
3
3
|
specification_version: 1
|
4
4
|
name: map_by_method
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.8.0
|
7
|
+
date: 2007-09-05 00:00:00 +02:00
|
8
8
|
summary: Replacement for map {|obj| obj.action} and Symbol.to_proc which is much cleaner and prettier NOW WORKS with ActiveRecord Associations!!
|
9
9
|
require_paths:
|
10
10
|
- lib
|