callbacks_attachable 0.3.4 → 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: e8a6bd534e0b47cbf70e05f3a770eddbb1b53bdc
4
- data.tar.gz: e76520e94fe00df151e178f4cf7e70e62b4c5504
3
+ metadata.gz: 7851ad52aa0e5f3e20f1b74669c7b055a83b3c07
4
+ data.tar.gz: 953968fa4f6af44dcbcff1c1c2451063cd3dcfa5
5
5
  SHA512:
6
- metadata.gz: 602b260b5e575d4efcffea7a1a2ac098cacc37ccf7ec02209315aab7f925191d8972b2ffab95cbd49310a2313f12dedcc447ec5340b4348693997a201ce449e1
7
- data.tar.gz: 848f86e423e5a8a235d30e6a21679e57e74df5f35ddee2904e041868c11d7905ebd12b20e5e95df4018cba39aae3e6c3c90027abe8d6cc22cafd3595f47955fa
6
+ metadata.gz: 718d264ba8b815bbe20b35f7c02af16684b71267a4cbf413f4979d62f1438039f8478a8775d92be830b22daa0692dc274d877372e6db50adbfdba34e79587bc3
7
+ data.tar.gz: 0b45a12da19188cb38cd0ec60b23b39b8274cee805437e9f7d9fa51bbd20ccefd0681f57f33361d1ac35d614376a7a4f35d579cecceb47017a6b5b2d76d6abc6
@@ -0,0 +1,24 @@
1
+ module CallbacksAttachable
2
+ class AllInstancesCallback
3
+ def initialize(klass, skip: 0, &callback)
4
+ @class = klass
5
+ @skip = skip
6
+ @callback = callback
7
+ @call_counts = {}
8
+ end
9
+
10
+ def call(*args, **opts)
11
+ if instance = opts[:instance]
12
+ call_for_instance(instance, *args)
13
+ else
14
+ ObjectSpace.each_object(@class).all?{ |inst| call_for_instance(inst, *args) }
15
+ end
16
+ end
17
+
18
+ def call_for_instance(instance, *args)
19
+ @call_counts[instance.__id__] = @call_counts[instance.__id__].to_i + 1
20
+ return true if @call_counts[instance.__id__] <= @skip
21
+ false != instance.instance_exec(*args, &@callback)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,49 @@
1
+ module CallbacksAttachable
2
+ class CallbackHandler
3
+ def initialize(object, callback_class)
4
+ @object = object
5
+ @callback_class = callback_class
6
+ end
7
+
8
+ def on(event, skip: 0, &callback)
9
+ __callbacks__[event] ||= []
10
+ __callbacks__[event] << @callback_class.new(@object, skip: skip, &callback)
11
+ __callbacks__[event].last
12
+ end
13
+
14
+ def once_on(event, *opts, &callback)
15
+ callback_registry = self
16
+ registered_callback = on(event, *opts) do |*args|
17
+ callback_registry.off(event, registered_callback)
18
+ yield(*args)
19
+ end
20
+ end
21
+
22
+ def until_true_on(event, *opts, &callback)
23
+ callback_registry = self
24
+ registered_callback = on(event, *opts) do |*args|
25
+ yield(*args).tap do |result|
26
+ callback_registry.off(event, registered_callback) if result
27
+ end
28
+ end
29
+ end
30
+
31
+ def trigger(event, *args, **opts)
32
+ return true if __callbacks__[event].nil?
33
+
34
+ # dup the callback list so that removing callbacks while iterating does
35
+ # still call all callbacks during map.
36
+ __callbacks__[event].dup.all?{ |callback| callback.call(*args, **opts) }
37
+ end
38
+
39
+ def off(event, callback)
40
+ __callbacks__[event].delete(callback)
41
+ end
42
+
43
+ private
44
+
45
+ def __callbacks__
46
+ @__callbacks__ ||= {}
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ module CallbacksAttachable
2
+ class InstanceCallback
3
+ def initialize(instance, skip: 0, &callback)
4
+ @instance = instance
5
+ @skip = skip
6
+ @callback = callback
7
+ @call_count = 0
8
+ end
9
+
10
+ def call(*args, **opts)
11
+ @call_count += 1
12
+ return true if @call_count <= @skip
13
+ @callback.call(*args) != false
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module CallbacksAttachable
2
- VERSION = "0.3.4"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,95 +1,65 @@
1
1
  require "callbacks_attachable/version"
2
+ require "callbacks_attachable/callback_handler"
3
+ require "callbacks_attachable/instance_callback"
4
+ require "callbacks_attachable/all_instances_callback"
2
5
 
3
6
  module CallbacksAttachable
4
- def self.included(base)
5
- base.extend ClassMethods
7
+ def self.included(klass)
8
+ klass.extend ClassMethods
6
9
  end
7
10
 
8
11
  module ClassMethods
9
- def on(event, skip: 0, &callback)
10
- count = 0
11
- callbacks[event] ||= []
12
- callbacks[event] << proc do |*args|
13
- next unless (count += 1) > skip
14
- instance_exec(*args, &callback)
15
- end
16
- callbacks[event].last
12
+ def on(*args, &block)
13
+ __callback_handler__.on(*args, &block)
17
14
  end
18
15
 
19
- def once_on(event, *opts, &callback)
20
- klass = self
21
- registered_callback = on(event, *opts) do |*args|
22
- klass.off(event, registered_callback)
23
- instance_exec(*args, &callback)
24
- end
16
+ def once_on(*args, &block)
17
+ __callback_handler__.once_on(*args, &block)
25
18
  end
26
19
 
27
- def until_true_on(event, *opts, &callback)
28
- klass = self
29
- registered_callback = on(event, *opts) do |*args|
30
- instance_exec(*args, &callback).tap do |result|
31
- klass.off(event, registered_callback) if result
32
- end
33
- end
20
+ def until_true_on(*args, &block)
21
+ __callback_handler__.until_true_on(*args, &block)
34
22
  end
35
23
 
36
- def trigger(event, *args, context: self)
37
- return true unless callbacks[event]
38
-
39
- # dup the callback list so that removing callbacks while iterating does
40
- # still call all callbacks during map.
41
- callbacks[event].dup.all? do |callback|
42
- context.instance_exec(*args, &callback) != false
43
- end
24
+ def off(*args)
25
+ __callback_handler__.off(*args)
44
26
  end
45
27
 
46
- def off(event, callback)
47
- if callbacks[event]
48
- callbacks[event].delete(callback)
49
- callbacks.delete(event) if callbacks[event].empty?
50
- end
51
- true
28
+ def trigger(*args)
29
+ __callback_handler__.trigger(*args)
52
30
  end
53
31
 
54
32
  private
55
33
 
56
- def callbacks
57
- @callbacks ||= {}
34
+ def __callback_handler__
35
+ @__callback_handler__ ||= CallbackHandler.new(self, AllInstancesCallback)
58
36
  end
59
37
  end
60
38
 
61
39
  def on(*args, &block)
62
- if_self(:on, *args, &block)
40
+ __callback_handler__.on(*args, &block)
63
41
  end
64
42
 
65
43
  def once_on(*args, &block)
66
- if_self(:until_true_on, *args) do |*args|
67
- block.call(*args)
68
- true
69
- end
44
+ __callback_handler__.once_on(*args, &block)
70
45
  end
71
46
 
72
47
  def until_true_on(*args, &block)
73
- if_self(:until_true_on, *args, &block)
48
+ __callback_handler__.until_true_on(*args, &block)
74
49
  end
75
50
 
76
- def trigger(event, *args)
77
- self.class.trigger(event, *args, context: self)
51
+ def off(*args)
52
+ __callback_handler__.off(*args)
78
53
  end
79
54
 
80
- def off(*args)
81
- self.class.off(*args)
55
+ def trigger(*args)
56
+ self.class.trigger(*args, instance: self)
57
+ __callback_handler__.trigger(*args)
82
58
  end
83
59
 
84
60
  private
85
61
 
86
- def if_self(*args, skip: 0, &block)
87
- instance = self
88
- count = 0
89
- self.class.__send__(*args, skip: 0) do |*args|
90
- next if instance != self
91
- next unless (count += 1) > skip
92
- block.call(*args)
93
- end
62
+ def __callback_handler__
63
+ @__callback_handler__ ||= CallbackHandler.new(self, InstanceCallback)
94
64
  end
95
65
  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: 0.3.4
4
+ version: 1.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: 2016-01-12 00:00:00.000000000 Z
11
+ date: 2016-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,6 +95,9 @@ files:
95
95
  - Rakefile
96
96
  - callbacks_attachable.gemspec
97
97
  - lib/callbacks_attachable.rb
98
+ - lib/callbacks_attachable/all_instances_callback.rb
99
+ - lib/callbacks_attachable/callback_handler.rb
100
+ - lib/callbacks_attachable/instance_callback.rb
98
101
  - lib/callbacks_attachable/version.rb
99
102
  homepage: https://github.com/christopheraue/ruby-callbacks_attachable
100
103
  licenses: