callbacks_attachable 2.3.1 → 3.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 +8 -25
- data/lib/all/callback.rb +8 -10
- data/lib/all/callback_registry.rb +0 -4
- data/lib/all/callbacks_attachable.rb +14 -18
- data/lib/all/version.rb +1 -1
- data/mrbgem.rake +7 -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: b74554af0366433cb7bcecddc081c5e906e0ab91
|
4
|
+
data.tar.gz: 5c86c645b70577ea1fd03982f0aa4151245f40d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45b1e71f8b7b91c38977e9f3acd9d0e404cd12c05ee4dbba63b9a45331319acea6cf0d6ad572fe030a0523603fc23a735e5ec2be432c142a368f0ce71502899a
|
7
|
+
data.tar.gz: 362bdae78978be2933f04696e7515229b4ce7988c95e2b280d06c6ddb626d0e0e19278e6c16a2319c302f3d6448e8231e58cc7fa0903c6ac4de9282cc9eb847e
|
data/README.md
CHANGED
@@ -34,15 +34,15 @@ end
|
|
34
34
|
Attach callbacks for an event for all instances of a class with:
|
35
35
|
|
36
36
|
```ruby
|
37
|
-
|
37
|
+
AClass.on(:event) do |method|
|
38
38
|
puts __send__(method)
|
39
39
|
end
|
40
40
|
|
41
41
|
instance0 = AClass.new(value: 0)
|
42
42
|
instance1 = AClass.new(value: 1)
|
43
43
|
|
44
|
-
|
45
|
-
|
44
|
+
instance0.trigger(:event, :value) # => 0
|
45
|
+
instance1.trigger(:event, :value) # => 1
|
46
46
|
```
|
47
47
|
|
48
48
|
Callbacks attached to a class are evaluated in the context of the instance they
|
@@ -68,34 +68,17 @@ If you want to execute a callback just a single time attach it with `.once_on`:
|
|
68
68
|
|
69
69
|
```ruby
|
70
70
|
AClass.once_on(:singular) { puts 'callback called!' }
|
71
|
-
AClass.trigger(:singular) # => puts 'callback called!' and immediately
|
71
|
+
AClass.new.trigger(:singular) # => puts 'callback called!' and immediately
|
72
72
|
# detaches the callback
|
73
|
-
AClass.trigger(:singular) # => does nothing
|
73
|
+
AClass.new.trigger(:singular) # => does nothing
|
74
74
|
```
|
75
75
|
|
76
|
-
To filter the instances to call the callback for, use the `:if` option:
|
77
|
-
|
78
|
-
```ruby
|
79
|
-
instance0 = AClass.new(value: 0)
|
80
|
-
instance1 = AClass.new(value: 1)
|
81
|
-
instance2 = AClass.new(value: 2)
|
82
|
-
AClass.on(:even, if: proc{ |inst| inst.value.even? }) do |instance|
|
83
|
-
puts instance.value
|
84
|
-
end
|
85
|
-
|
86
|
-
AClass.trigger(:even) # => 0
|
87
|
-
# 2
|
88
|
-
```
|
89
|
-
|
90
|
-
This is especially useful in combination with `.once_on` to execute the
|
91
|
-
callback just for the first instance meeting certain criteria.
|
92
|
-
|
93
76
|
### Callbacks attached to an instance
|
94
77
|
|
95
78
|
All above mentioned methods on the class level also exist for each instance of
|
96
|
-
the class.
|
97
|
-
block was defined, just like normal blocks
|
98
|
-
same as outside of it.
|
79
|
+
the class. They behave the same with the one exception that callbacks are
|
80
|
+
executed bound to the context its block was defined, just like normal blocks
|
81
|
+
are: `self` inside a callback is the same as outside of it.
|
99
82
|
|
100
83
|
Callbacks for an individual instance are executed by calling `#trigger` on it.
|
101
84
|
This also executes callbacks attached to the class.
|
data/lib/all/callback.rb
CHANGED
@@ -3,21 +3,19 @@ module CallbacksAttachable
|
|
3
3
|
def initialize(registry, events, opts, instance_scope, callback)
|
4
4
|
@registry = registry
|
5
5
|
@events = events
|
6
|
-
@
|
7
|
-
@cancel_condition = opts.fetch(:until, false)
|
6
|
+
@once = opts.fetch(:once?, false)
|
8
7
|
@instance_scope = instance_scope
|
9
8
|
@callback = callback
|
10
|
-
@
|
9
|
+
@cancelled = false
|
11
10
|
end
|
12
11
|
|
13
12
|
def call(instance, args)
|
14
|
-
return if @call_condition and not @call_condition.call instance, *args
|
15
13
|
if @instance_scope
|
16
14
|
instance.instance_exec(*args, &@callback)
|
17
15
|
else
|
18
16
|
@callback.call(*args)
|
19
17
|
end
|
20
|
-
cancel if @
|
18
|
+
cancel if @once
|
21
19
|
end
|
22
20
|
|
23
21
|
def on_cancel(&on_cancel)
|
@@ -25,17 +23,17 @@ module CallbacksAttachable
|
|
25
23
|
end
|
26
24
|
|
27
25
|
def cancel
|
28
|
-
if @
|
29
|
-
raise Error, 'already
|
26
|
+
if @cancelled
|
27
|
+
raise Error, 'already cancelled'
|
30
28
|
else
|
31
29
|
@events.each{ |event| @registry.deregister event, self }
|
32
30
|
@on_cancel.call if @on_cancel
|
33
|
-
@
|
31
|
+
@cancelled = true
|
34
32
|
end
|
35
33
|
end
|
36
34
|
|
37
|
-
def
|
38
|
-
@
|
35
|
+
def cancelled?
|
36
|
+
@cancelled
|
39
37
|
end
|
40
38
|
end
|
41
39
|
end
|
@@ -6,10 +6,6 @@ module CallbacksAttachable
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def register(*events, opts, callback)
|
9
|
-
unless opts.is_a? Hash
|
10
|
-
events << opts
|
11
|
-
opts = {}
|
12
|
-
end
|
13
9
|
callback = Callback.new(self, events, opts, !@singleton_owner, callback)
|
14
10
|
events.each do |event|
|
15
11
|
@callbacks[event] ||= {}
|
@@ -8,26 +8,21 @@ module CallbacksAttachable
|
|
8
8
|
CallbacksAttachable.included klass
|
9
9
|
end
|
10
10
|
|
11
|
-
def on(
|
12
|
-
__callbacks__.register(
|
11
|
+
def on(*events, &callback)
|
12
|
+
__callbacks__.register(*events, @on_opts ? @on_opts : {}, callback)
|
13
13
|
end
|
14
14
|
|
15
|
-
def once_on(
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def once_on(*events, &callback)
|
16
|
+
@on_opts = {once?: true}
|
17
|
+
on *events, &callback
|
18
|
+
ensure
|
19
|
+
@on_opts = nil
|
19
20
|
end
|
20
21
|
|
21
22
|
def on?(event)
|
22
23
|
@__callbacks__ ? (@__callbacks__.registered? event) : false
|
23
24
|
end
|
24
25
|
|
25
|
-
def trigger(event, *args)
|
26
|
-
ObjectSpace.each_object(self).each do |inst|
|
27
|
-
trigger_for_instance(inst, event, args)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
26
|
def trigger_for_instance(inst, event, args)
|
32
27
|
if superclass.respond_to? :trigger_for_instance
|
33
28
|
superclass.trigger_for_instance(inst, event, args)
|
@@ -49,14 +44,15 @@ module CallbacksAttachable
|
|
49
44
|
klass.extend RegistryOwnable
|
50
45
|
end
|
51
46
|
|
52
|
-
def on(
|
53
|
-
singleton_class.on
|
47
|
+
def on(*events, &callback)
|
48
|
+
singleton_class.on *events, &callback
|
54
49
|
end
|
55
50
|
|
56
|
-
def once_on(
|
57
|
-
|
58
|
-
|
59
|
-
|
51
|
+
def once_on(*events, &callback)
|
52
|
+
singleton_class.instance_variable_set :@on_opts, {once?: true}
|
53
|
+
on *events, &callback
|
54
|
+
ensure
|
55
|
+
singleton_class.remove_instance_variable :@on_opts
|
60
56
|
end
|
61
57
|
|
62
58
|
def on?(event)
|
data/lib/all/version.rb
CHANGED
data/mrbgem.rake
CHANGED
@@ -12,7 +12,6 @@ DESC
|
|
12
12
|
spec.license = 'MIT'
|
13
13
|
spec.authors = ['Christopher Aue']
|
14
14
|
|
15
|
-
spec.add_dependency 'mruby-objectspace', :core => 'mruby-objectspace'
|
16
15
|
spec.add_dependency 'mruby-object-ext', :core => 'mruby-object-ext'
|
17
16
|
|
18
17
|
spec.objs = Dir["#{spec.dir}/ext/mruby/**/*.c"].sort.map do |f|
|
@@ -21,4 +20,11 @@ DESC
|
|
21
20
|
spec.rbfiles =
|
22
21
|
Dir["#{spec.dir}/lib/all/**/*.rb"].sort +
|
23
22
|
Dir["#{spec.dir}/lib/mruby/**/*.rb"].sort
|
23
|
+
|
24
|
+
unless system("git merge-base --is-ancestor 5a9eedf5417266b82e3695ae0c29797182a5d04e HEAD")
|
25
|
+
# mruby commit 5a9eedf fixed the usage of spec.rbfiles. mruby 1.3.0
|
26
|
+
# did not have that commit, yet. Add the patch for this case:
|
27
|
+
@generate_functions = true
|
28
|
+
@objs << objfile("#{build_dir}/gem_init")
|
29
|
+
end
|
24
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callbacks_attachable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.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-10-
|
11
|
+
date: 2017-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
Attach callbacks to classes to be triggered for all instances or attach them
|