methodchain 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 +40 -28
- data/coverage/-var-lib-gems-1_8-gems-rcov-0_8_0_2-lib-rcov_rb.html +1607 -0
- data/coverage/index.html +347 -0
- data/coverage/lib-methodchain-not_included_rb.html +689 -0
- data/coverage/lib-methodchain_rb.html +635 -0
- data/doc/created.rid +1 -1
- data/doc/fr_method_index.html +5 -5
- data/pkg/methodchain-0.2.0.gem +0 -0
- data/rakefile +74 -9
- metadata +8 -2
data/README
CHANGED
@@ -7,65 +7,77 @@ Copyright (c) 2008 Greg Weber, http://gregweber.info
|
|
7
7
|
Licensed under the MIT license
|
8
8
|
|
9
9
|
== Examples
|
10
|
-
|
10
|
+
=== ##tap
|
11
11
|
if you don't already know about this method, look it up on the net. The tap included here allows message sending.
|
12
12
|
|
13
|
-
|
13
|
+
==== old way
|
14
|
+
arr = [1]
|
15
|
+
arr.compact! # => nil
|
16
|
+
arr.first # => 1
|
17
|
+
|
18
|
+
==== normal ##tap (still valid)
|
14
19
|
[1].tap {|arr| arr.compact!}.first # => 1
|
15
|
-
NEW WAY
|
16
|
-
[1].tap(:compact!).first # => 1
|
17
20
|
|
18
|
-
====
|
19
|
-
|
21
|
+
==== new ##tap
|
22
|
+
[1].tap(:compact!).first # => 1
|
20
23
|
|
24
|
+
=== ##then and ##else
|
25
|
+
==== old way
|
26
|
+
person = nil
|
21
27
|
name = person ? person.name : nil
|
22
28
|
|
23
|
-
|
24
|
-
|
29
|
+
==== new way
|
25
30
|
name = person.then {|p| p.name}
|
26
31
|
|
27
32
|
not a huge savings. But sometimes the person variable is actually a function call, and then we must save it in a variable first.
|
28
33
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
==== old way
|
35
|
+
def find(*args)
|
36
|
+
# do some expensive database queries
|
37
|
+
end
|
33
38
|
|
34
|
-
|
39
|
+
location = find(:first)
|
40
|
+
@phone = location && location.phone # => nil
|
35
41
|
|
36
|
-
|
42
|
+
==== new way
|
43
|
+
@phone = find(:first).then {phone} # => nil
|
37
44
|
|
38
|
-
|
45
|
+
We have reduced a line of code and removed a local variable.
|
39
46
|
#then and #else can return a default value instead of evaluating a block
|
40
|
-
'a'.then('b') #=> 'b'
|
41
|
-
nil.then('b').else('c') #=> 'c'
|
42
47
|
|
43
|
-
|
44
|
-
|
48
|
+
'a'.then('b') #=> 'b'
|
49
|
+
nil.then('b').else('c') #=> 'c'
|
45
50
|
|
51
|
+
=== ##chain
|
52
|
+
==== old way
|
53
|
+
customer = nil
|
46
54
|
customer && customer.order && customer.order.id
|
47
55
|
|
48
|
-
|
49
|
-
|
56
|
+
==== new way
|
50
57
|
customer.chain(:order, :id)
|
51
58
|
|
52
59
|
note that this is equivalent to
|
53
60
|
|
54
61
|
customer.then {order}.then {id}
|
55
62
|
|
56
|
-
=== chain - Custom guards and
|
57
|
-
|
63
|
+
=== ##chain - Custom guards, multiple arguments, and procs
|
64
|
+
==== old way - guarding against zero
|
65
|
+
value = 0
|
58
66
|
|
59
67
|
result = if value == 0 then value else
|
60
|
-
tmp =
|
68
|
+
tmp = value.abs
|
61
69
|
|
62
70
|
if tmp == 0 then tmp else
|
63
|
-
|
71
|
+
tmp * 20
|
64
72
|
end
|
65
73
|
end
|
74
|
+
result # => 0
|
75
|
+
|
76
|
+
==== new way
|
77
|
+
value.chain(:abs, [:*, 20]) {|s| s == 0 } # => 0
|
66
78
|
|
67
|
-
|
68
|
-
value.chain(:
|
79
|
+
procs can be used, so this is equivalent to
|
80
|
+
value.chain(:abs, lambda {|n| n * 20 }) {|s| s == 0 } # => 0
|
69
81
|
|
70
82
|
== Usage
|
71
83
|
require 'rubygems'
|
@@ -83,7 +95,7 @@ Already have your own version of tap? use the module-import gem to decide what t
|
|
83
95
|
|
84
96
|
== Implementation
|
85
97
|
* There are no proxy objects and no use of method_missing- these are simply function calls, so it should be fast.
|
86
|
-
* A helper method called
|
98
|
+
* A helper method called yield_or_eval is also exposed. This method allows the two different block forms {|p| p.name} and {name}, where the first form yields self and the second form is called using instance_eval.
|
87
99
|
|
88
100
|
== Install
|
89
101
|
gem install methodchain
|