runaround 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +60 -18
- data/lib/runaround/instance_methods.rb +2 -1
- data/lib/runaround/manager.rb +5 -3
- data/lib/runaround/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57391a740400e9e80e52dad9b0c1d7e029881669
|
4
|
+
data.tar.gz: 5da2fff261a2a08fbaa14758d3360e46f85c4584
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 402edc5d3604d0c25f63d2b85a5fac0d7baf53fa64f555653c28d7b4aed777c14c053d88fb885e2929f73d5ad743b9f4e3379db3ffd5e1ee4f8e274889e51db9
|
7
|
+
data.tar.gz: cfaa6d2cfd0ea7cfa4037dc1f481824ad6e9689a99ca4059e9781f2678433643b26f91f742d7bdd4c9d90fd70e17d1a653b81f1fa5420a5e39ccd6bab4ed25fd
|
data/README.md
CHANGED
@@ -1,41 +1,83 @@
|
|
1
1
|
# Runaround
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
An Easy Callback System for Ruby Objects
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
Add
|
7
|
+
Add `gem 'runaround'` to your Gemfile
|
10
8
|
|
11
|
-
|
12
|
-
gem 'runaround'
|
13
|
-
```
|
9
|
+
## Usage
|
14
10
|
|
15
|
-
|
11
|
+
`Runaround` can be used to add `before`, `after`, and `around` callbacks to your ruby objects.
|
16
12
|
|
17
|
-
|
13
|
+
#### Callbacks on a specific object instance
|
14
|
+
```ruby
|
15
|
+
class Subtractor
|
16
|
+
include Runaround
|
17
|
+
def subtract(a,b)
|
18
|
+
a - b
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
object = Subtractor.new
|
23
|
+
object.runaround.before(:subtract){ |mc| mc.args.reverse! }
|
24
|
+
object.subtract(7,4)
|
25
|
+
=> -3
|
26
|
+
Subtractor.new.subtract(7,4)
|
27
|
+
=> 3
|
28
|
+
```
|
18
29
|
|
19
|
-
|
30
|
+
#### Callbacks on class methods
|
31
|
+
```ruby
|
32
|
+
class Formatter
|
33
|
+
extend Runaround
|
34
|
+
def self.format(string)
|
35
|
+
string.downcase.tr('[w m]', '[m w]')
|
36
|
+
end
|
37
|
+
runaround.after(:format){ |mc| mc.return_value += '!' }
|
38
|
+
end
|
39
|
+
|
40
|
+
Formatter.format('WALMART')
|
41
|
+
=> 'malwart!'
|
42
|
+
```
|
20
43
|
|
21
|
-
|
44
|
+
#### Callbacks on instance methods
|
45
|
+
```ruby
|
46
|
+
require 'json'
|
47
|
+
class Worker
|
48
|
+
extend Runaround::InstanceMethods
|
49
|
+
def work(**opts)
|
50
|
+
opts.to_json
|
51
|
+
end
|
52
|
+
irunaround.around(:work) do |mc|
|
53
|
+
puts " BEFORE WORK"
|
54
|
+
mc.opts[:foo_id] = 12345
|
55
|
+
result = mc.run_method
|
56
|
+
puts " WORK COMPLETE, GOT: #{result.inspect}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
worker = Worker.new
|
61
|
+
worker.work(thing: 'one')
|
62
|
+
BEFORE WORK
|
63
|
+
WORK COMPLETE, GOT: "{\"thing\":\"one\",\"foo_id\":12345}"
|
64
|
+
=> "{\"thing\":\"one\",\"foo_id\":12345}"
|
65
|
+
```
|
22
66
|
|
23
|
-
|
67
|
+
#### More Details
|
68
|
+
Callbacks are passed a [Runaround::MethodCall](https://github.com/fledman/runaround/blob/master/lib/runaround/method_call.rb) struct. This allows for manipulation of the input arguments/options as well as the return value. Be careful when using this functionality.
|
24
69
|
|
25
|
-
|
70
|
+
`before` and `after` callbacks are implemented using simple blocks, while `around` callbacks are implemented using `Fiber`s.
|
71
|
+
As such, `MethodCall#run_method` is only used for `around` callbacks. All `around` callbacks must call it.
|
26
72
|
|
27
73
|
## Development
|
28
74
|
|
29
75
|
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
76
|
|
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
77
|
## Contributing
|
34
78
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
36
|
-
|
79
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fledman/runaround.
|
37
80
|
|
38
81
|
## License
|
39
82
|
|
40
83
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
-
|
data/lib/runaround/manager.rb
CHANGED
@@ -4,11 +4,12 @@ require "runaround/errors"
|
|
4
4
|
|
5
5
|
module Runaround
|
6
6
|
class Manager
|
7
|
-
attr_reader :receiver, :apply
|
7
|
+
attr_reader :receiver, :apply, :for_instances
|
8
8
|
|
9
|
-
def initialize(receiver, apply: true)
|
9
|
+
def initialize(receiver, apply: true, for_instances: false)
|
10
10
|
@receiver = receiver
|
11
11
|
@apply = !!apply
|
12
|
+
@for_instances = !!for_instances
|
12
13
|
end
|
13
14
|
|
14
15
|
def before(method, fifo: true, &block)
|
@@ -75,7 +76,8 @@ module Runaround
|
|
75
76
|
end
|
76
77
|
|
77
78
|
def validate_method!(method)
|
78
|
-
return if receiver.respond_to?(method)
|
79
|
+
return if !for_instances && receiver.respond_to?(method)
|
80
|
+
return if for_instances && receiver.method_defined?(method)
|
79
81
|
msg = "the receiver does not respond to #{method.inspect}"
|
80
82
|
raise CallbackSetupError, "#{msg} ==> receiver.inspect"
|
81
83
|
end
|
data/lib/runaround/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: runaround
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Feldman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|