wendy 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/README.md +36 -0
- data/lib/wendy.rb +4 -3
- data/spec/lib/wendy_spec.rb +15 -1
- data/wendy.gemspec +1 -1
- metadata +2 -1
data/.gitignore
CHANGED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
## Wendy ##
|
2
|
+
###An elegant way to chain methods in Ruby ##
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
Simply include `Wendy` in the class you want to chain methods in. You now have access to the `wend` method.
|
7
|
+
|
8
|
+
The `wend` method accepts as its argument the initial parameter for the chain.
|
9
|
+
|
10
|
+
You then 'wend' this argument through multiple methods by chaining them with the `through` and `and` methods.
|
11
|
+
|
12
|
+
When you're done, call `result` or `and_return` to get the final result.
|
13
|
+
|
14
|
+
Here's an example:
|
15
|
+
|
16
|
+
class MathsFunctions
|
17
|
+
include Wendy
|
18
|
+
|
19
|
+
def double(x)
|
20
|
+
x + x
|
21
|
+
end
|
22
|
+
|
23
|
+
def square(x)
|
24
|
+
x * x
|
25
|
+
end
|
26
|
+
|
27
|
+
def two_x_squared(x)
|
28
|
+
wend(x).through(:double).and(:square).and_return
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
MathsFunctions.new.two_x_squared(5) # => 100
|
34
|
+
|
35
|
+
|
36
|
+
|
data/lib/wendy.rb
CHANGED
@@ -6,13 +6,14 @@ module Wendy
|
|
6
6
|
|
7
7
|
class Wender
|
8
8
|
|
9
|
-
def initialize(initiator, arg)
|
9
|
+
def initialize(initiator, arg, *others)
|
10
10
|
@initiator = initiator
|
11
11
|
@arg = arg
|
12
|
+
@others = others
|
12
13
|
end
|
13
14
|
|
14
|
-
def through(method)
|
15
|
-
Wender.new(initiator, initiator.send(method, arg))
|
15
|
+
def through(method, *others)
|
16
|
+
Wender.new(initiator, initiator.send(method, *([arg] + others)))
|
16
17
|
end
|
17
18
|
|
18
19
|
def result
|
data/spec/lib/wendy_spec.rb
CHANGED
@@ -11,16 +11,30 @@ class TestWender
|
|
11
11
|
x + x
|
12
12
|
end
|
13
13
|
|
14
|
+
def add(x, y)
|
15
|
+
x + y
|
16
|
+
end
|
17
|
+
|
14
18
|
def wend_test
|
15
19
|
wend(5).through(:square).and(:double).and_return
|
16
20
|
end
|
17
21
|
|
22
|
+
def wend_test_with_arg
|
23
|
+
wend(5).through(:square).and(:double).and(:add, 15).and_return
|
24
|
+
end
|
25
|
+
|
18
26
|
end
|
19
27
|
|
20
28
|
describe Wendy do
|
21
29
|
|
30
|
+
let(:wender) { TestWender.new }
|
31
|
+
|
22
32
|
it 'allows method chaining' do
|
23
|
-
|
33
|
+
wender.wend_test.should eq 50
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'passes additional arguments to methods in the chain' do
|
37
|
+
wender.wend_test_with_arg.should eq 65
|
24
38
|
end
|
25
39
|
|
26
40
|
end
|
data/wendy.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wendy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- .gitignore
|
38
38
|
- Gemfile
|
39
39
|
- Gemfile.lock
|
40
|
+
- README.md
|
40
41
|
- Rakefile
|
41
42
|
- _license
|
42
43
|
- lib/wendy.rb
|