ick 0.0.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -1
- data/lib/ick.rb +3 -2
- data/lib/ick/version.rb +2 -2
- data/test/test_helper.rb +1 -4
- data/test/test_ick.rb +11 -0
- data/website/index.html +6 -6
- data/website/index.txt +4 -4
- metadata +1 -1
data/History.txt
CHANGED
@@ -3,5 +3,9 @@
|
|
3
3
|
* 1 major enhancement:
|
4
4
|
* Initial super-alpha release
|
5
5
|
* Not particularly good at nesting
|
6
|
-
*
|
6
|
+
* 2 minor enhancement:
|
7
7
|
* Changed #with to #inside avoid confusion with Object#with
|
8
|
+
* 3 minor enhancement:
|
9
|
+
* refactored Wrapped into Wrap with runtime options
|
10
|
+
* added ArrayWrapper and Tee classes
|
11
|
+
|
data/lib/ick.rb
CHANGED
data/lib/ick/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,7 +1,4 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require File.dirname(__FILE__) + '/../lib/ick
|
3
|
-
require File.dirname(__FILE__) + '/../lib/ick/wrapped'
|
4
|
-
require File.dirname(__FILE__) + '/../lib/ick/guarded'
|
5
|
-
require File.dirname(__FILE__) + '/../lib/ick/sugar'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/ick'
|
6
3
|
|
7
4
|
Ick.sugarize
|
data/test/test_ick.rb
CHANGED
@@ -125,4 +125,15 @@ class TestIck < Test::Unit::TestCase
|
|
125
125
|
)
|
126
126
|
end
|
127
127
|
|
128
|
+
def test_tee
|
129
|
+
|
130
|
+
box1 = Box.new(1)
|
131
|
+
box2 = Box.new(2)
|
132
|
+
|
133
|
+
tee(box1,box2) { |box| box.value = 7 }
|
134
|
+
assert_equal(7, box1.value)
|
135
|
+
assert_equal(7, box2.value)
|
136
|
+
|
137
|
+
end
|
138
|
+
|
128
139
|
end
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>Invocation Construction Kit</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/ick"; return false'>
|
35
35
|
<p>Get Version</p>
|
36
|
-
<a href="http://rubyforge.org/projects/ick" class="numbers">0.0
|
36
|
+
<a href="http://rubyforge.org/projects/ick" class="numbers">0.2.0</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘ick’</h1>
|
39
39
|
|
@@ -77,20 +77,20 @@
|
|
77
77
|
<p>This code makes it clear that you only need the <code>person</code> variable inside the block. If you want to refactor this code, you know that the entire expression can move without breaking another piece of code. We’ll elaborate on the differences between #let, #returning, #my, and #inside below.</p>
|
78
78
|
|
79
79
|
|
80
|
-
<p>(The four methods were inspired by <a href="http://blog.rubyenrails.nl/articles/2008/02/18/our-daily-method-10-object-r-rs-ds-s">Michiel de Mare’s post on the same subject</a>, although Ick’s nomenclature is not compatible with Michiel’s. Michiel’s #
|
80
|
+
<p>(The four methods were inspired by <a href="http://blog.rubyenrails.nl/articles/2008/02/18/our-daily-method-10-object-r-rs-ds-s">Michiel de Mare’s post on the same subject</a>, although Ick’s nomenclature is not compatible with Michiel’s. Michiel’s #rsss, #rrss, #rsds, and #rrds are called #returning, #let, #inside, and #my in Ick.)</p>
|
81
81
|
|
82
82
|
|
83
83
|
<h2>Guarded Evaluation</h2>
|
84
84
|
|
85
85
|
|
86
|
-
<p>The example above is a common one. Sometimes we want to evaluate a chain of method calls without throwing a <
|
86
|
+
<p>The example above is a common one. Sometimes we want to evaluate a chain of method calls without throwing a <em>NoMethodError</em> if one of the recipients is nil. Sometimes we want to send something a message if and only if it handles the method. There are lots of ad-hoc solutions, including <a href="http://andand.rubyforge.org">Object#andand</a>. What if you don’t want to install lots of different gems, one for each use?</p>
|
87
87
|
|
88
88
|
|
89
89
|
<p>Ick solves this problem by providing a structure for rolling your own guarded evaluation. You can check for nil, #respond_to?, custom permissions, whatever you like. It looks like this:</p>
|
90
90
|
|
91
91
|
|
92
92
|
<p><pre class='syntax'>
|
93
|
-
<span class="keyword">class </span><span class="class">Try</span> <span class="punct"><</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">
|
93
|
+
<span class="keyword">class </span><span class="class">Try</span> <span class="punct"><</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">Guard</span>
|
94
94
|
<span class="ident">guard_with</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">value</span><span class="punct">,</span> <span class="ident">sym</span><span class="punct">|</span> <span class="ident">value</span><span class="punct">.</span><span class="ident">respond_to?</span><span class="punct">(</span><span class="ident">sym</span><span class="punct">)</span> <span class="punct">==</span> <span class="constant">true</span> <span class="punct">}</span>
|
95
95
|
<span class="ident">evaluates_in_calling_environment</span> <span class="keyword">and</span> <span class="ident">returns_result</span>
|
96
96
|
<span class="ident">belongs_to</span> <span class="constant">Object</span>
|
@@ -194,7 +194,7 @@
|
|
194
194
|
|
195
195
|
|
196
196
|
<p><pre class='syntax'>
|
197
|
-
<span class="keyword">class </span><span class="class">Please</span> <span class="punct"><</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">
|
197
|
+
<span class="keyword">class </span><span class="class">Please</span> <span class="punct"><</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">Guard</span>
|
198
198
|
<span class="ident">guard_with</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">value</span><span class="punct">,</span> <span class="ident">sym</span><span class="punct">|</span> <span class="ident">value</span><span class="punct">.</span><span class="ident">respond_to?</span><span class="punct">(</span><span class="ident">sym</span><span class="punct">)</span> <span class="punct">==</span> <span class="constant">true</span> <span class="punct">}</span>
|
199
199
|
<span class="ident">evaluates_in_value_environment</span> <span class="keyword">and</span> <span class="ident">returns_result</span>
|
200
200
|
<span class="ident">belongs_to</span> <span class="constant">Object</span>
|
@@ -318,7 +318,7 @@
|
|
318
318
|
|
319
319
|
<p>Comments are welcome. Send an email to <a href="mailto:raganwald+rubyforge@gmail.com">Reginald Braithwaite</a>. And you can always visit <a href="http://weblog.raganwald.com/">weblog.raganwald.com</a> to see what’s cooking.</p>
|
320
320
|
<p class="coda">
|
321
|
-
<a href="http://weblog.raganwald.com/">Reginald Braithwaite</a>,
|
321
|
+
<a href="http://weblog.raganwald.com/">Reginald Braithwaite</a>, 8th March 2008<br>
|
322
322
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
323
323
|
</p>
|
324
324
|
</div>
|
data/website/index.txt
CHANGED
@@ -30,16 +30,16 @@ let(Person.find(:first, ...)) { |person| person.phone_number if person.friend? }
|
|
30
30
|
|
31
31
|
This code makes it clear that you only need the @person@ variable inside the block. If you want to refactor this code, you know that the entire expression can move without breaking another piece of code. We'll elaborate on the differences between #let, #returning, #my, and #inside below.
|
32
32
|
|
33
|
-
(The four methods were inspired by "Michiel de Mare's post on the same subject":http://blog.rubyenrails.nl/articles/2008/02/18/our-daily-method-10-object-r-rs-ds-s, although Ick's nomenclature is not compatible with Michiel's. Michiel's #
|
33
|
+
(The four methods were inspired by "Michiel de Mare's post on the same subject":http://blog.rubyenrails.nl/articles/2008/02/18/our-daily-method-10-object-r-rs-ds-s, although Ick's nomenclature is not compatible with Michiel's. Michiel's #rsss, #rrss, #rsds, and #rrds are called #returning, #let, #inside, and #my in Ick.)
|
34
34
|
|
35
35
|
h2. Guarded Evaluation
|
36
36
|
|
37
|
-
The example above is a common one. Sometimes we want to evaluate a chain of method calls without throwing a
|
37
|
+
The example above is a common one. Sometimes we want to evaluate a chain of method calls without throwing a _NoMethodError_ if one of the recipients is nil. Sometimes we want to send something a message if and only if it handles the method. There are lots of ad-hoc solutions, including "Object#andand":http://andand.rubyforge.org. What if you don't want to install lots of different gems, one for each use?
|
38
38
|
|
39
39
|
Ick solves this problem by providing a structure for rolling your own guarded evaluation. You can check for nil, #respond_to?, custom permissions, whatever you like. It looks like this:
|
40
40
|
|
41
41
|
<pre syntax="ruby">
|
42
|
-
class Try < Ick::
|
42
|
+
class Try < Ick::Guard
|
43
43
|
guard_with { |value, sym| value.respond_to?(sym) == true }
|
44
44
|
evaluates_in_calling_environment and returns_result
|
45
45
|
belongs_to Object
|
@@ -119,7 +119,7 @@ There are two binary decisions to be made about every block: First, do you want
|
|
119
119
|
The methods #try and #maybe are both implemented as _evaluates_in_calling_environment_, because that is least surprising. But when you're rolling your own, you might want to change that to make things more sugary. For example, here is a different version of #try:
|
120
120
|
|
121
121
|
<pre syntax="ruby">
|
122
|
-
class Please < Ick::
|
122
|
+
class Please < Ick::Guard
|
123
123
|
guard_with { |value, sym| value.respond_to?(sym) == true }
|
124
124
|
evaluates_in_value_environment and returns_result
|
125
125
|
belongs_to Object
|