wwood-reach 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/reach.rb +16 -4
- data/reach.gemspec +1 -1
- data/test/test_reach.rb +6 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -44,7 +44,7 @@ Removing reachability from the array, or retracting, is equally as simple - just
|
|
44
44
|
|
45
45
|
=== Slap
|
46
46
|
|
47
|
-
Like reach
|
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
48
|
|
49
49
|
% require 'reach'
|
50
50
|
% [[1,2,3],[4]].slap.length.retract => [1,3]
|
data/lib/reach.rb
CHANGED
@@ -47,7 +47,13 @@ class ReachingArray
|
|
47
47
|
# array elements in place as we want, so have to use collect
|
48
48
|
# instead.
|
49
49
|
@retract = @retract.collect do |o|
|
50
|
-
o.
|
50
|
+
unless o.kind_of?(Array)
|
51
|
+
o.send(method, *args, &block)
|
52
|
+
else
|
53
|
+
# Update in 0.2.1: If the element of the array is an array
|
54
|
+
# itself, then operate on it as a ReachingArray as well.
|
55
|
+
o.reach.send(method, *args, &block).retract
|
56
|
+
end
|
51
57
|
end
|
52
58
|
return self
|
53
59
|
end
|
@@ -60,7 +66,7 @@ class ReachingArray
|
|
60
66
|
end
|
61
67
|
|
62
68
|
def to_s
|
63
|
-
|
69
|
+
method_missing(:to_s)
|
64
70
|
end
|
65
71
|
|
66
72
|
def slap
|
@@ -80,7 +86,13 @@ class SlappingArray
|
|
80
86
|
# Try to pass the method to each of the array members
|
81
87
|
def method_missing(method, *args, &block)
|
82
88
|
@retract = @retract.collect do |o|
|
83
|
-
o.
|
89
|
+
unless o.kind_of?(Array)
|
90
|
+
o.send(method, *args, &block)
|
91
|
+
else
|
92
|
+
# Update in 0.2.1: If the element of the array is an array
|
93
|
+
# itself, then operate on it as a ReachingArray as well.
|
94
|
+
o.slap.send(method, *args, &block).retract
|
95
|
+
end
|
84
96
|
end
|
85
97
|
return self
|
86
98
|
end
|
@@ -92,7 +104,7 @@ class SlappingArray
|
|
92
104
|
end
|
93
105
|
|
94
106
|
def to_s
|
95
|
-
|
107
|
+
method_missing(:to_s)
|
96
108
|
end
|
97
109
|
|
98
110
|
def reach
|
data/reach.gemspec
CHANGED
data/test/test_reach.rb
CHANGED
@@ -59,6 +59,11 @@ class ReachTest < Test::Unit::TestCase
|
|
59
59
|
def test_to_s
|
60
60
|
assert_equal [1,2].to_s, [1,2].reach.to_s
|
61
61
|
end
|
62
|
+
|
63
|
+
def test_recursive
|
64
|
+
assert_equal [1,[2,3],[[5]]],
|
65
|
+
['1',['2','3'],[['5']]].reach.to_i.retract
|
66
|
+
end
|
62
67
|
end
|
63
68
|
|
64
69
|
class SlapTest < Test::Unit::TestCase
|
@@ -70,7 +75,7 @@ class SlapTest < Test::Unit::TestCase
|
|
70
75
|
def test_simple
|
71
76
|
assert_kind_of SlappingArray, @two_level.slap
|
72
77
|
|
73
|
-
assert_equal [3,
|
78
|
+
assert_equal [['1','2','3'],['5']], @two_level.slap.to_s.retract
|
74
79
|
end
|
75
80
|
|
76
81
|
def test_mutate
|