callbacks_attachable 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/README.md +6 -0
- data/callbacks_attachable.gemspec +0 -5
- data/mrblib/callback.rb +3 -3
- data/mrblib/callback_registry.rb +10 -4
- data/mrblib/callbacks_attachable.rb +12 -8
- data/mrblib/version.rb +1 -1
- metadata +4 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5704f2e0b5be12b67d21705fd058832d8b43c768
|
4
|
+
data.tar.gz: af96252e63b77c3d83b99f9c0b1df8d75144e916
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 908846d84dcba7ffa75ee38efd635e6c600015cfd029d39cc83b08ece263a80ec2b66c87c916d5d534aeff5e75d5684e34126109d8374a18e8b5763a9a7067c4
|
7
|
+
data.tar.gz: 619ad1b6ea59e844fb2e54bd2835ca7b1637b662a1dec721cfa84efb06ecca1b973e2e1a984c7afee60f05be34c55326dd3989969be5586837c3c6a079f2fe75
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -58,6 +58,12 @@ callback = AClass.on(:event) { do_something }
|
|
58
58
|
callback.cancel
|
59
59
|
```
|
60
60
|
|
61
|
+
A callback can also be registered for multiple events:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
AClass.on(:event1, :event2, :event3, opts, &callback)
|
65
|
+
```
|
66
|
+
|
61
67
|
If you want to execute a callback just a single time attach it with `.once_on`:
|
62
68
|
|
63
69
|
```ruby
|
@@ -1,4 +1,3 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
require_relative 'mrblib/version'
|
3
2
|
|
4
3
|
Gem::Specification.new do |spec|
|
@@ -18,8 +17,4 @@ to an individual instance to be triggered only for this instance.
|
|
18
17
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
19
|
spec.require_paths = ["lib"]
|
21
|
-
|
22
|
-
spec.add_development_dependency "bundler", "~> 1.8"
|
23
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
-
spec.add_development_dependency "rspec", "~> 3.4"
|
25
20
|
end
|
data/mrblib/callback.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module CallbacksAttachable
|
2
2
|
class Callback
|
3
|
-
def initialize(registry,
|
3
|
+
def initialize(registry, events, opts, instance_scope, callback)
|
4
4
|
@registry = registry
|
5
|
-
@
|
5
|
+
@events = events
|
6
6
|
@call_condition = opts.fetch(:if, false)
|
7
7
|
@cancel_condition = opts.fetch(:until, false)
|
8
8
|
@instance_scope = instance_scope
|
@@ -28,7 +28,7 @@ module CallbacksAttachable
|
|
28
28
|
if @canceled
|
29
29
|
raise Error, 'already canceled'
|
30
30
|
else
|
31
|
-
@registry.deregister
|
31
|
+
@events.each{ |event| @registry.deregister event, self }
|
32
32
|
@on_cancel.call if @on_cancel
|
33
33
|
@canceled = true
|
34
34
|
end
|
data/mrblib/callback_registry.rb
CHANGED
@@ -5,10 +5,16 @@ module CallbacksAttachable
|
|
5
5
|
@callbacks = {}
|
6
6
|
end
|
7
7
|
|
8
|
-
def register(
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
def register(*events, opts, callback)
|
9
|
+
unless opts.is_a? Hash
|
10
|
+
events << opts
|
11
|
+
opts = {}
|
12
|
+
end
|
13
|
+
callback = Callback.new(self, events, opts, !@singleton_owner, callback)
|
14
|
+
events.each do |event|
|
15
|
+
@callbacks[event] ||= {}
|
16
|
+
@callbacks[event][callback] = true
|
17
|
+
end
|
12
18
|
callback
|
13
19
|
end
|
14
20
|
|
@@ -8,12 +8,14 @@ module CallbacksAttachable
|
|
8
8
|
CallbacksAttachable.included klass
|
9
9
|
end
|
10
10
|
|
11
|
-
def on(event,
|
12
|
-
__callbacks__.register(event,
|
11
|
+
def on(event, *more_events_and_opts, &callback)
|
12
|
+
__callbacks__.register(event, *more_events_and_opts, callback)
|
13
13
|
end
|
14
14
|
|
15
|
-
def once_on(event,
|
16
|
-
|
15
|
+
def once_on(event, *more_events_and_opts, &callback)
|
16
|
+
opts = (more_events_and_opts.last.is_a? Hash) ? more_events_and_opts.pop : {}
|
17
|
+
opts[:until] = proc{ true }
|
18
|
+
on event, *more_events_and_opts, opts, &callback
|
17
19
|
end
|
18
20
|
|
19
21
|
def on?(event)
|
@@ -47,12 +49,14 @@ module CallbacksAttachable
|
|
47
49
|
klass.extend RegistryOwnable
|
48
50
|
end
|
49
51
|
|
50
|
-
def on(*
|
51
|
-
singleton_class.on *
|
52
|
+
def on(event, *more_events_and_opts, &callback)
|
53
|
+
singleton_class.on event, *more_events_and_opts, &callback
|
52
54
|
end
|
53
55
|
|
54
|
-
def once_on(event,
|
55
|
-
|
56
|
+
def once_on(event, *more_events_and_opts, &callback)
|
57
|
+
opts = (more_events_and_opts.last.is_a? Hash) ? more_events_and_opts.pop : {}
|
58
|
+
opts[:until] = proc{ true }
|
59
|
+
on event, *more_events_and_opts, opts, &callback
|
56
60
|
end
|
57
61
|
|
58
62
|
def on?(event)
|
data/mrblib/version.rb
CHANGED
metadata
CHANGED
@@ -1,57 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callbacks_attachable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Aue
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
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.8'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.8'
|
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.4'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '3.4'
|
11
|
+
date: 2017-07-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
55
13
|
description: |
|
56
14
|
Attach callbacks to classes to be triggered for all instances or attach them
|
57
15
|
to an individual instance to be triggered only for this instance.
|
@@ -95,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
53
|
version: '0'
|
96
54
|
requirements: []
|
97
55
|
rubyforge_project:
|
98
|
-
rubygems_version: 2.6.
|
56
|
+
rubygems_version: 2.6.11
|
99
57
|
signing_key:
|
100
58
|
specification_version: 4
|
101
59
|
summary: Attach callbacks to classes or individual instances.
|