block_chain 0.1.0 → 0.2.0
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.
- checksums.yaml +7 -0
- data/.travis.yml +12 -0
- data/README.md +11 -2
- data/Rakefile +11 -1
- data/lib/block_chain.rb +4 -4
- data/lib/block_chain/version.rb +1 -1
- data/spec/block_chain_spec.rb +51 -0
- metadata +11 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dc4d3650effed7ea3364a9ed2797e186eaaf6b89
|
4
|
+
data.tar.gz: 8cc27e9ce3a78803813d093760bf00783cb28385
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bfd015384e595a892f073902c4a2c33404575aa44b2ab4f24ffbc84fe7f77421612848a5c0a62b3ed000062cdad7fbe99100655aaeed82f84a46293361227010
|
7
|
+
data.tar.gz: 9385eb94f1972e551d32690b0049b2cb9c6c87e5e99664f8316b8389f22a0e009f05940580cb6b2e756cf3312f4a2e0024fdbb170907fb7964de317c5138803a
|
data/.travis.yml
CHANGED
@@ -2,5 +2,17 @@ language: ruby
|
|
2
2
|
rvm:
|
3
3
|
- 1.9.2
|
4
4
|
- 1.9.3
|
5
|
+
- 2.0.0
|
5
6
|
- jruby-19mode
|
6
7
|
- rbx-19mode
|
8
|
+
matrix:
|
9
|
+
include:
|
10
|
+
- rvm: jruby-19mode
|
11
|
+
env: WITH_RUBY_BUGS=true
|
12
|
+
- rvm: rbx-19mode
|
13
|
+
env: WITH_RUBY_BUGS=true
|
14
|
+
allow_failures:
|
15
|
+
- rvm: jruby-19mode
|
16
|
+
env: WITH_RUBY_BUGS=true
|
17
|
+
- rvm: rbx-19mode
|
18
|
+
env: WITH_RUBY_BUGS=true
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# BlockChain
|
2
2
|
|
3
|
-
[](http://travis-ci.org/amarshall/block_chain)
|
3
|
+
[](http://travis-ci.org/amarshall/block_chain)
|
4
4
|
|
5
5
|
Makes it easy to chain nested blocks together without creating a ton of nesting.
|
6
6
|
|
@@ -39,7 +39,16 @@ methods = [:foo, :bar, :baz, :qux].map { |name| method name }
|
|
39
39
|
BlockChain.new(*methods).call { yield }
|
40
40
|
```
|
41
41
|
|
42
|
-
|
42
|
+
or even this:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
require 'block_chain'
|
46
|
+
BlockChain.new(:foo, :bar, :baz, :qux).call(self) { yield }
|
47
|
+
```
|
48
|
+
|
49
|
+
With the last form, the argument given to `call` is the receiver of the methods (since the receiver cannot be implicit, it must be explicitly specified). Also note that this form does not work in JRuby or Rubinius due to a bug. A gap in RubySpec was found as a result of this feature, see [RubySpec #174](https://github.com/rubyspec/rubyspec/pull/174).
|
50
|
+
|
51
|
+
This can make deeply-nested block wrappings a bit more digestable. For more details, read the specs, they’re short and descriptive.
|
43
52
|
|
44
53
|
## Contributing
|
45
54
|
|
data/Rakefile
CHANGED
@@ -1,7 +1,17 @@
|
|
1
1
|
require 'bundler/gem_tasks'
|
2
2
|
require 'rspec/core/rake_task'
|
3
3
|
|
4
|
+
def rspec_tag_options
|
5
|
+
return if ENV['WITH_RUBY_BUGS']
|
6
|
+
case RUBY_ENGINE
|
7
|
+
when 'jruby' then '--tag ~jruby_bug'
|
8
|
+
when 'rbx' then '--tag ~rbx_bug'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
4
12
|
desc 'Run specs'
|
5
|
-
RSpec::Core::RakeTask.new
|
13
|
+
RSpec::Core::RakeTask.new do |t|
|
14
|
+
t.rspec_opts = rspec_tag_options
|
15
|
+
end
|
6
16
|
|
7
17
|
task :default => :spec
|
data/lib/block_chain.rb
CHANGED
@@ -5,16 +5,16 @@ class BlockChain
|
|
5
5
|
@procs = methods.map(&:to_proc)
|
6
6
|
end
|
7
7
|
|
8
|
-
def call
|
8
|
+
def call *args
|
9
9
|
if @procs.none?
|
10
10
|
yield
|
11
11
|
elsif @procs.one?
|
12
|
-
@procs.first.call { yield }
|
12
|
+
@procs.first.call(*args) { yield }
|
13
13
|
else
|
14
14
|
current = @procs.first
|
15
15
|
nexts = self.class.new(*@procs.slice(1..-1))
|
16
|
-
current.call do
|
17
|
-
nexts.call { yield }
|
16
|
+
current.call(*args) do
|
17
|
+
nexts.call(*args) { yield }
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
data/lib/block_chain/version.rb
CHANGED
data/spec/block_chain_spec.rb
CHANGED
@@ -11,6 +11,57 @@ describe BlockChain do
|
|
11
11
|
block_chain.call { 'x' }.should == 'f(g(h(x)))'
|
12
12
|
end
|
13
13
|
|
14
|
+
it "passes along arguments to the methods" do
|
15
|
+
def f(arg); "f(#{arg}#{yield})"; end
|
16
|
+
def g(arg); "g(#{arg}#{yield})"; end
|
17
|
+
def h(arg); "h(#{arg}#{yield})"; end
|
18
|
+
|
19
|
+
block_chain = BlockChain.new method(:f), method(:g), method(:h)
|
20
|
+
|
21
|
+
block_chain.call(42) { 'x' }.should == 'f(42g(42h(42x)))'
|
22
|
+
end
|
23
|
+
|
24
|
+
context "on MRI Ruby only", jruby_bug: true, rbx_bug: true do
|
25
|
+
it "accepts methods passed as a symbol, calling them on the object passed to call" do
|
26
|
+
def f; "f(#{yield})"; end
|
27
|
+
def g; "g(#{yield})"; end
|
28
|
+
def h; "h(#{yield})"; end
|
29
|
+
|
30
|
+
block_chain = BlockChain.new :f, :g, :h
|
31
|
+
|
32
|
+
block_chain.call(self) { 'x' }.should == 'f(g(h(x)))'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "cannot call private methods when passing a symbol" do
|
36
|
+
klass = Class.new do
|
37
|
+
def f; "f(#{yield})"; end
|
38
|
+
def g; "g(#{yield})"; end
|
39
|
+
def h; "h(#{yield})"; end
|
40
|
+
private :f, :g, :h
|
41
|
+
end
|
42
|
+
block_chain = BlockChain.new :f, :g, :h
|
43
|
+
|
44
|
+
-> do
|
45
|
+
block_chain.call(klass.new) { 'x' }
|
46
|
+
end.should raise_error(NoMethodError, /private method/)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it "can call private methods when passing a method" do
|
51
|
+
klass = Class.new do
|
52
|
+
def f; "f(#{yield})"; end
|
53
|
+
def g; "g(#{yield})"; end
|
54
|
+
def h; "h(#{yield})"; end
|
55
|
+
private :f, :g, :h
|
56
|
+
end
|
57
|
+
instance = klass.new
|
58
|
+
block_chain = BlockChain.new instance.method(:f), instance.method(:g), instance.method(:h)
|
59
|
+
|
60
|
+
-> do
|
61
|
+
block_chain.call { 'x' }
|
62
|
+
end.should_not raise_error(NoMethodError, /private method/)
|
63
|
+
end
|
64
|
+
|
14
65
|
it "just yields when no methods are given" do
|
15
66
|
BlockChain.new.call { 'x' }.should == 'x'
|
16
67
|
end
|
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: block_chain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Andrew Marshall
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
11
|
+
date: 2013-02-27 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: Makes it easy to chain nested blocks together without creating a ton
|
@@ -64,27 +59,26 @@ files:
|
|
64
59
|
homepage: http://johnandrewmarshall.com/projects/block_chain
|
65
60
|
licenses:
|
66
61
|
- MIT
|
62
|
+
metadata: {}
|
67
63
|
post_install_message:
|
68
64
|
rdoc_options: []
|
69
65
|
require_paths:
|
70
66
|
- lib
|
71
67
|
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
68
|
requirements:
|
74
|
-
- -
|
69
|
+
- - '>='
|
75
70
|
- !ruby/object:Gem::Version
|
76
71
|
version: '0'
|
77
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
-
none: false
|
79
73
|
requirements:
|
80
|
-
- -
|
74
|
+
- - '>='
|
81
75
|
- !ruby/object:Gem::Version
|
82
76
|
version: '0'
|
83
77
|
requirements: []
|
84
78
|
rubyforge_project:
|
85
|
-
rubygems_version:
|
79
|
+
rubygems_version: 2.0.0
|
86
80
|
signing_key:
|
87
|
-
specification_version:
|
81
|
+
specification_version: 4
|
88
82
|
summary: Makes it easy to chain nested blocks together without creating a ton of nesting.
|
89
83
|
test_files:
|
90
84
|
- spec/block_chain_spec.rb
|