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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7e1ea725774c4dd07097acebe90266cbcfe6090173fc72562bf5d1e255972a6
4
- data.tar.gz: 7ad89a95e9805f784674238bea4fc55502280f2a992f8ce46e5a69db9d71a7b1
3
+ metadata.gz: 5682ae87ad43e4d13e0cc1ab6bd323c4fc54ac673dfdc0b9781cd5ac85581f1c
4
+ data.tar.gz: c048bd4af9d3f821151b9270717328f61ba57da0e8d76ec8abe721a53f5d7a18
5
5
  SHA512:
6
- metadata.gz: b5872e8d7f10d7481ba58ca25fb4e99bf63dae4129f222935ab0f0e5fb28f4c10cb9a6aef249bb407b36ec399046105923bd0d952e243a2f76a2ac43554cc1a1
7
- data.tar.gz: 9423b758a4e91f871577d14f5220ddfe2b83442d5897a02c531d48a6c624fa43e3664e2c929c50373a9595e0afb4b5daaebcb609122a1cd2c22a7b1457d4c75e
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. Extend `Callbacky` in your class
28
+ ### 1. Include `Callbacky` in your class
29
29
  ```ruby
30
30
  class MyService
31
- class << self
32
- extend Callbacky
33
-
34
- callbacky_after :init
35
- end
31
+ include Callbacky
36
32
  end
37
33
  ```
38
- ### 2. Define a callback
34
+ ### 2. Define callbacks
39
35
 
40
36
  ***Using method name:***
41
37
  ```ruby
42
38
  class MyService
43
- callbacky_after_init :handle_after_init
39
+ include Callbacky
40
+
41
+ callbacky :before, :init, :prepare_context
42
+ callbacky :after, :init, ->(obj) { obj.log_init }
44
43
 
45
- def handle_after_init
46
- puts "After init"
44
+ callbacky :after, :init do |instance|
45
+ instance.send_metrics
47
46
  end
48
- end
49
- ```
50
47
 
51
- ***Using a lambda or proc:***
52
- ```ruby
53
- class MyService
54
- callbacky_after_init ->(obj) { obj.log_event }
55
- end
56
- ```
48
+ def prepare_context
49
+ puts "Preparing context"
50
+ end
57
51
 
58
- ***Using a block:***
59
- ```ruby
60
- class MyService
61
- callbacky_after_init do |instance|
62
- instance.send_metrics
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 initializer
72
- ### callbacky_init will run before and after hooks
73
- callbacky_init do |instance|
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Callbacky
4
- VERSION = "0.1.3.1"
4
+ VERSION = "0.2"
5
5
  end
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
- VALID_LIFECYCLES = %i[before after].freeze
7
- class << self
8
- def define_callback(lifecycle_event)
9
- unless VALID_LIFECYCLES.include?(lifecycle_event)
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
- define_method("callbacky_#{lifecycle_event}") do |event|
15
- define_method("callbacky_#{lifecycle_event}_#{event}") do |callback_method_or_block = nil, &block|
16
- kallbacks = instance_variable_get("@#{lifecycle_event}_kallbacks") || []
17
- kallbacks << callback_method_or_block if callback_method_or_block
18
- kallbacks << block if block
19
- instance_variable_set("@#{lifecycle_event}_kallbacks", kallbacks)
20
- end
21
-
22
- define_method("callbacky_#{event}") do |obj, &block|
23
- before_kallbacks = instance_variable_get(:@before_kallbacks)
24
- before_kallbacks.each { |it| it.is_a?(Proc) ? it.call(obj) : obj.send(it) } if before_kallbacks
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
- VALID_LIFECYCLES.each do |it|
34
- define_callback(it)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: callbacky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3.1
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - pucinsk