runaround 0.1.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/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +9 -0
- data/bin/setup +8 -0
- data/lib/runaround/callback_hook.rb +22 -0
- data/lib/runaround/errors.rb +3 -0
- data/lib/runaround/instance_methods.rb +28 -0
- data/lib/runaround/manager.rb +123 -0
- data/lib/runaround/method_call.rb +15 -0
- data/lib/runaround/version.rb +3 -0
- data/lib/runaround.rb +11 -0
- data/runaround.gemspec +23 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2d8e09f17dd32a9ac41b6d2dff013ae31db37f53
|
4
|
+
data.tar.gz: 930519fee7dc11a0b55803654c4287b264377b90
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 61d153b46fb7b713f5189f1af0c835110ceb13f84b046b1d1c0859421c1a210c256fd008e421687e06725a124449a8806bef089fb3bde5ca7bc09c3c381db3df
|
7
|
+
data.tar.gz: 1ffcf26ce051603210fa4ad0b97a84e206dee9144f229cf99ea1bc9b8194733febdfa8a360d6359c0b5fd875ef90607a9f16af13848ed0663cefa18fa87d17de
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 David Feldman
|
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
|
+
# Runaround
|
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/runaround`. 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 'runaround'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install runaround
|
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]/runaround.
|
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
data/bin/setup
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "runaround/method_call"
|
2
|
+
|
3
|
+
module Runaround
|
4
|
+
module CallbackHook
|
5
|
+
|
6
|
+
def self.build_for(method, manager)
|
7
|
+
Module.new do
|
8
|
+
define_method(method) do |*args,**opts,&block|
|
9
|
+
mc = MethodCall.new(method, args, opts, block)
|
10
|
+
blocks, fibers = manager.callbacks(method)
|
11
|
+
blocks[:before].each { |b| b.call(mc) }
|
12
|
+
fibers.each { |f| f.resume(mc) }
|
13
|
+
mc.return_value = super(*mc.argsopts, &mc.block)
|
14
|
+
fibers.reverse_each { |f| f.resume(mc.return_value) }
|
15
|
+
blocks[:after].each { |b| b.call(mc) }
|
16
|
+
mc.return_value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Runaround
|
2
|
+
module InstanceMethods
|
3
|
+
|
4
|
+
def self.included(into)
|
5
|
+
raise RuntimeError,
|
6
|
+
"Runaround::InstanceMethods expects to be extended, not included"
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.extended(into)
|
10
|
+
into.extend Runaround
|
11
|
+
into.include Runaround
|
12
|
+
into.runaround.after(:new) do |mc|
|
13
|
+
mc.return_value.runaround.import(into.runaround_instance_methods)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def runaround_instance_methods
|
18
|
+
@runaround_instance_methods ||= Manager.new(self, apply: false)
|
19
|
+
end
|
20
|
+
|
21
|
+
def irunaround
|
22
|
+
runaround_instance_methods.tap do |x|
|
23
|
+
yield(x) if block_given?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require "fiber"
|
2
|
+
require "runaround/callback_hook"
|
3
|
+
require "runaround/errors"
|
4
|
+
|
5
|
+
module Runaround
|
6
|
+
class Manager
|
7
|
+
attr_reader :receiver, :apply
|
8
|
+
|
9
|
+
def initialize(receiver, apply: true)
|
10
|
+
@receiver = receiver
|
11
|
+
@apply = !!apply
|
12
|
+
end
|
13
|
+
|
14
|
+
def before(method, fifo: true, &block)
|
15
|
+
prepare_callback(
|
16
|
+
method: method, type: :before, fifo: fifo, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def after(method, fifo: true, &block)
|
20
|
+
prepare_callback(
|
21
|
+
method: method, type: :after, fifo: fifo, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def around(method, fifo: false, &block)
|
25
|
+
prepare_callback(
|
26
|
+
method: method, type: :around, fifo: fifo, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def callbacks(method)
|
30
|
+
all = all_callbacks(method)
|
31
|
+
blocks = all.dup.tap{ |h| h.delete(:around) }
|
32
|
+
fibers = all[:around].map{ |b| Fiber.new(&b) }
|
33
|
+
return blocks, fibers
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_h
|
37
|
+
callback_hooks.keys.reduce({}) do |h, method|
|
38
|
+
h[method] = copy_callback_map(method); h
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def import(other)
|
43
|
+
validate_manager!(other)
|
44
|
+
other.to_h.each do |method, callbacks|
|
45
|
+
callbacks.each do |type, list|
|
46
|
+
list.each do |block|
|
47
|
+
prepare_callback(
|
48
|
+
method: method, type: type, fifo: true, &block)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def prepare_callback(method:, type:, fifo:, &block)
|
57
|
+
validate_opts!(method, type, fifo, block)
|
58
|
+
setup_callback_hook(method)
|
59
|
+
list = all_callbacks(method)[type]
|
60
|
+
fifo ? list << block : list.unshift(block)
|
61
|
+
list.size
|
62
|
+
end
|
63
|
+
|
64
|
+
def setup_callback_hook(method)
|
65
|
+
return true if callback_hooks[method]
|
66
|
+
callback_hooks[method] = CallbackHook.build_for(method, self)
|
67
|
+
receiver.singleton_class.prepend(callback_hooks[method]) if apply
|
68
|
+
end
|
69
|
+
|
70
|
+
def validate_opts!(method, type, fifo, block)
|
71
|
+
validate_method!(method)
|
72
|
+
validate_type!(type)
|
73
|
+
validate_fifo!(fifo)
|
74
|
+
validate_block!(block)
|
75
|
+
end
|
76
|
+
|
77
|
+
def validate_method!(method)
|
78
|
+
return if receiver.respond_to?(method)
|
79
|
+
msg = "the receiver does not respond to #{method.inspect}"
|
80
|
+
raise CallbackSetupError, "#{msg} ==> receiver.inspect"
|
81
|
+
end
|
82
|
+
|
83
|
+
def validate_type!(type)
|
84
|
+
return if [:before, :after, :around].include?(type)
|
85
|
+
msg = "#{type.inspect} is not a valid callback type"
|
86
|
+
msg += "; must be one of [:before, :after, :around]"
|
87
|
+
raise CallbackSetupError, msg
|
88
|
+
end
|
89
|
+
|
90
|
+
def validate_fifo!(fifo)
|
91
|
+
return if [true, false, nil].include?(fifo)
|
92
|
+
msg = "fifo must be true, false, or nil; got #{fifo.inspect}"
|
93
|
+
raise CallbackSetupError, msg
|
94
|
+
end
|
95
|
+
|
96
|
+
def validate_block!(block)
|
97
|
+
return if block.is_a?(Proc)
|
98
|
+
raise CallbackSetupError, "you must pass a block for the callback!"
|
99
|
+
end
|
100
|
+
|
101
|
+
def validate_manager!(manager)
|
102
|
+
return if manager.is_a?(self.class)
|
103
|
+
msg = "expected a #{self.class}, got: #{manager.inspect}"
|
104
|
+
raise CallbackSetupError, msg
|
105
|
+
end
|
106
|
+
|
107
|
+
def copy_callback_map(method)
|
108
|
+
all_callbacks(method).reduce({}) do |map,(type,arr)|
|
109
|
+
map[type] = arr.dup; map
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def callback_hooks
|
114
|
+
@callback_hooks ||= {}
|
115
|
+
end
|
116
|
+
|
117
|
+
def all_callbacks(method)
|
118
|
+
@all_callbacks ||= {}
|
119
|
+
@all_callbacks[method] ||= { before: [], after: [], around: [] }
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Runaround
|
2
|
+
|
3
|
+
MethodCall = Struct.new(:method, :args, :opts, :block, :return_value) do
|
4
|
+
def run_method
|
5
|
+
Fiber.yield
|
6
|
+
end
|
7
|
+
|
8
|
+
def argsopts
|
9
|
+
argsopts = args ? args.dup : []
|
10
|
+
argsopts << opts if opts && !opts.empty?
|
11
|
+
argsopts
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/lib/runaround.rb
ADDED
data/runaround.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'runaround/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "runaround"
|
8
|
+
spec.version = Runaround::VERSION
|
9
|
+
spec.authors = ["David Feldman"]
|
10
|
+
spec.email = ["dbfeldman@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Easy Callback System for Ruby Objects"
|
13
|
+
spec.description = "Easy Callback System for Ruby Objects"
|
14
|
+
spec.homepage = "https://github.com/fledman/runaround"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
22
|
+
spec.add_development_dependency "pry", "~> 0.10"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: runaround
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Feldman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.10'
|
55
|
+
description: Easy Callback System for Ruby Objects
|
56
|
+
email:
|
57
|
+
- dbfeldman@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/runaround.rb
|
71
|
+
- lib/runaround/callback_hook.rb
|
72
|
+
- lib/runaround/errors.rb
|
73
|
+
- lib/runaround/instance_methods.rb
|
74
|
+
- lib/runaround/manager.rb
|
75
|
+
- lib/runaround/method_call.rb
|
76
|
+
- lib/runaround/version.rb
|
77
|
+
- runaround.gemspec
|
78
|
+
homepage: https://github.com/fledman/runaround
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.4.5.1
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Easy Callback System for Ruby Objects
|
102
|
+
test_files: []
|