blox 1.0.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.
Files changed (8) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +21 -0
  4. data/README.md +46 -0
  5. data/Rakefile +2 -0
  6. data/blox.gemspec +14 -0
  7. data/blox.rb +88 -0
  8. metadata +54 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ..gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright 2012 Kim Burgestrand. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are
4
+ permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of
7
+ conditions and the following disclaimer.
8
+
9
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
10
+ of conditions and the following disclaimer in the documentation and/or other materials
11
+ provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY KIM BURGESTRAND ``AS IS'' AND ANY EXPRESS OR IMPLIED
14
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KIM BURGESTRAND OR
16
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Blox
2
+
3
+ A tiny Ruby gem to aid you when you wish to pass multiple blocks to any ruby method.
4
+
5
+ ## Examples
6
+
7
+ ### Passing multiple blocks to a method
8
+
9
+ ```ruby
10
+ def accepts_multiple_blocks(is_awesome, &block)
11
+ if is_awesome
12
+ block.yield_to(:awesome, "this is awesome!") do
13
+ puts "You may pass blocks to the blocks, too!"
14
+ end
15
+ else
16
+ block.yield_to(:not_awesome, "bad, this is bad")
17
+ end
18
+ end
19
+
20
+ accepts_multiple_blocks(:awesome) do |is|
21
+ is.awesome do |&block|
22
+ puts "Very awesome!"
23
+ block.call
24
+ end
25
+
26
+ is.not_awesome do
27
+ puts "Not so awesome"
28
+ end
29
+ end
30
+
31
+ accepts_multiple_blocks(:not_awesome) do |event|
32
+ if event.not_awesome?
33
+ puts ":("
34
+ else
35
+ puts "\\o/!"
36
+ end
37
+ end
38
+ ```
39
+
40
+ If you wish to enforce that at least one of the blocks are executed,
41
+ you may use `block.yield\_to!` (with a bang) instead, which will raise
42
+ an exception if no block was executed.
43
+
44
+ ## License
45
+
46
+ 2-clause (simplified) BSD license. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/blox.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "blox"
5
+ gem.authors = ["Kim Burgestrand"]
6
+ gem.email = ["kim@burgestrand.se"]
7
+ gem.summary = "Blox helps you write Ruby methods that accept multiple blocks"
8
+ gem.homepage = "http://github.com/Burgestrand/numerous_blocks"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.test_files = 'blox.rb'
12
+ gem.require_paths = ["."]
13
+ gem.version = "1.0.0"
14
+ end
data/blox.rb ADDED
@@ -0,0 +1,88 @@
1
+ module Blox
2
+ def yield_to(name, *args, &block)
3
+ blox_call(name, *args, &block).__result__
4
+ end
5
+
6
+ def yield_to!(name, *args, &block)
7
+ blox_call(name, *args, &block).__result__!
8
+ end
9
+
10
+ private
11
+
12
+ def blox_call(name, *args, &block)
13
+ blox_block = BasicObject.new
14
+ (class << blox_block; self; end).instance_eval do
15
+ define_method(:__result__) do
16
+ @result
17
+ end
18
+
19
+ define_method(:__result__!) do
20
+ unless defined?(@result)
21
+ Kernel.raise LocalJumpError, "no block given for #{name}"
22
+ end
23
+
24
+ @result
25
+ end
26
+
27
+ define_method("#{name}?") do
28
+ true
29
+ end
30
+
31
+ define_method(:method_missing) do |method_name = :method_missing, &handler|
32
+ if name == method_name
33
+ @result = handler.call(*args, &block)
34
+ end
35
+ end
36
+ end
37
+
38
+ call(blox_block)
39
+ blox_block
40
+ end
41
+ end
42
+
43
+ Proc.send(:include, Blox)
44
+
45
+ if $0 == __FILE__
46
+ require 'minitest/autorun'
47
+ require 'minitest/spec'
48
+
49
+ def handler_block
50
+ lambda do |on|
51
+ on.some_event do |*params|
52
+ [:first, params]
53
+ end
54
+
55
+ on.another_event do |*params|
56
+ [:second, params]
57
+ end
58
+ end
59
+ end
60
+
61
+ describe Proc do
62
+ describe "#yield_to" do
63
+ it "yields to the matching handler and returns a result" do
64
+ result = handler_block.yield_to(:some_event, 1, "two", :three)
65
+ result.must_equal [:first, [1, "two", :three]]
66
+ result = handler_block.yield_to(:another_event)
67
+ result.must_equal [:second, []]
68
+ end
69
+
70
+ it "does nothing if no handler was invoked" do
71
+ handler_block.yield_to(:nonexistant).must_equal nil
72
+ end
73
+
74
+ it "allows boolean operator for conditional checks" do
75
+ lambda { |dsl|
76
+ dsl.some_event?.must_equal true
77
+ dsl.another_event?.must_be_nil
78
+ }.yield_to(:some_event)
79
+ end
80
+ end
81
+
82
+ describe "#yield_to!" do
83
+ it "raises an error if no handler was invoked" do
84
+ proc { handler_block.yield_to!(:nonexistant) }.must_raise LocalJumpError
85
+ end
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blox
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kim Burgestrand
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - kim@burgestrand.se
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - blox.gemspec
27
+ - blox.rb
28
+ homepage: http://github.com/Burgestrand/numerous_blocks
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - .
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.17
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Blox helps you write Ruby methods that accept multiple blocks
52
+ test_files:
53
+ - blox.rb
54
+ has_rdoc: