blox 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/blox.gemspec +2 -2
  2. data/blox.rb +37 -11
  3. metadata +2 -2
data/blox.gemspec CHANGED
@@ -5,10 +5,10 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Kim Burgestrand"]
6
6
  gem.email = ["kim@burgestrand.se"]
7
7
  gem.summary = "Blox helps you write Ruby methods that accept multiple blocks"
8
- gem.homepage = "http://github.com/Burgestrand/numerous_blocks"
8
+ gem.homepage = "https://github.com/Burgestrand/blox"
9
9
 
10
10
  gem.files = `git ls-files`.split($\)
11
11
  gem.test_files = 'blox.rb'
12
12
  gem.require_paths = ["."]
13
- gem.version = "1.0.0"
13
+ gem.version = "1.1.0"
14
14
  end
data/blox.rb CHANGED
@@ -1,17 +1,43 @@
1
+ # Call a callable with the Blox object and return the result.
2
+ #
3
+ # @param (see Blox.object)
4
+ # @return the result of the blox object
5
+ def Blox(block, name, *args, &extra)
6
+ Blox.object(block, name, *args, &extra).__result__
7
+ end
8
+
9
+ # Call a callable and return the result, raising if the result is undefined.
10
+ #
11
+ # @raise [LocalJumpError] if none of the blocks was invoked
12
+ # @param (see Blox.object)
13
+ # @return the result of the blox object
14
+ def Blox!(block, name, *args, &extra)
15
+ Blox.object(block, name, *args, &extra).__result__!
16
+ end
17
+
1
18
  module Blox
2
- def yield_to(name, *args, &block)
3
- blox_call(name, *args, &block).__result__
19
+ # @param (see Blox)
20
+ # @return (see Blox)
21
+ def yield_to(*args, &extra)
22
+ Blox(self, *args, &extra)
4
23
  end
5
24
 
6
- def yield_to!(name, *args, &block)
7
- blox_call(name, *args, &block).__result__!
25
+ # @param (see Blox!)
26
+ # @return (see Blox!)
27
+ def yield_to!(*args, &extra)
28
+ Blox!(self, *args, &extra)
8
29
  end
9
30
 
10
- private
31
+ # Construct a Blox object with the given proc, and call it with the given arguments.
32
+ #
33
+ # @param [#call] block
34
+ # @param [#to_s] name
35
+ # @param *args any additional args to the multi-block
36
+ # @return a blox object that responds to __result__ and __result__!
37
+ def self.object(block, name, *args, &extra)
38
+ blox_object = BasicObject.new
11
39
 
12
- def blox_call(name, *args, &block)
13
- blox_block = BasicObject.new
14
- (class << blox_block; self; end).instance_eval do
40
+ (class << blox_object; self; end).instance_eval do
15
41
  define_method(:__result__) do
16
42
  @result
17
43
  end
@@ -30,13 +56,13 @@ module Blox
30
56
 
31
57
  define_method(:method_missing) do |method_name = :method_missing, &handler|
32
58
  if name == method_name
33
- @result = handler.call(*args, &block)
59
+ @result = handler.call(*args, &extra)
34
60
  end
35
61
  end
36
62
  end
37
63
 
38
- call(blox_block)
39
- blox_block
64
+ block.call(blox_object)
65
+ blox_object
40
66
  end
41
67
  end
42
68
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blox
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -25,7 +25,7 @@ files:
25
25
  - Rakefile
26
26
  - blox.gemspec
27
27
  - blox.rb
28
- homepage: http://github.com/Burgestrand/numerous_blocks
28
+ homepage: https://github.com/Burgestrand/blox
29
29
  licenses: []
30
30
  post_install_message:
31
31
  rdoc_options: []