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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d107585e18c31fa65029de71abfe920461083fb
4
- data.tar.gz: dbcea68a454e67c3aa6c500729d10d027afca3ea
3
+ metadata.gz: 12ec8224c4c44dbd6b2f911c85dfdee057523d61
4
+ data.tar.gz: b73d81ff789dc2ba7db11bf0fc7fbc022f79623b
5
5
  SHA512:
6
- metadata.gz: 3ddf6d868a2aa06a7b378458bc8711ecfeb80303c5c9022b5b1d972653ebb989f8684e41ce1ff183e59ced231e7f2c622b28f6e5eb2717481fa8ecf6f54fe61f
7
- data.tar.gz: ab01d260af78b5f0c46c51582fe45a5675a5edbce9b8c7dbb7f5fd64a8c9db900d74effcc0bbc5f456a17ee09da62b350d8c19f8067ea4b83a6fc73b0b71737b
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 |instance, method|
38
- puts instance.__send__(method)
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
- The first argument of the callback is the instance the callback is executed
49
- for. The second and further arguments given to `.trigger` are passed to the
50
- callback as additional arguments.
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.
@@ -1,5 +1,2 @@
1
- require_relative "../mrblib/callbacks_attachable"
2
- require_relative "../mrblib/version"
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
@@ -13,4 +13,5 @@ DESC
13
13
  spec.authors = ['Christopher Aue']
14
14
 
15
15
  spec.add_dependency 'mruby-objectspace', :core => 'mruby-objectspace'
16
+ spec.add_dependency 'mruby-object-ext', :core => 'mruby-object-ext'
16
17
  end
@@ -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 = {}, callback)
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
- @callback.call(instance, *args)
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
 
@@ -1,13 +1,13 @@
1
1
  module CallbacksAttachable
2
2
  class CallbackRegistry
3
- def initialize(owner)
4
- @owner = owner
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
@@ -1,3 +1,3 @@
1
1
  module CallbacksAttachable
2
- VERSION = "2.1.2"
2
+ VERSION = "2.2.0"
3
3
  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: 2.1.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-04-27 00:00:00.000000000 Z
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.5.1
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: