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 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
- ==== tap
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
- OLD WAY (still valid with this tap)
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
- ==== #then and #else
19
- OLD WAY
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
- NEW WaY
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
- OLD WAY
30
-
31
- location = Location.find(:first, ...)
32
- @phone = location && location.phone
34
+ ==== old way
35
+ def find(*args)
36
+ # do some expensive database queries
37
+ end
33
38
 
34
- NEW WaY
39
+ location = find(:first)
40
+ @phone = location && location.phone # => nil
35
41
 
36
- @phone = Location.find(:first, ...).then {phone}
42
+ ==== new way
43
+ @phone = find(:first).then {phone} # => nil
37
44
 
38
- here we have reduced a line of code and removed a local variable
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
- === #chain
44
- OLD WAY
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
- NEW WaY
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 multiple argumentes
57
- OLD WAY - guarding against zero
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 = calc(value)
68
+ tmp = value.abs
61
69
 
62
70
  if tmp == 0 then tmp else
63
- more_calc(tmp, 20)
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
- NEW WAY
68
- value.chain(:calc, [:more_calc, 20]) {|s| s == 0 }
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 self_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.
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