callbacks_attachable 2.1.2 → 2.2.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 +9 -6
- data/lib/callbacks_attachable.rb +2 -5
- data/mrbgem.rake +1 -0
- data/mrblib/_ext_module_m.rb +12 -0
- data/mrblib/callback.rb +7 -2
- data/mrblib/callback_registry.rb +3 -3
- data/mrblib/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12ec8224c4c44dbd6b2f911c85dfdee057523d61
|
4
|
+
data.tar.gz: b73d81ff789dc2ba7db11bf0fc7fbc022f79623b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bf6762f10eb1429d222a696e125f7e724ff589c8f4f49a457725471d20931261d35874518cceb336223d54cbe89c7bc6505a2bfcddcb81325af4596f46ba4c4
|
7
|
+
data.tar.gz: ced69ccc31423f7fd0d3255e503890c40043ae313a046babf154561fa931978940dc1c73a3a624b94445a4df62f3b8d2f4ea6b978cdc157aeb0a8b12636964ce
|
data/README.md
CHANGED
@@ -34,8 +34,8 @@ end
|
|
34
34
|
Attach callbacks for an event for all instances of a class with:
|
35
35
|
|
36
36
|
```ruby
|
37
|
-
callback = AClass.on(:event) do |
|
38
|
-
|
37
|
+
callback = AClass.on(:event) do |method|
|
38
|
+
puts __send__(method)
|
39
39
|
end
|
40
40
|
|
41
41
|
instance0 = AClass.new(value: 0)
|
@@ -45,9 +45,10 @@ AClass.trigger(:event, :value) # => 0
|
|
45
45
|
# 1
|
46
46
|
```
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
callback
|
48
|
+
Callbacks attached to a class are evaluated in the context of the instance they
|
49
|
+
are triggered for with `#instance_exec`. This means `self` inside the callback
|
50
|
+
is the instance the callback is evaluated for. The arguments given to
|
51
|
+
`.trigger` are passed to the callback as additional arguments.
|
51
52
|
|
52
53
|
Registering a callback returns a `Callback` object. To cancel the callback use
|
53
54
|
`#cancel` on that object.
|
@@ -86,7 +87,9 @@ callback just for the first instance meeting certain criteria.
|
|
86
87
|
### Callbacks attached to an instance
|
87
88
|
|
88
89
|
All above mentioned methods on the class level also exist for each instance of
|
89
|
-
the class.
|
90
|
+
the class. With one exception: Callbacks are executed bound to the context its
|
91
|
+
block was defined, just like normal blocks are. `self` inside a callback is the
|
92
|
+
same as outside of it.
|
90
93
|
|
91
94
|
Callbacks for an individual instance are executed by calling `#trigger` on it.
|
92
95
|
This also executes callbacks attached to the class.
|
data/lib/callbacks_attachable.rb
CHANGED
@@ -1,5 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require_relative "../mrblib/callback_registry"
|
4
|
-
require_relative "../mrblib/callback"
|
5
|
-
require_relative "../mrblib/error"
|
1
|
+
dir = File.dirname File.dirname __FILE__
|
2
|
+
Dir[File.join(dir, 'mrblib', '*.rb')].reject{ |f| f.end_with? '_m.rb' }.sort.each{ |f| require f }
|
data/mrbgem.rake
CHANGED
@@ -0,0 +1,12 @@
|
|
1
|
+
Module.class_eval do
|
2
|
+
def singleton_class?
|
3
|
+
# The object id of a module is its pointer address shifted 5 to the left
|
4
|
+
# and adding its type id:
|
5
|
+
# https://github.com/mruby/mruby/blob/2e8ed9514feb74f4137e5835e74f256d52d6f191/src/etc.c#L108
|
6
|
+
#
|
7
|
+
# The type id of a singleton class is 0xc
|
8
|
+
# https://github.com/mruby/mruby/blob/a0fbc46ccd3e129532b05a9fe4f13f42a3c349b2/include/mruby/value.h#L107
|
9
|
+
|
10
|
+
__id__ & 0x1f == 0xc
|
11
|
+
end
|
12
|
+
end
|
data/mrblib/callback.rb
CHANGED
@@ -1,17 +1,22 @@
|
|
1
1
|
module CallbacksAttachable
|
2
2
|
class Callback
|
3
|
-
def initialize(registry, event, opts
|
3
|
+
def initialize(registry, event, opts, instance_scope, callback)
|
4
4
|
@registry = registry
|
5
5
|
@event = event
|
6
6
|
@call_condition = opts.fetch(:if, false)
|
7
7
|
@cancel_condition = opts.fetch(:until, false)
|
8
|
+
@instance_scope = instance_scope
|
8
9
|
@callback = callback
|
9
10
|
@canceled = false
|
10
11
|
end
|
11
12
|
|
12
13
|
def call(instance, args)
|
13
14
|
return if @call_condition and not @call_condition.call instance, *args
|
14
|
-
@
|
15
|
+
if @instance_scope
|
16
|
+
instance.instance_exec(*args, &@callback)
|
17
|
+
else
|
18
|
+
@callback.call(*args)
|
19
|
+
end
|
15
20
|
cancel if @cancel_condition and @cancel_condition.call instance, *args
|
16
21
|
end
|
17
22
|
|
data/mrblib/callback_registry.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
module CallbacksAttachable
|
2
2
|
class CallbackRegistry
|
3
|
-
def initialize(
|
4
|
-
@
|
3
|
+
def initialize(owner_class)
|
4
|
+
@singleton_owner = owner_class.singleton_class?
|
5
5
|
@callbacks = {}
|
6
6
|
end
|
7
7
|
|
8
8
|
def register(event, opts, callback)
|
9
9
|
@callbacks[event] ||= {}
|
10
|
-
callback = Callback.new(self, event, opts, callback)
|
10
|
+
callback = Callback.new(self, event, opts, !@singleton_owner, callback)
|
11
11
|
@callbacks[event][callback] = true
|
12
12
|
callback
|
13
13
|
end
|
data/mrblib/version.rb
CHANGED
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: 2.
|
4
|
+
version: 2.2.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-
|
11
|
+
date: 2017-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- callbacks_attachable.gemspec
|
70
70
|
- lib/callbacks_attachable.rb
|
71
71
|
- mrbgem.rake
|
72
|
+
- mrblib/_ext_module_m.rb
|
72
73
|
- mrblib/callback.rb
|
73
74
|
- mrblib/callback_registry.rb
|
74
75
|
- mrblib/callbacks_attachable.rb
|
@@ -94,9 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
95
|
version: '0'
|
95
96
|
requirements: []
|
96
97
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.6.8
|
98
99
|
signing_key:
|
99
100
|
specification_version: 4
|
100
101
|
summary: Attach callbacks to classes or individual instances.
|
101
102
|
test_files: []
|
102
|
-
has_rdoc:
|