named_return 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/named_return/core.rb +44 -0
- data/lib/named_return/proxy.rb +39 -0
- data/lib/named_return/version.rb +3 -0
- data/lib/named_return/wrapped_method.rb +87 -0
- data/lib/named_return.rb +39 -0
- data/named_return.gemspec +27 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f252510b6986e02f0ef431ca08b045c722092bc
|
4
|
+
data.tar.gz: 91bf016d226be3a6ff5221212835c9e62fc39b1a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ac5b138b9d6bb2e5f8e62348367273279a13ca3dbe89cc5defa8bd05577642a171be5bf33677950d18ceee9ee0f88f980d7165da04b741102f51366a36ccde88
|
7
|
+
data.tar.gz: 70659daf030d1dbec058b62d713cf5ed2e77ca2c0335c676c76af6c6f914f6d5f2fd500275a6f3b47ca1224fe9705763a8372a807eecf8c9182d0556c3a8396a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Simon George
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# NamedReturn
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/named_return`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'named_return'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install named_return
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/named_return.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "named_return"
|
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
@@ -0,0 +1,44 @@
|
|
1
|
+
require "named_return/proxy"
|
2
|
+
|
3
|
+
module NamedReturn
|
4
|
+
# Mix in to your class to make the magic happen.
|
5
|
+
module Core
|
6
|
+
# Class methods that get extended.
|
7
|
+
module ClassMethods
|
8
|
+
# "decorator" to call before defining a method to wrap that method
|
9
|
+
def named_return
|
10
|
+
_named_return_proxy.wrap_method = true
|
11
|
+
end
|
12
|
+
|
13
|
+
# local configuration, e.g. `Foo.named_return_config.test = true`
|
14
|
+
def named_return_config
|
15
|
+
_named_return_proxy.config
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_added(name)
|
19
|
+
_named_return_proxy.wrap self, instance_method(name)
|
20
|
+
end
|
21
|
+
|
22
|
+
def singleton_method_added(name)
|
23
|
+
_named_return_proxy.wrap self, method(name), :_singleton
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
# raw options that later get set on config
|
29
|
+
attr_accessor :options
|
30
|
+
end
|
31
|
+
|
32
|
+
# Set config options local to your class.
|
33
|
+
def self.[](**options)
|
34
|
+
clone.tap { |cls| cls.options = options }
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.included(obj)
|
38
|
+
proxy = Proxy.new(options || {}) # creating a closure
|
39
|
+
obj.send(:define_singleton_method, :_named_return_proxy) { proxy }
|
40
|
+
# obj.send(:private, :_named_return_proxy)
|
41
|
+
obj.extend clone.const_get(:ClassMethods)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "named_return/wrapped_method"
|
2
|
+
|
3
|
+
module NamedReturn
|
4
|
+
# The Core mixin calls this class to wrap methods with the catch DSL.
|
5
|
+
class Proxy
|
6
|
+
attr_accessor :wrap_method, :config
|
7
|
+
|
8
|
+
def initialize(**options)
|
9
|
+
@config = NamedReturn.config.clone
|
10
|
+
options.each { |option, value| config.send(:"#{option}=", value) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def wrap(obj, meth, singleton = nil)
|
14
|
+
@wrap_method ||= config.auto_wrap?(meth.name, singleton)
|
15
|
+
return unless @wrap_method == true # not just truthy, true for reals
|
16
|
+
|
17
|
+
@wrap_method = :overriding # this prevents infinite loops :P
|
18
|
+
wrapped_method = WrappedMethod.new meth, config
|
19
|
+
override_method(obj, wrapped_method, singleton)
|
20
|
+
@wrap_method = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def override_method(obj, wrapped, singleton)
|
26
|
+
base = self
|
27
|
+
obj.send(:"define#{singleton}_method", wrapped.name) do |*args, &block|
|
28
|
+
wrapped.reset(self)
|
29
|
+
# skip the magic if test mode is on
|
30
|
+
return wrapped.call_original(*args, &block) if base.config.test
|
31
|
+
|
32
|
+
return wrapped unless block
|
33
|
+
|
34
|
+
block.call(wrapped)
|
35
|
+
wrapped.call(*args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module NamedReturn
|
2
|
+
# Wrapper to store the original method with a DSL to catch named returns.
|
3
|
+
class WrappedMethod
|
4
|
+
attr_reader :config
|
5
|
+
|
6
|
+
# Wrap a method with catch DSL and pass in config.
|
7
|
+
def initialize(method, config)
|
8
|
+
@method = method
|
9
|
+
@config = config
|
10
|
+
end
|
11
|
+
|
12
|
+
# Get wrapped method name.
|
13
|
+
def name
|
14
|
+
@method.name
|
15
|
+
end
|
16
|
+
|
17
|
+
# DSL to add nested catch statements.
|
18
|
+
def on(label, &block)
|
19
|
+
@callbacks[label] = block
|
20
|
+
end
|
21
|
+
|
22
|
+
# Prepare for next run.
|
23
|
+
def reset(bind)
|
24
|
+
@callbacks = {}
|
25
|
+
@response = {}
|
26
|
+
bind(bind)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Call the wrapped method with catches inserted by the DSL.
|
30
|
+
def call(*args, &block)
|
31
|
+
return_pair = recursive_catch(@callbacks.to_a, *args, &block)
|
32
|
+
label, value = @response.first
|
33
|
+
value = return_pair unless label # important swizzle for no `on` blocks
|
34
|
+
|
35
|
+
if value.is_a?(Array) && value.first == :return
|
36
|
+
# NB in this case `label` will be wrong (nil or outermost `catch`),
|
37
|
+
# so we return a value where first element is the "real" label :return
|
38
|
+
# and the last element is the actual value returned
|
39
|
+
handle_return(value.last)
|
40
|
+
else
|
41
|
+
@callbacks[label].call(value)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Call the wrapped method directly.
|
46
|
+
def call_original(*args, &block)
|
47
|
+
@method.call(*args, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# Ensure the wrapped method is bound to the correct instance if necessary.
|
53
|
+
def bind(obj)
|
54
|
+
return if obj.class == Class
|
55
|
+
|
56
|
+
@method = @method.unbind if @method.respond_to? :unbind
|
57
|
+
@method = @method.bind(obj)
|
58
|
+
end
|
59
|
+
|
60
|
+
def recursive_catch(callbacks, *args, &block)
|
61
|
+
# this return format allows returns to be "re-thrown" or raised
|
62
|
+
return [:return, call_original(*args, &block)] if callbacks.empty?
|
63
|
+
|
64
|
+
label, _callback = callbacks.pop
|
65
|
+
# the all important catch, throws zip up the stack here
|
66
|
+
@response[label] = catch(label) do
|
67
|
+
# recurse using __method__ so renaming is less brittle
|
68
|
+
send __method__, callbacks, *args, &block
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def handle_return(value)
|
73
|
+
case config.return
|
74
|
+
when :raise
|
75
|
+
# there may be a better exception but ArgumentError get the point over
|
76
|
+
raise ArgumentError, "returned without throwing", caller(2)
|
77
|
+
when :throw
|
78
|
+
# we're not really re-throwing, just faking the behaviour ;)
|
79
|
+
raise UncaughtThrowError.new(:return, value), "uncaught throw :return",
|
80
|
+
caller(2) unless @callbacks[:return]
|
81
|
+
@callbacks[:return].call(value)
|
82
|
+
else
|
83
|
+
value
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/lib/named_return.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "named_return/version"
|
2
|
+
require "named_return/core"
|
3
|
+
|
4
|
+
# Named return paths using throw and a DSL around catch.
|
5
|
+
module NamedReturn
|
6
|
+
DefaultConfig = Struct.new(
|
7
|
+
# valid config options
|
8
|
+
*%i(return only class_only except class_except test)
|
9
|
+
) do
|
10
|
+
# Sets defaults.
|
11
|
+
def initialize
|
12
|
+
self.return = false
|
13
|
+
self.only = []
|
14
|
+
self.class_only = []
|
15
|
+
end
|
16
|
+
|
17
|
+
# Should a method be wrapped by named_return?
|
18
|
+
def auto_wrap?(name, singleton)
|
19
|
+
if singleton
|
20
|
+
class_only.include?(name) ||
|
21
|
+
(class_except && !class_except.include?(name))
|
22
|
+
else
|
23
|
+
only.include?(name) || (except && !except.include?(name))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Global configuration block.
|
29
|
+
def self.configure
|
30
|
+
@config = DefaultConfig.new
|
31
|
+
yield(@config) if block_given?
|
32
|
+
@config
|
33
|
+
end
|
34
|
+
|
35
|
+
# Get configuration.
|
36
|
+
def self.config
|
37
|
+
@config || configure
|
38
|
+
end
|
39
|
+
end
|
@@ -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 "named_return/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "named_return"
|
8
|
+
spec.version = NamedReturn::VERSION
|
9
|
+
spec.authors = ["Simon George"]
|
10
|
+
spec.email = ["simon@sfcgeorge.co.uk"]
|
11
|
+
|
12
|
+
spec.summary = "Named return paths using `throw` and DSL around catch"
|
13
|
+
spec.description = "Named return paths using `throw` and DSL around catch"
|
14
|
+
spec.homepage = "https://www.sfcgeorge.co.uk/"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: named_return
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon George
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Named return paths using `throw` and DSL around catch
|
56
|
+
email:
|
57
|
+
- simon@sfcgeorge.co.uk
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/named_return.rb
|
72
|
+
- lib/named_return/core.rb
|
73
|
+
- lib/named_return/proxy.rb
|
74
|
+
- lib/named_return/version.rb
|
75
|
+
- lib/named_return/wrapped_method.rb
|
76
|
+
- named_return.gemspec
|
77
|
+
homepage: https://www.sfcgeorge.co.uk/
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
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'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Named return paths using `throw` and DSL around catch
|
101
|
+
test_files: []
|