callbacks_attachable 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/callbacks_attachable/version.rb +1 -1
- data/lib/callbacks_attachable.rb +21 -17
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b05399a4f322a216b88e51598db9b38b91162e3
|
4
|
+
data.tar.gz: ed5a62269bb820e77ebe95fbd424fa6c1b89db90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c66ee4bab342a2564dcc7ba70ca4a208f851ee6828c2ce4309b45addc0cbcbb67944d050f1718e846a425acf2cef8d3ccb0931096a5c194e8f6c8ea27d3fe92
|
7
|
+
data.tar.gz: c3c806c8de55a7254d8056b4e02626a649a9256f1f0091d06d79e1729d4c2e023825e80723156b72afb3c913ddb36541c80a3befb3a31e77c7c5ae447805bcf7
|
data/lib/callbacks_attachable.rb
CHANGED
@@ -6,23 +6,27 @@ module CallbacksAttachable
|
|
6
6
|
end
|
7
7
|
|
8
8
|
module ClassMethods
|
9
|
-
def on(event, &callback)
|
9
|
+
def on(event, skip: 0, &callback)
|
10
|
+
count = 0
|
10
11
|
callbacks[event] ||= []
|
11
|
-
callbacks[event] <<
|
12
|
-
|
12
|
+
callbacks[event] << proc do |*args|
|
13
|
+
next unless (count += 1) > skip
|
14
|
+
instance_exec(*args, &callback)
|
15
|
+
end
|
16
|
+
callbacks[event].last
|
13
17
|
end
|
14
18
|
|
15
|
-
def once_on(event, &callback)
|
19
|
+
def once_on(event, *opts, &callback)
|
16
20
|
klass = self
|
17
|
-
registered_callback = on(event) do |*args|
|
21
|
+
registered_callback = on(event, *opts) do |*args|
|
18
22
|
klass.off(event, registered_callback)
|
19
23
|
instance_exec(*args, &callback)
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
23
|
-
def until_true_on(event, &callback)
|
27
|
+
def until_true_on(event, *opts, &callback)
|
24
28
|
klass = self
|
25
|
-
registered_callback = on(event) do |*args|
|
29
|
+
registered_callback = on(event, *opts) do |*args|
|
26
30
|
klass.off(event, registered_callback) if instance_exec(*args, &callback)
|
27
31
|
end
|
28
32
|
end
|
@@ -52,34 +56,34 @@ module CallbacksAttachable
|
|
52
56
|
end
|
53
57
|
end
|
54
58
|
|
55
|
-
def on(
|
56
|
-
if_self(:on,
|
59
|
+
def on(*args, &block)
|
60
|
+
if_self(:on, *args, &block)
|
57
61
|
end
|
58
62
|
|
59
|
-
def once_on(
|
60
|
-
if_self(:until_true_on,
|
63
|
+
def once_on(*args, &block)
|
64
|
+
if_self(:until_true_on, *args) do |*args|
|
61
65
|
block.call(*args)
|
62
66
|
true
|
63
67
|
end
|
64
68
|
end
|
65
69
|
|
66
|
-
def until_true_on(
|
67
|
-
if_self(:until_true_on,
|
70
|
+
def until_true_on(*args, &block)
|
71
|
+
if_self(:until_true_on, *args, &block)
|
68
72
|
end
|
69
73
|
|
70
74
|
def trigger(event, *args)
|
71
75
|
self.class.trigger(event, *args, context: self)
|
72
76
|
end
|
73
77
|
|
74
|
-
def off(
|
75
|
-
self.class.off(
|
78
|
+
def off(*args)
|
79
|
+
self.class.off(*args)
|
76
80
|
end
|
77
81
|
|
78
82
|
private
|
79
83
|
|
80
|
-
def if_self(
|
84
|
+
def if_self(*args, &block)
|
81
85
|
instance = self
|
82
|
-
self.class.__send__(
|
86
|
+
self.class.__send__(*args) do |*args|
|
83
87
|
next false if instance != self
|
84
88
|
block.call(*args)
|
85
89
|
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.
|
4
|
+
version: 0.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: 2015-
|
11
|
+
date: 2015-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
version: '0'
|
76
76
|
requirements: []
|
77
77
|
rubyforge_project:
|
78
|
-
rubygems_version: 2.4.
|
78
|
+
rubygems_version: 2.4.5.1
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: Attach callbacks to ruby objects. Then, trigger and detach them.
|