multiblock 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/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +97 -0
- data/Rakefile +1 -0
- data/lib/multiblock.rb +18 -0
- data/multiblock.gemspec +24 -0
- data/spec/multiblock_spec.rb +47 -0
- data/spec/spec_helper.rb +3 -0
- metadata +133 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
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,97 @@
|
|
1
|
+
# Multiblock
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/monterail/multiblock.png)](http://travis-ci.org/monterail/multiblock)
|
4
|
+
|
5
|
+
Ruby methods can accept only one block at a time. Multiblock helps to build multiple-block wrappers that can be passed to Ruby methods in pleasant way.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'multiblock'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install multiblock
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Register blocks simply by calling methods on a multiblock instance and passing actual Ruby callable objects (procs, lamdas, etc.) along:
|
24
|
+
|
25
|
+
multiblock = Multiblock.new
|
26
|
+
multiblock.foo { "foo" }
|
27
|
+
multiblock.bar { |arg| "bar with #{arg}"}
|
28
|
+
|
29
|
+
Then call registered blocks:
|
30
|
+
|
31
|
+
multiblock.call(:foo)
|
32
|
+
# => "foo"
|
33
|
+
|
34
|
+
multiblock.call(:bar, "argument")
|
35
|
+
# => "bar with argument"
|
36
|
+
|
37
|
+
When calling a block under unregistered name `nil` is returned by default:
|
38
|
+
|
39
|
+
multiblock.call(:bar)
|
40
|
+
# => nil
|
41
|
+
|
42
|
+
But you can supply a block that will be called by default in place of unregistered one:
|
43
|
+
|
44
|
+
multiblock = Multiblock.new { "default" }
|
45
|
+
multiblock.call(:bar)
|
46
|
+
# => "default"
|
47
|
+
|
48
|
+
## Real world example
|
49
|
+
|
50
|
+
Multiblock shines in situations when you would like to pass multiple blocks to a method. Perhaps to handle its different outcomes.
|
51
|
+
|
52
|
+
Since Ruby methods accepts only one block at a time, we simulate passing multiple blocks with this nice-looking syntax:
|
53
|
+
|
54
|
+
process(message) do |on|
|
55
|
+
on.success { puts "ok" }
|
56
|
+
on.failure { puts "fail" }
|
57
|
+
end
|
58
|
+
|
59
|
+
To make it work, let's define `process` method in following way:
|
60
|
+
|
61
|
+
def process(message)
|
62
|
+
multiblock = Multiblock.new
|
63
|
+
|
64
|
+
# wrap blocks
|
65
|
+
yield(multiblock)
|
66
|
+
|
67
|
+
# do actual processing...
|
68
|
+
|
69
|
+
if result == "success"
|
70
|
+
multiblock.call(:success)
|
71
|
+
else
|
72
|
+
multiblock.call(:failure)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
Another example which kinda resembles `respond_with` feature from Ruby on Rails `ActionController`:
|
77
|
+
|
78
|
+
def respond_with(object)
|
79
|
+
multiblock = Multiblock.new
|
80
|
+
yield(multiblock)
|
81
|
+
|
82
|
+
# assume that request.format returns either 'json' or 'xml'
|
83
|
+
multiblock.call(request.format, object)
|
84
|
+
end
|
85
|
+
|
86
|
+
respond_with(object) do |format|
|
87
|
+
format.xml { |object| render :xml => object.to_xml }
|
88
|
+
format.json { |object| render :json => object.to_json }
|
89
|
+
end
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
1. Fork it
|
94
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
95
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
96
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
97
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/multiblock.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "blankslate"
|
2
|
+
|
3
|
+
class Multiblock < BlankSlate
|
4
|
+
def initialize(&default)
|
5
|
+
default ||= lambda { |*args| nil }
|
6
|
+
|
7
|
+
@blocks = Hash.new(default)
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(name, *args, &blk)
|
11
|
+
raise ArgumentError.new("No block given when registering '#{name}' block.") unless block_given?
|
12
|
+
@blocks[name.to_s] = blk
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(name, *args)
|
16
|
+
@blocks[name.to_s].call(*args)
|
17
|
+
end
|
18
|
+
end
|
data/multiblock.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "multiblock"
|
7
|
+
spec.version = "0.1.0"
|
8
|
+
spec.authors = ["Michał Szajbe"]
|
9
|
+
spec.email = ["michal.szajbe@gmail.com"]
|
10
|
+
spec.description = %q{Ruby methods can accept only one block at a time. Multiblock helps to build multiple-block wrappers that can be passed to Ruby methods in pleasant way.}
|
11
|
+
spec.summary = %q{Pass multiple blocks to Ruby methods with style}
|
12
|
+
spec.homepage = "http://github.com/monterail/multiblock"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "blankslate"
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Multiblock do
|
4
|
+
|
5
|
+
let(:multiblock) {
|
6
|
+
Multiblock.new
|
7
|
+
}
|
8
|
+
|
9
|
+
it "should register block" do
|
10
|
+
multiblock.foo { "foo" }
|
11
|
+
multiblock.call(:foo).should == "foo"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should call registered block by String" do
|
15
|
+
multiblock.foo { "foo" }
|
16
|
+
multiblock.call("foo").should == "foo"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return nil when calling unregistered block" do
|
20
|
+
multiblock.call(:foo).should be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return nil when calling unregistered block with arguments" do
|
24
|
+
multiblock.call(:foo).should be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should pass arguments to called block" do
|
28
|
+
multiblock.foo { |arg| arg }
|
29
|
+
multiblock.call(:foo, "foo").should == "foo"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should raise ArgumentError exception when registering without block" do
|
33
|
+
lambda {
|
34
|
+
multiblock.foo
|
35
|
+
}.should raise_exception(ArgumentError, "No block given when registering 'foo' block.")
|
36
|
+
end
|
37
|
+
|
38
|
+
context "with custom default block" do
|
39
|
+
let(:multiblock) {
|
40
|
+
Multiblock.new { "default" }
|
41
|
+
}
|
42
|
+
|
43
|
+
it "should call custom default block when calling unregistered block" do
|
44
|
+
multiblock.call(:foo).should == "default"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multiblock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Micha\xC5\x82 Szajbe"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2013-04-08 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: blankslate
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: bundler
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 9
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 3
|
46
|
+
version: "1.3"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rake
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
type: :development
|
76
|
+
version_requirements: *id004
|
77
|
+
description: Ruby methods can accept only one block at a time. Multiblock helps to build multiple-block wrappers that can be passed to Ruby methods in pleasant way.
|
78
|
+
email:
|
79
|
+
- michal.szajbe@gmail.com
|
80
|
+
executables: []
|
81
|
+
|
82
|
+
extensions: []
|
83
|
+
|
84
|
+
extra_rdoc_files: []
|
85
|
+
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- .rspec
|
89
|
+
- .travis.yml
|
90
|
+
- Gemfile
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- lib/multiblock.rb
|
95
|
+
- multiblock.gemspec
|
96
|
+
- spec/multiblock_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: http://github.com/monterail/multiblock
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
hash: 3
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
version: "0"
|
124
|
+
requirements: []
|
125
|
+
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 1.8.15
|
128
|
+
signing_key:
|
129
|
+
specification_version: 3
|
130
|
+
summary: Pass multiple blocks to Ruby methods with style
|
131
|
+
test_files:
|
132
|
+
- spec/multiblock_spec.rb
|
133
|
+
- spec/spec_helper.rb
|