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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12ec8224c4c44dbd6b2f911c85dfdee057523d61
4
- data.tar.gz: b73d81ff789dc2ba7db11bf0fc7fbc022f79623b
3
+ metadata.gz: 5704f2e0b5be12b67d21705fd058832d8b43c768
4
+ data.tar.gz: af96252e63b77c3d83b99f9c0b1df8d75144e916
5
5
  SHA512:
6
- metadata.gz: 5bf6762f10eb1429d222a696e125f7e724ff589c8f4f49a457725471d20931261d35874518cceb336223d54cbe89c7bc6505a2bfcddcb81325af4596f46ba4c4
7
- data.tar.gz: ced69ccc31423f7fd0d3255e503890c40043ae313a046babf154561fa931978940dc1c73a3a624b94445a4df62f3b8d2f4ea6b978cdc157aeb0a8b12636964ce
6
+ metadata.gz: 908846d84dcba7ffa75ee38efd635e6c600015cfd029d39cc83b08ece263a80ec2b66c87c916d5d534aeff5e75d5684e34126109d8374a18e8b5763a9a7067c4
7
+ data.tar.gz: 619ad1b6ea59e844fb2e54bd2835ca7b1637b662a1dec721cfa84efb06ecca1b973e2e1a984c7afee60f05be34c55326dd3989969be5586837c3c6a079f2fe75
data/Gemfile CHANGED
@@ -1,3 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :test do
6
+ gem 'rspec', '~>3.6'
7
+ end
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, event, opts, instance_scope, callback)
3
+ def initialize(registry, events, opts, instance_scope, callback)
4
4
  @registry = registry
5
- @event = event
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 @event, self
31
+ @events.each{ |event| @registry.deregister event, self }
32
32
  @on_cancel.call if @on_cancel
33
33
  @canceled = true
34
34
  end
@@ -5,10 +5,16 @@ module CallbacksAttachable
5
5
  @callbacks = {}
6
6
  end
7
7
 
8
- def register(event, opts, callback)
9
- @callbacks[event] ||= {}
10
- callback = Callback.new(self, event, opts, !@singleton_owner, callback)
11
- @callbacks[event][callback] = true
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, opts = {}, &callback)
12
- __callbacks__.register(event, opts, callback)
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, opts = {}, &callback)
16
- on(event, opts.merge(until: proc{ true }), &callback)
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(*args, &callback)
51
- singleton_class.on *args, &callback
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, opts = {}, &callback)
55
- on(event, opts.merge(until: proc{ true }), &callback)
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
@@ -1,3 +1,3 @@
1
1
  module CallbacksAttachable
2
- VERSION = "2.2.0"
2
+ VERSION = "2.3.0"
3
3
  end
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.2.0
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-06-03 00:00:00.000000000 Z
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.8
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.