block_chain 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +7 -0
- data/block_chain.gemspec +23 -0
- data/lib/block_chain/version.rb +3 -0
- data/lib/block_chain.rb +21 -0
- data/spec/block_chain_spec.rb +17 -0
- metadata +91 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 J. Andrew Marshall <http://johnandrewmarshall.com/>.
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# BlockChain
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/amarshall/block_chain.png)](http://travis-ci.org/amarshall/block_chain)
|
4
|
+
|
5
|
+
Makes it easy to chain nested blocks together without creating a ton of nesting.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install as usual: `gem install block_chain` or add `gem 'block_chain'` to your Gemfile.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Turn this:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
foo do
|
17
|
+
bar do
|
18
|
+
baz do
|
19
|
+
qux do
|
20
|
+
yield
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
into this:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'block_chain'
|
31
|
+
BlockChain.new(method(:foo), method(:bar), method(:baz), method(:qux)).call { yield }
|
32
|
+
```
|
33
|
+
|
34
|
+
or:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'block_chain'
|
38
|
+
methods = [:foo, :bar, :baz, :qux].map { |name| method name }
|
39
|
+
BlockChain.new(*methods).call { yield }
|
40
|
+
```
|
41
|
+
|
42
|
+
This can make deeply-nested block wrappings a bit more digestable.
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Contributions are welcome. Please be sure that your pull requests are atomic so they can be considered and accepted separately.
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
53
|
+
|
54
|
+
## Credits & License
|
55
|
+
|
56
|
+
Copyright © 2013 J. Andrew Marshall. License is available in the LICENSE file.
|
data/Rakefile
ADDED
data/block_chain.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'block_chain/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'block_chain'
|
8
|
+
gem.version = BlockChain::VERSION
|
9
|
+
gem.authors = ['Andrew Marshall']
|
10
|
+
gem.email = ['andrew@johnandrewmarshall.com']
|
11
|
+
gem.description = %q{Makes it easy to chain nested blocks together without creating a ton of nesting.}
|
12
|
+
gem.summary = %q{Makes it easy to chain nested blocks together without creating a ton of nesting.}
|
13
|
+
gem.homepage = 'http://johnandrewmarshall.com/projects/block_chain'
|
14
|
+
gem.license = 'MIT'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
end
|
data/lib/block_chain.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'block_chain/version'
|
2
|
+
|
3
|
+
class BlockChain
|
4
|
+
def initialize *methods
|
5
|
+
@procs = methods.map(&:to_proc)
|
6
|
+
end
|
7
|
+
|
8
|
+
def call
|
9
|
+
if @procs.none?
|
10
|
+
yield
|
11
|
+
elsif @procs.one?
|
12
|
+
@procs.first.call { yield }
|
13
|
+
else
|
14
|
+
current = @procs.first
|
15
|
+
nexts = self.class.new(*@procs.slice(1..-1))
|
16
|
+
current.call do
|
17
|
+
nexts.call { yield }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'block_chain'
|
2
|
+
|
3
|
+
describe BlockChain do
|
4
|
+
it "chains multiple methods together by wrapping each in a block" do
|
5
|
+
def f; "f(#{yield})"; end
|
6
|
+
def g; "g(#{yield})"; end
|
7
|
+
def h; "h(#{yield})"; end
|
8
|
+
|
9
|
+
block_chain = BlockChain.new method(:f), method(:g), method(:h)
|
10
|
+
|
11
|
+
block_chain.call { 'x' }.should == 'f(g(h(x)))'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "just yields when no methods are given" do
|
15
|
+
BlockChain.new.call { 'x' }.should == 'x'
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: block_chain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Marshall
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Makes it easy to chain nested blocks together without creating a ton
|
47
|
+
of nesting.
|
48
|
+
email:
|
49
|
+
- andrew@johnandrewmarshall.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .travis.yml
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- block_chain.gemspec
|
61
|
+
- lib/block_chain.rb
|
62
|
+
- lib/block_chain/version.rb
|
63
|
+
- spec/block_chain_spec.rb
|
64
|
+
homepage: http://johnandrewmarshall.com/projects/block_chain
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.25
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Makes it easy to chain nested blocks together without creating a ton of nesting.
|
89
|
+
test_files:
|
90
|
+
- spec/block_chain_spec.rb
|
91
|
+
has_rdoc:
|