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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d8e09f17dd32a9ac41b6d2dff013ae31db37f53
4
- data.tar.gz: 930519fee7dc11a0b55803654c4287b264377b90
3
+ metadata.gz: 57391a740400e9e80e52dad9b0c1d7e029881669
4
+ data.tar.gz: 5da2fff261a2a08fbaa14758d3360e46f85c4584
5
5
  SHA512:
6
- metadata.gz: 61d153b46fb7b713f5189f1af0c835110ceb13f84b046b1d1c0859421c1a210c256fd008e421687e06725a124449a8806bef089fb3bde5ca7bc09c3c381db3df
7
- data.tar.gz: 1ffcf26ce051603210fa4ad0b97a84e206dee9144f229cf99ea1bc9b8194733febdfa8a360d6359c0b5fd875ef90607a9f16af13848ed0663cefa18fa87d17de
6
+ metadata.gz: 402edc5d3604d0c25f63d2b85a5fac0d7baf53fa64f555653c28d7b4aed777c14c053d88fb885e2929f73d5ad743b9f4e3379db3ffd5e1ee4f8e274889e51db9
7
+ data.tar.gz: cfaa6d2cfd0ea7cfa4037dc1f481824ad6e9689a99ca4059e9781f2678433643b26f91f742d7bdd4c9d90fd70e17d1a653b81f1fa5420a5e39ccd6bab4ed25fd
data/README.md CHANGED
@@ -1,41 +1,83 @@
1
1
  # Runaround
2
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
3
+ An Easy Callback System for Ruby Objects
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
7
+ Add `gem 'runaround'` to your Gemfile
10
8
 
11
- ```ruby
12
- gem 'runaround'
13
- ```
9
+ ## Usage
14
10
 
15
- And then execute:
11
+ `Runaround` can be used to add `before`, `after`, and `around` callbacks to your ruby objects.
16
12
 
17
- $ bundle
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
- Or install it yourself as:
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
- $ gem install runaround
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
- ## Usage
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
- TODO: Write usage instructions here
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/[USERNAME]/runaround.
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
-
@@ -15,7 +15,8 @@ module Runaround
15
15
  end
16
16
 
17
17
  def runaround_instance_methods
18
- @runaround_instance_methods ||= Manager.new(self, apply: false)
18
+ @runaround_instance_methods ||= Manager.new(
19
+ self, apply: false, for_instances: true)
19
20
  end
20
21
 
21
22
  def irunaround
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Runaround
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
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: 0.1.0
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-10-31 00:00:00.000000000 Z
11
+ date: 2016-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake