switchblock 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +121 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/switchblock.rb +43 -0
- data/lib/switchblock/version.rb +3 -0
- data/switchblock.gemspec +27 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d63a531ba8a7f9eb73e9a61af6f5db773bf6dad4
|
4
|
+
data.tar.gz: 316df4c6f8809f3c0322bf16f8d2ea3e2b4e7145
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f15078869af57b8b50af31f5cfaade53c6ee2a601bd5d3051c2a743626a0cb82483681b3d31f6c0cff570f64363b13f5ca798dcc3ac137ee487e91b0c0eb56ef
|
7
|
+
data.tar.gz: 8852f1870c7b7e8fe1b9ff755bc570325db821b06a9285035805678ecd49c69e51fc98ae0740f145daa7826e038858ee7f4049309aa9f7cf3fa7c17b4fb28bdb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# Switchblock - A way to pass multiple blocks to a method
|
2
|
+
|
3
|
+
Switchblock is a module mixin that adds a simple way to pass multiple blocks to methods to your classes. It does this by creating an object that acts as a switch block.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
First, you must ```include Switchblock``` in your class (or your program). You can now write methods that take multiple blocks using the "it_to" method like this:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
def did_we_succeed?(success)
|
11
|
+
if success
|
12
|
+
yield it_to "success"
|
13
|
+
else
|
14
|
+
yield it_to "failure"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
did_we_succeed?(true) do |s|
|
19
|
+
s.success {"Yes!!"}
|
20
|
+
s.failure {"Aw... Bummer..."}
|
21
|
+
end
|
22
|
+
|
23
|
+
=> "Yes!!"
|
24
|
+
```
|
25
|
+
|
26
|
+
The call to it_to will give you BlockSwitcher, which will take care of the block you give to the method and execute just the subblock(s) you want.
|
27
|
+
|
28
|
+
## Features
|
29
|
+
|
30
|
+
As seen in the usage example, you can pass an unlimited number of subblocks to your BlockSwitcher. These will be executed if the method you call on the BlockSwitcher matches whatever name you specified in the "yield it_to" call.
|
31
|
+
|
32
|
+
### Switches
|
33
|
+
|
34
|
+
Switches are really simple, just make up a name and use it to call one your subblocks by yielding to it in your "yield it_to" call. The first argument to "it_to" is the name of the block you want to call. You can use anything that has a #to_sym method.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
def yield_name(name)
|
38
|
+
yield it_to name
|
39
|
+
end
|
40
|
+
|
41
|
+
yield_name("Larry") do |s|
|
42
|
+
s.larry {"Hi, Larry!}
|
43
|
+
end
|
44
|
+
|
45
|
+
=> "Hi, Larry!"
|
46
|
+
```
|
47
|
+
|
48
|
+
Instead of blocks, you can also use a proc as the argument to a switch.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
larry_greeter = lambda {"Hi, Larry"}
|
52
|
+
|
53
|
+
yield_name("Larry") do |s|
|
54
|
+
s.larry larry_greeter
|
55
|
+
end
|
56
|
+
|
57
|
+
=> "Hi, Larry!"
|
58
|
+
```
|
59
|
+
|
60
|
+
Any additional arguments you yield after the switch name will be passed on to your subblock.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
def even_or_odd(x)
|
64
|
+
if x.even?
|
65
|
+
yield it_to "even", x
|
66
|
+
else
|
67
|
+
yield it_to "odd", x
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
even_or_odd(5) do |s|
|
72
|
+
s.even {|x| "#{x} is an even number" }
|
73
|
+
s.odd {|x| "#{x} is an odd number" }
|
74
|
+
end
|
75
|
+
|
76
|
+
=> "5 is an odd number"
|
77
|
+
```
|
78
|
+
|
79
|
+
The return value of your "yield it_to" call will be the return value of the last executed subblock. If nothing at all is executed, the whole block will return nil. It will raise an error if you use "ensure" (see below).
|
80
|
+
|
81
|
+
### Special Switches
|
82
|
+
You can use any name for your switch, but "else", "always" and "ensure" have a special meaning and are treated differently.
|
83
|
+
|
84
|
+
#### always
|
85
|
+
|
86
|
+
You can use "always" as often as you wish, just as normal switches, and it will always execute no matter what.
|
87
|
+
|
88
|
+
#### else
|
89
|
+
|
90
|
+
You should use "else" only as the last switch of your switchblock. The block you give to it will only execute if no previous block executed before. You can use this to implement default behaviour.
|
91
|
+
|
92
|
+
#### ensure
|
93
|
+
You should use "ensure" only as as the last switch of your switchblock. It will ignore all passed blocks and arguments. It makes sure that at least one of your switches was called during execution and will raise a NothingExecutedError otherwise.
|
94
|
+
|
95
|
+
You can use this to protect against typos. For example, you mistype and write "yield it_to 'succes'" and you have no idea why the block passed to "s.success" in your switchblock is not executed. Using ensure will give you a nice error message in this case, telling you what should have been executed and what was actually present.
|
96
|
+
|
97
|
+
Needless to say, you should probably not use "ensure" together with "always" or "else". You can though, if you are really clever (or just feel like it).
|
98
|
+
|
99
|
+
## Installation
|
100
|
+
|
101
|
+
Add this line to your application's Gemfile:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
gem 'switchblock'
|
105
|
+
```
|
106
|
+
|
107
|
+
And then execute:
|
108
|
+
|
109
|
+
$ bundle
|
110
|
+
|
111
|
+
Or install it yourself as:
|
112
|
+
|
113
|
+
$ gem install switchblock
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
1. Fork it ( https://github.com/[my-github-username]/switchblock/fork )
|
118
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
119
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
120
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
121
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "switchblock"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/switchblock.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "switchblock/version"
|
2
|
+
require "blankslate"
|
3
|
+
|
4
|
+
module Switchblock
|
5
|
+
|
6
|
+
class NothingExecutedError < Exception; end
|
7
|
+
|
8
|
+
class BlockSwitcher < BlankSlate
|
9
|
+
|
10
|
+
def initialize(called_block, args)
|
11
|
+
@called_block = called_block
|
12
|
+
@args = args
|
13
|
+
@ret = nil
|
14
|
+
@called = false
|
15
|
+
@callees = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def ensure
|
19
|
+
raise NothingExecutedError, "Nothing got executed! Expected one of #{@callees.inspect}, but only got #{@called_block}" if not @called
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(method,*args, &block)
|
23
|
+
if (method.downcase == @called_block.to_sym.downcase or method == :always) or (@called == false and method == :else) then
|
24
|
+
@called = true
|
25
|
+
if args.size > 0 then
|
26
|
+
@ret = args.first.call(*@args)
|
27
|
+
else
|
28
|
+
@ret = block.call(*@args)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
# For error reporting
|
32
|
+
@callees << method
|
33
|
+
@callees.uniq!
|
34
|
+
# Always return something meaningful
|
35
|
+
@ret
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def it_to(called_block, *args)
|
41
|
+
BlockSwitcher.new(called_block, args)
|
42
|
+
end
|
43
|
+
end
|
data/switchblock.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'switchblock/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "switchblock"
|
8
|
+
spec.version = Switchblock::VERSION
|
9
|
+
spec.authors = ["Roger Braun"]
|
10
|
+
spec.email = ["roger@rogerbraun.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{A module that helps you pass multiple blocks to a method.}
|
13
|
+
spec.homepage = "http://github.com/rogerbraun/switchblock"
|
14
|
+
|
15
|
+
spec.license = 'GPL-3.0'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "blankslate"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: switchblock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roger Braun
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: blankslate
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- roger@rogerbraun.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/switchblock.rb
|
85
|
+
- lib/switchblock/version.rb
|
86
|
+
- switchblock.gemspec
|
87
|
+
homepage: http://github.com/rogerbraun/switchblock
|
88
|
+
licenses:
|
89
|
+
- GPL-3.0
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.4.5
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: A module that helps you pass multiple blocks to a method.
|
111
|
+
test_files: []
|