callbacky 0.1.3.1 → 0.2
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/README.md +27 -27
- data/lib/callbacky/version.rb +1 -1
- data/lib/callbacky.rb +34 -25
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5682ae87ad43e4d13e0cc1ab6bd323c4fc54ac673dfdc0b9781cd5ac85581f1c
|
|
4
|
+
data.tar.gz: c048bd4af9d3f821151b9270717328f61ba57da0e8d76ec8abe721a53f5d7a18
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 13124ff72116bad7ad4b536c4ae5ce9beb57d372957279b851840a0539fcd330171811d11c765d119e1d2bca25e82ea5bdc266c39583aa0c9181823c4370f989
|
|
7
|
+
data.tar.gz: ca8551e470afd392e711ae4c4efb7b9c524e482c80ce3aaeaec73e7e0eb292008c0dce9ae8dc1e43bbdc40f601d68436f208ea2ec0c7594218f72dd1c16105c7
|
data/README.md
CHANGED
|
@@ -25,56 +25,56 @@ gem install callbacky
|
|
|
25
25
|
|
|
26
26
|
## Usage
|
|
27
27
|
|
|
28
|
-
### 1.
|
|
28
|
+
### 1. Include `Callbacky` in your class
|
|
29
29
|
```ruby
|
|
30
30
|
class MyService
|
|
31
|
-
|
|
32
|
-
extend Callbacky
|
|
33
|
-
|
|
34
|
-
callbacky_after :init
|
|
35
|
-
end
|
|
31
|
+
include Callbacky
|
|
36
32
|
end
|
|
37
33
|
```
|
|
38
|
-
### 2. Define
|
|
34
|
+
### 2. Define callbacks
|
|
39
35
|
|
|
40
36
|
***Using method name:***
|
|
41
37
|
```ruby
|
|
42
38
|
class MyService
|
|
43
|
-
|
|
39
|
+
include Callbacky
|
|
40
|
+
|
|
41
|
+
callbacky :before, :init, :prepare_context
|
|
42
|
+
callbacky :after, :init, ->(obj) { obj.log_init }
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
callbacky :after, :init do |instance|
|
|
45
|
+
instance.send_metrics
|
|
47
46
|
end
|
|
48
|
-
end
|
|
49
|
-
```
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
callbacky_after_init ->(obj) { obj.log_event }
|
|
55
|
-
end
|
|
56
|
-
```
|
|
48
|
+
def prepare_context
|
|
49
|
+
puts "Preparing context"
|
|
50
|
+
end
|
|
57
51
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
def log_init
|
|
53
|
+
puts "Logged init"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def send_metrics
|
|
57
|
+
puts "Sent metrics"
|
|
63
58
|
end
|
|
64
59
|
end
|
|
60
|
+
|
|
65
61
|
```
|
|
66
62
|
|
|
67
63
|
### 3. Trigger the callback
|
|
68
64
|
|
|
69
65
|
```ruby
|
|
70
66
|
class MyService
|
|
71
|
-
def
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
instance.do_some_other_things_inside
|
|
67
|
+
def initialize
|
|
68
|
+
callbacky_init
|
|
69
|
+
do_work
|
|
75
70
|
end
|
|
76
71
|
end
|
|
72
|
+
|
|
73
|
+
def do_work
|
|
74
|
+
puts "Doing the actual work"
|
|
75
|
+
end
|
|
77
76
|
end
|
|
77
|
+
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
## Development
|
data/lib/callbacky/version.rb
CHANGED
data/lib/callbacky.rb
CHANGED
|
@@ -1,36 +1,45 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "callbacky/version"
|
|
4
|
-
|
|
5
3
|
module Callbacky
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
raise ArgumentError,
|
|
11
|
-
"Valid life cycles are: #{VALID_LIFECYCLES}. #{lifecycle_event} was passed"
|
|
4
|
+
module ClassMethods
|
|
5
|
+
def callbacky_callbacks
|
|
6
|
+
@callbacky_callbacks ||= Hash.new do |events_hash, event|
|
|
7
|
+
events_hash[event] = Hash.new { |methods_hash, cycle| methods_hash[cycle] = [] }
|
|
12
8
|
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def callbacky(cycle, event, *methods, &block)
|
|
12
|
+
store_callbacks(cycle, event, *methods, &block)
|
|
13
|
+
define_callback_executor(event) unless method_defined?("callbacky_#{event}")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
yield if block
|
|
26
|
-
after_kallbacks = instance_variable_get(:@after_kallbacks)
|
|
27
|
-
after_kallbacks.each { |it| it.is_a?(Proc) ? it.call(obj) : obj.send(it) } if after_kallbacks
|
|
28
|
-
end
|
|
18
|
+
def store_callbacks(cycle, event, *methods, &block)
|
|
19
|
+
callbacks = callbacky_callbacks[event][cycle]
|
|
20
|
+
methods.each { |method| callbacks << method unless callbacks.include?(method) }
|
|
21
|
+
callbacks << block if block_given?
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def define_callback_executor(event)
|
|
25
|
+
define_method("callbacky_#{event}") do |&block|
|
|
26
|
+
run_callback_cycle(:before, event)
|
|
27
|
+
block&.call(self)
|
|
28
|
+
run_callback_cycle(:after, event)
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
def self.included(base)
|
|
34
|
+
base.extend(ClassMethods)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def run_callback_cycle(cycle, event)
|
|
40
|
+
callbacks = self.class.callbacky_callbacks.dig(event, cycle) || []
|
|
41
|
+
callbacks.each do |callback|
|
|
42
|
+
callback.is_a?(Proc) ? callback.call(self) : send(callback)
|
|
43
|
+
end
|
|
35
44
|
end
|
|
36
45
|
end
|