wwood-reach 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/reach.rb +57 -0
- data/reach.gemspec +14 -0
- data/test/test_reach.rb +58 -0
- metadata +54 -0
data/lib/reach.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Copyright (c) 2008 Ben Woodcroft <donttrustben somewhere near gmail.com>
|
2
|
+
#
|
3
|
+
# This program is free software.
|
4
|
+
# You can distribute/modify this program under the terms of
|
5
|
+
# the GNU Lesser General Public License version 3.
|
6
|
+
|
7
|
+
# The point of this is that the
|
8
|
+
# Bookshelf.all.reach.books.each do {|book| puts book.name}
|
9
|
+
# instead of
|
10
|
+
# Bookshelf.all.each do |bookshelf|
|
11
|
+
# bookshelf.books.each do |book|
|
12
|
+
# puts book.name
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
|
16
|
+
# Bookshelf.all.reach is a ReachableArray
|
17
|
+
# the books is an array (bookshelves) of arrays (books)
|
18
|
+
class Array
|
19
|
+
def reach
|
20
|
+
ReachingArray.new(self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class ReachingArray
|
25
|
+
# The array that this reaching array is extending. This might
|
26
|
+
# be a real Array, or a ReachingArray
|
27
|
+
attr_accessor :retract
|
28
|
+
|
29
|
+
def initialize(retract)
|
30
|
+
@retract = retract
|
31
|
+
end
|
32
|
+
|
33
|
+
# When a method is called on an array and that method
|
34
|
+
# isn't defined for an array, then run
|
35
|
+
#
|
36
|
+
def method_missing(method, *args, &block)
|
37
|
+
if @retract.respond_to?(method)
|
38
|
+
# If this is an array method, just use that
|
39
|
+
r = @retract.send(method, *args, &block)
|
40
|
+
return r
|
41
|
+
else
|
42
|
+
# If this is an object-specific method, run an each on it.
|
43
|
+
# A simple each won't work because that doesn't modify the
|
44
|
+
# array elements in place as we want, so have to use collect
|
45
|
+
# instead.
|
46
|
+
@retract = @retract.collect do |o|
|
47
|
+
o.send(method, *args, &block)
|
48
|
+
end
|
49
|
+
return self
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Mainly for testing
|
54
|
+
def ==(another)
|
55
|
+
@retract <=> another.retract
|
56
|
+
end
|
57
|
+
end
|
data/reach.gemspec
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "reach"
|
3
|
+
s.version = "0.1"
|
4
|
+
s.date = "2008-09-05"
|
5
|
+
s.summary = "Extend the Ruby Array class for less loops and blocks"
|
6
|
+
s.email = "donttrustben somewhere near gmail.com"
|
7
|
+
s.homepage = "http://github.com/wwood/reach"
|
8
|
+
s.description = "Extend the Ruby Array class for less loops and blocks."
|
9
|
+
s.authors = ["Ben J Woodcroft"]
|
10
|
+
s.files = ["reach.gemspec",
|
11
|
+
"lib/reach.rb"]
|
12
|
+
s.test_files = ["test/test_reach.rb"]
|
13
|
+
end
|
14
|
+
|
data/test/test_reach.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Copyright (c) 2008 Ben Woodcroft <donttrustben somewhere near gmail.com>
|
2
|
+
#
|
3
|
+
# This program is free software.
|
4
|
+
# You can distribute/modify this program under the terms of
|
5
|
+
# the GNU Lesser General Public License version 3.
|
6
|
+
|
7
|
+
|
8
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
9
|
+
|
10
|
+
require 'test/unit'
|
11
|
+
require 'reach'
|
12
|
+
|
13
|
+
class ReachTest < Test::Unit::TestCase
|
14
|
+
def setup
|
15
|
+
@one_level = %w(a b c)
|
16
|
+
@two_level = [[1,2,3],[5]]
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_normality
|
20
|
+
# Test arrays seem normal - this is kinda pointless I would say
|
21
|
+
index = 0
|
22
|
+
@one_level.each { |n|
|
23
|
+
assert_equal @one_level[index], n
|
24
|
+
index += 1
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_simple_method
|
29
|
+
out = @one_level.reach.succ!
|
30
|
+
assert_kind_of ReachingArray, out
|
31
|
+
assert_equal ReachingArray.new(%w(b c d)), out
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_scoping_one
|
35
|
+
assert_equal ReachingArray.new(%w(c d e)), @one_level.reach.succ!.succ!
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_collect
|
39
|
+
assert_equal %w(b c d), @one_level.reach.collect{|c| c.succ}
|
40
|
+
end
|
41
|
+
|
42
|
+
# Catches if the array elements are not modified within the reach
|
43
|
+
def test_double_scope
|
44
|
+
# make sure my assumptions are correct about the
|
45
|
+
# classes being tested
|
46
|
+
assert 1.respond_to?(:zero?)
|
47
|
+
assert_equal false, '1'.respond_to?(:zero?)
|
48
|
+
|
49
|
+
# make sure that the array elements are being reversed
|
50
|
+
str = ['0','2','3']
|
51
|
+
assert_equal [0,2,3], str.reach.to_i.retract
|
52
|
+
assert_equal [true, false, false], str.reach.to_i.zero?.retract
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_block_accepted
|
56
|
+
assert_equal %w(d e f), @one_level.reach.succ.collect{|c| c.succ.succ}
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wwood-reach
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben J Woodcroft
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-09-05 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Extend the Ruby Array class for less loops and blocks.
|
17
|
+
email: donttrustben somewhere near gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- reach.gemspec
|
26
|
+
- lib/reach.rb
|
27
|
+
has_rdoc: false
|
28
|
+
homepage: http://github.com/wwood/reach
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: "0"
|
39
|
+
version:
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
requirements: []
|
47
|
+
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.2.0
|
50
|
+
signing_key:
|
51
|
+
specification_version: 2
|
52
|
+
summary: Extend the Ruby Array class for less loops and blocks
|
53
|
+
test_files:
|
54
|
+
- test/test_reach.rb
|