wwood-reach 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,8 +1,10 @@
1
1
  Copyright (C) 2008 Ben J Woodcroft <donttrustben somewhere near gmail.com>
2
2
 
3
+ This software is BETA! Use at your own risk.
4
+
3
5
  = Reach
4
6
 
5
- Reach is a small Ruby liby that extends Array so they are more transparent
7
+ Reach is a small Ruby liby that extends the Array class so that Arrays are more transparent
6
8
  to methods. For instance, a ReachableArray of Book objects can not only take normal Array methods such as
7
9
  collect and sum, but also methods that operate Book objects, such as author and title.
8
10
 
@@ -14,11 +16,11 @@ Say I want all the books.
14
16
 
15
17
  % Book.all
16
18
 
17
- Then I want all the bookshelves from those books. A common way to do this is to use the collect method of arrays:
19
+ Then I want to find all the bookshelves from those that contain those books.
18
20
 
19
21
  % Book.all.collect{|book| book.bookshelf} => array of bookshelves.
20
22
 
21
- From personal experience, running a single method on each array element and collecting the results is quite common, especially in Rails. The idea, then, is to make the arrays not just responsive to the methods that apply to the Array itself, but to methods that apply to the members of that array. That is, make the array reachable (caution: made up word). In the case above we want to run the 'bookshelf' method on each of the book objects in the array.
23
+ From personal experience, running a single method on each array element and collecting the results is quite common, especially in Rails. The idea, then, is to make the arrays not just responsive to the methods that apply to the Array itself, but to methods that apply to the +members+ of that array. That is, make the array reachable (caution: made up word). In the case above we want to run the 'bookshelf' method on each of the book objects in the array.
22
24
 
23
25
  To make an array reachable, convert it into a ReachableArray object, by calling the reach method of Array, which is added by the inclusion of the reach library.
24
26
 
@@ -30,7 +32,7 @@ Now getting the corresponding array of bookshelves requires less typing:
30
32
  % require 'reach'
31
33
  % Book.all.reach.bookshelf => ReachableArray of Bookshelf objects
32
34
 
33
- Notice that a ReachableArray is returned, not an Array. This means you can chain reaches together:
35
+ Notice that a ReachableArray is returned by the bookshelf method, not an Array. This means you can chain reaches together:
34
36
 
35
37
  % require 'reach'
36
38
  % Author.all.reach.book.bookshelf => ReachableArray of Bookshelf objects
@@ -40,6 +42,13 @@ Removing reachability from the array, or retracting, is equally as simple - just
40
42
  % require 'reach'
41
43
  % Book.all.reach.bookshelf.retract => Array of Bookshelf objects
42
44
 
45
+ === Slap
46
+
47
+ Like reach. the slap method is another method added to the Array class to make it mroe transparent. The difference is that reach looks to see if the given method operates on the Array class, and if not then applies it to each member of that array. The slap method bypasses the first step, and just applies the given method to each member. For instance
48
+
49
+ % require 'reach'
50
+ % [[1,2,3],[4]].slap.length.retract => [1,3]
51
+
43
52
  == Caution
44
53
 
45
54
  When operating on a ReachableArray, methods that operate on the Array itself take precedence over methods that apply to array members.
data/lib/reach.rb CHANGED
@@ -19,6 +19,10 @@ class Array
19
19
  def reach
20
20
  ReachingArray.new(self)
21
21
  end
22
+
23
+ def slap
24
+ SlappingArray.new(self)
25
+ end
22
26
  end
23
27
 
24
28
  class ReachingArray
@@ -58,4 +62,40 @@ class ReachingArray
58
62
  def to_s
59
63
  @retract.to_s
60
64
  end
65
+
66
+ def slap
67
+ retract.slap
68
+ end
69
+ end
70
+
71
+ class SlappingArray
72
+ # The array that this reaching array is extending. This might
73
+ # be a real Array, or a ReachingArray
74
+ attr_accessor :retract
75
+
76
+ def initialize(retract)
77
+ @retract = retract
78
+ end
79
+
80
+ # Try to pass the method to each of the array members
81
+ def method_missing(method, *args, &block)
82
+ @retract = @retract.collect do |o|
83
+ o.send(method, *args, &block)
84
+ end
85
+ return self
86
+ end
87
+
88
+ # Equality test - equality of the underlying retract
89
+ # arrays is all that matter
90
+ def ==(another)
91
+ @retract <=> another.retract
92
+ end
93
+
94
+ def to_s
95
+ @retract.to_s
96
+ end
97
+
98
+ def reach
99
+ retract.reach
100
+ end
61
101
  end
data/reach.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "reach"
3
- s.version = "0.1.1"
4
- s.date = "2008-09-05"
3
+ s.version = "0.2.0"
4
+ s.date = "2008-10-23"
5
5
  s.summary = "Extend the Ruby Array class for less loops and blocks"
6
6
  s.email = "donttrustben somewhere near gmail.com"
7
7
  s.homepage = "http://github.com/wwood/reach"
data/test/test_reach.rb CHANGED
@@ -60,3 +60,28 @@ class ReachTest < Test::Unit::TestCase
60
60
  assert_equal [1,2].to_s, [1,2].reach.to_s
61
61
  end
62
62
  end
63
+
64
+ class SlapTest < Test::Unit::TestCase
65
+ def setup
66
+ @one_level = %w(a b c)
67
+ @two_level = [[1,2,3],[5]]
68
+ end
69
+
70
+ def test_simple
71
+ assert_kind_of SlappingArray, @two_level.slap
72
+
73
+ assert_equal [3,1], @two_level.slap.length.retract
74
+ end
75
+
76
+ def test_mutate
77
+ assert_kind_of ReachingArray, @two_level.slap.reach
78
+ assert_equal [[1,2,3],[5]], @two_level.slap.reach.retract
79
+ end
80
+
81
+ def test_array_method_fail
82
+ assert_raise NoMethodError do
83
+ @one_level.slap.join(' ')
84
+ end
85
+ end
86
+
87
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wwood-reach
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben J Woodcroft
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-05 00:00:00 -07:00
12
+ date: 2008-10-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15