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 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
- module ClassMethods
3
- def map_by_method_regex
4
- /(map|collect|select|each|reject)(_by)?_([\w\_]+\??)/
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
- module InstanceMethods
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
- method_missing_before_map_by_method(method, *args, &block)
13
- rescue NoMethodError
14
- error = $!
15
- begin
16
- if (match = method.to_s.match(self.class.map_by_method_regex))
17
- iterator, callmethod = match[1], match[3]
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
- method_name = method.to_s
27
- @@support_singularize ||= method_name.methods.include? "singularize"
28
- return self.map {|item| item.send method_name.singularize.to_sym} if @@support_singularize
29
- rescue NoMethodError
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.extend(ClassMethods)
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
@@ -1,7 +1,7 @@
1
1
  module MapByMethod #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 7
4
+ MINOR = 8
5
5
  TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
data/test/test_helper.rb CHANGED
@@ -1,2 +1,10 @@
1
1
  require 'test/unit'
2
2
  require File.dirname(__FILE__) + '/../lib/map_by_method'
3
+
4
+ begin
5
+ require 'active_support'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ gem 'activesupport'
9
+ require 'active_support'
10
+ end
@@ -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.7.0</a>
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 =&gt; [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.0
7
- date: 2007-08-12 00:00:00 +02:00
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