wwood-reach 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +45 -0
- data/lib/reach.rb +9 -5
- data/reach.gemspec +2 -1
- data/test/test_reach.rb +4 -0
- metadata +2 -1
data/README.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
Copyright (C) 2008 Ben J Woodcroft <donttrustben somewhere near gmail.com>
|
2
|
+
|
3
|
+
= Reach
|
4
|
+
|
5
|
+
Reach is a small Ruby liby that extends Array so they are more transparent
|
6
|
+
to methods. For instance, a ReachableArray of Book objects can not only take normal Array methods such as
|
7
|
+
collect and sum, but also methods that operate Book objects, such as author and title.
|
8
|
+
|
9
|
+
== Example
|
10
|
+
|
11
|
+
Say I have an regular ActiveRecord driven database, that has Bookshelf, Book, and Author objects. Bookshelves have many books, and books have many authors.
|
12
|
+
|
13
|
+
Say I want all the books.
|
14
|
+
|
15
|
+
% Book.all
|
16
|
+
|
17
|
+
Then I want all the bookshelves from those books. A common way to do this is to use the collect method of arrays:
|
18
|
+
|
19
|
+
% Book.all.collect{|book| book.bookshelf} => array of bookshelves.
|
20
|
+
|
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.
|
22
|
+
|
23
|
+
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
|
+
|
25
|
+
% require 'reach'
|
26
|
+
% Book.all.reach => ReachableArray of books
|
27
|
+
|
28
|
+
Now getting the corresponding array of bookshelves requires less typing:
|
29
|
+
|
30
|
+
% require 'reach'
|
31
|
+
% Book.all.reach.bookshelf => ReachableArray of Bookshelf objects
|
32
|
+
|
33
|
+
Notice that a ReachableArray is returned, not an Array. This means you can chain reaches together:
|
34
|
+
|
35
|
+
% require 'reach'
|
36
|
+
% Author.all.reach.book.bookshelf => ReachableArray of Bookshelf objects
|
37
|
+
|
38
|
+
Removing reachability from the array, or retracting, is equally as simple - just use the retract method:
|
39
|
+
|
40
|
+
% require 'reach'
|
41
|
+
% Book.all.reach.bookshelf.retract => Array of Bookshelf objects
|
42
|
+
|
43
|
+
== Caution
|
44
|
+
|
45
|
+
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
@@ -2,7 +2,7 @@
|
|
2
2
|
#
|
3
3
|
# This program is free software.
|
4
4
|
# You can distribute/modify this program under the terms of
|
5
|
-
# the GNU
|
5
|
+
# the GNU General Public License version 3.
|
6
6
|
|
7
7
|
# The point of this is that the
|
8
8
|
# Bookshelf.all.reach.books.each do {|book| puts book.name}
|
@@ -30,9 +30,8 @@ class ReachingArray
|
|
30
30
|
@retract = retract
|
31
31
|
end
|
32
32
|
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
33
|
+
# The method could be missing from the array, the members, or both.
|
34
|
+
# Try to pass the method to each, in that order, accepting the first taker
|
36
35
|
def method_missing(method, *args, &block)
|
37
36
|
if @retract.respond_to?(method)
|
38
37
|
# If this is an array method, just use that
|
@@ -50,8 +49,13 @@ class ReachingArray
|
|
50
49
|
end
|
51
50
|
end
|
52
51
|
|
53
|
-
#
|
52
|
+
# Equality test - equality of the underlying retract
|
53
|
+
# arrays is all that matter
|
54
54
|
def ==(another)
|
55
55
|
@retract <=> another.retract
|
56
56
|
end
|
57
|
+
|
58
|
+
def to_s
|
59
|
+
@retract.to_s
|
60
|
+
end
|
57
61
|
end
|
data/reach.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "reach"
|
3
|
-
s.version = "0.1"
|
3
|
+
s.version = "0.1.1"
|
4
4
|
s.date = "2008-09-05"
|
5
5
|
s.summary = "Extend the Ruby Array class for less loops and blocks"
|
6
6
|
s.email = "donttrustben somewhere near gmail.com"
|
@@ -8,6 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.description = "Extend the Ruby Array class for less loops and blocks."
|
9
9
|
s.authors = ["Ben J Woodcroft"]
|
10
10
|
s.files = ["reach.gemspec",
|
11
|
+
"README.rdoc",
|
11
12
|
"lib/reach.rb"]
|
12
13
|
s.test_files = ["test/test_reach.rb"]
|
13
14
|
end
|
data/test/test_reach.rb
CHANGED
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:
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben J Woodcroft
|
@@ -23,6 +23,7 @@ extra_rdoc_files: []
|
|
23
23
|
|
24
24
|
files:
|
25
25
|
- reach.gemspec
|
26
|
+
- README.rdoc
|
26
27
|
- lib/reach.rb
|
27
28
|
has_rdoc: false
|
28
29
|
homepage: http://github.com/wwood/reach
|