multiblock 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9d60eb3f2db6d5129cf689c5eba26bf3affc540
4
+ data.tar.gz: 3efa1d6b3e9c47f26a96acbd41b6a8b5ce217c13
5
+ SHA512:
6
+ metadata.gz: 0dc718f167a7ab6a37434fcad69b0585612a81542451980ade57f62ba16bbc2b7674944d7d803dead950091fe61437dc1e3df320a66e5c92a8da1bbda6f44fe4
7
+ data.tar.gz: f7b4ade589df0f43bb8814a1c96c1ccf318ba96e72c7c4c07022d683ef7592cb23ad7159162a6e05d0d7a694aa5492831bfb3c4726c0c3a0a25932855ebd0aa3
data/.travis.yml CHANGED
@@ -6,3 +6,4 @@ rvm:
6
6
  - 1.9.2
7
7
  - 1.8.7
8
8
  - ree
9
+ - jruby
data/README.md CHANGED
@@ -20,29 +20,36 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Register blocks simply by calling methods on a multiblock instance and passing actual Ruby callable objects (procs, lamdas, etc.) along:
23
+ Obtain multiblock wrapper instance
24
24
 
25
- multiblock = Multiblock.new
26
- multiblock.foo { "foo" }
27
- multiblock.bar { |arg| "bar with #{arg}"}
25
+ wrapper = Multiblock::Wrapper.new
26
+
27
+ # or via shortcut
28
+ wrapper = Multiblock.wrapper
29
+
30
+ Register blocks simply by calling methods on a multiblock wrapper instance and passing actual Ruby callable objects (procs, lamdas, etc.) along:
31
+
32
+ wrapper = Multiblock.wrapper
33
+ wrapper.foo { "foo" }
34
+ wrapper.bar { |arg| "bar with #{arg}"}
28
35
 
29
36
  Then call registered blocks:
30
37
 
31
- multiblock.call(:foo)
38
+ wrapper.call(:foo)
32
39
  # => "foo"
33
40
 
34
- multiblock.call(:bar, "argument")
41
+ wrapper.call(:bar, "argument")
35
42
  # => "bar with argument"
36
43
 
37
44
  When calling a block under unregistered name `nil` is returned by default:
38
45
 
39
- multiblock.call(:bar)
46
+ wrapper.call(:bar)
40
47
  # => nil
41
48
 
42
49
  But you can supply a block that will be called by default in place of unregistered one:
43
50
 
44
- multiblock = Multiblock.new { "default" }
45
- multiblock.call(:bar)
51
+ wrapper = Multiblock.wrapper { "default" }
52
+ wrapper.call(:bar)
46
53
  # => "default"
47
54
 
48
55
  ## Real world example
@@ -59,28 +66,28 @@ Since Ruby methods accepts only one block at a time, we simulate passing multipl
59
66
  To make it work, let's define `process` method in following way:
60
67
 
61
68
  def process(message)
62
- multiblock = Multiblock.new
69
+ wrapper = Multiblock.wrapper
63
70
 
64
71
  # wrap blocks
65
- yield(multiblock)
72
+ yield(wrapper)
66
73
 
67
74
  # do actual processing...
68
75
 
69
76
  if result == "success"
70
- multiblock.call(:success)
77
+ wrapper.call(:success)
71
78
  else
72
- multiblock.call(:failure)
79
+ wrapper.call(:failure)
73
80
  end
74
81
  end
75
82
 
76
83
  Another example which kinda resembles `respond_with` feature from Ruby on Rails `ActionController`:
77
84
 
78
85
  def respond_with(object)
79
- multiblock = Multiblock.new
80
- yield(multiblock)
86
+ wrapper = Multiblock.new
87
+ yield(wrapper)
81
88
 
82
89
  # assume that request.format returns either 'json' or 'xml'
83
- multiblock.call(request.format, object)
90
+ wrapper.call(request.format, object)
84
91
  end
85
92
 
86
93
  respond_with(object) do |format|
@@ -0,0 +1,3 @@
1
+ module Multiblock
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ module Multiblock
2
+ class Wrapper < BasicObject
3
+ def initialize(&default)
4
+ default ||= ::Kernel.lambda { |*args| nil }
5
+
6
+ @blocks = ::Hash.new(default)
7
+ end
8
+
9
+ def method_missing(name, *args, &blk)
10
+ ::Kernel.raise ::ArgumentError.new("No block given when registering '#{name}' block.") if blk.nil?
11
+ @blocks[name.to_s] = blk
12
+ end
13
+
14
+ def call(name, *args)
15
+ @blocks[name.to_s].call(*args)
16
+ end
17
+
18
+ def inspect
19
+ "#<Multiblock::Wrapper @blocks=#{@blocks.inspect}>"
20
+ end
21
+ end
22
+ end
23
+
data/lib/multiblock.rb CHANGED
@@ -1,18 +1,28 @@
1
- require "blankslate"
1
+ require "multiblock/version"
2
2
 
3
- class Multiblock < BlankSlate
4
- def initialize(&default)
5
- default ||= lambda { |*args| nil }
3
+ module Multiblock
4
+ if defined?(BasicObject)
5
+ BasicObject = ::BasicObject
6
+ elsif defined?(BlankSlate)
7
+ BasicObject = ::BlankSlate
8
+ else
6
9
 
7
- @blocks = Hash.new(default)
10
+ # If neither BasicObject (Ruby 1.9) nor BlankSlate (typically provided by Builder)
11
+ # are present, define our simple implementation
12
+ class BasicObject
13
+ instance_methods.each do |meth|
14
+ unless meth =~ /\A__/ || %w[ instance_exec instance_eval == equal? object_id ! != ].include?(meth)
15
+ undef_method(meth)
16
+ end
17
+ end
18
+ end
8
19
  end
9
20
 
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)
21
+ class << self
22
+ def wrapper(*args)
23
+ Wrapper.new(*args)
24
+ end
17
25
  end
18
26
  end
27
+
28
+ require "multiblock/wrapper"
data/multiblock.gemspec CHANGED
@@ -1,10 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "multiblock/version"
4
5
 
5
6
  Gem::Specification.new do |spec|
6
7
  spec.name = "multiblock"
7
- spec.version = "0.1.0"
8
+ spec.version = Multiblock::VERSION
8
9
  spec.authors = ["Michał Szajbe"]
9
10
  spec.email = ["michal.szajbe@gmail.com"]
10
11
  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.}
@@ -17,7 +18,6 @@ Gem::Specification.new do |spec|
17
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
19
  spec.require_paths = ["lib"]
19
20
 
20
- spec.add_dependency "blankslate"
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
@@ -0,0 +1,47 @@
1
+ require "spec_helper"
2
+
3
+ describe Multiblock::Wrapper do
4
+
5
+ let(:wrapper) {
6
+ described_class.new
7
+ }
8
+
9
+ it "should register block" do
10
+ wrapper.foo { "foo" }
11
+ wrapper.call(:foo).should == "foo"
12
+ end
13
+
14
+ it "should call registered block by String" do
15
+ wrapper.foo { "foo" }
16
+ wrapper.call("foo").should == "foo"
17
+ end
18
+
19
+ it "should return nil when calling unregistered block" do
20
+ wrapper.call(:foo).should be_nil
21
+ end
22
+
23
+ it "should return nil when calling unregistered block with arguments" do
24
+ wrapper.call(:foo).should be_nil
25
+ end
26
+
27
+ it "should pass arguments to called block" do
28
+ wrapper.foo { |arg| arg }
29
+ wrapper.call(:foo, "foo").should == "foo"
30
+ end
31
+
32
+ it "should raise ArgumentError exception when registering without block" do
33
+ lambda {
34
+ wrapper.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(:wrapper) {
40
+ described_class.new { "default" }
41
+ }
42
+
43
+ it "should call custom default block when calling unregistered block" do
44
+ wrapper.call(:foo).should == "default"
45
+ end
46
+ end
47
+ end
@@ -1,47 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
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
4
+ it "should construct wrapper" do
5
+ described_class.wrapper.inspect.should =~ /Multiblock::Wrapper/
46
6
  end
47
7
  end
metadata CHANGED
@@ -1,89 +1,65 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
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
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
11
5
  platform: ruby
12
- authors:
13
- - "Micha\xC5\x82 Szajbe"
6
+ authors:
7
+ - Michał Szajbe
14
8
  autorequire:
15
9
  bindir: bin
16
10
  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
11
+ date: 2013-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
35
14
  name: bundler
36
- prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
- requirements:
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
40
17
  - - ~>
41
- - !ruby/object:Gem::Version
42
- hash: 9
43
- segments:
44
- - 1
45
- - 3
46
- version: "1.3"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
47
20
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rake
51
21
  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"
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
61
34
  type: :development
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: rspec
65
35
  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"
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
75
48
  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:
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ruby methods can accept only one block at a time. Multiblock helps to
56
+ build multiple-block wrappers that can be passed to Ruby methods in pleasant way.
57
+ email:
79
58
  - michal.szajbe@gmail.com
80
59
  executables: []
81
-
82
60
  extensions: []
83
-
84
61
  extra_rdoc_files: []
85
-
86
- files:
62
+ files:
87
63
  - .gitignore
88
64
  - .rspec
89
65
  - .travis.yml
@@ -92,42 +68,37 @@ files:
92
68
  - README.md
93
69
  - Rakefile
94
70
  - lib/multiblock.rb
71
+ - lib/multiblock/version.rb
72
+ - lib/multiblock/wrapper.rb
95
73
  - multiblock.gemspec
74
+ - spec/multiblock/wrapper_spec.rb
96
75
  - spec/multiblock_spec.rb
97
76
  - spec/spec_helper.rb
98
77
  homepage: http://github.com/monterail/multiblock
99
- licenses:
78
+ licenses:
100
79
  - MIT
80
+ metadata: {}
101
81
  post_install_message:
102
82
  rdoc_options: []
103
-
104
- require_paths:
83
+ require_paths:
105
84
  - 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"
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
124
95
  requirements: []
125
-
126
96
  rubyforge_project:
127
- rubygems_version: 1.8.15
97
+ rubygems_version: 2.0.0
128
98
  signing_key:
129
- specification_version: 3
99
+ specification_version: 4
130
100
  summary: Pass multiple blocks to Ruby methods with style
131
- test_files:
101
+ test_files:
102
+ - spec/multiblock/wrapper_spec.rb
132
103
  - spec/multiblock_spec.rb
133
104
  - spec/spec_helper.rb