statisfy 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: 5bf901bb209f43e88099e395db0b4651e1c9b49aa119fdcbe0d941b8434e058a
4
- data.tar.gz: 5330f8c09b29c28c16117c9ef633cd94cd9af12df70f299a4ef71bb730cde91c
3
+ metadata.gz: 42017075ea6e11814c69273dad495fed1143fe56c84cca652f52c45e44f58be6
4
+ data.tar.gz: 855f430c83b255b10228675c387947689cfd4dc3147073f65e56c15acb2bd485
5
5
  SHA512:
6
- metadata.gz: 4d2ad92ebf235e4114f424e770487d29d78940cba1d6b590840bb15610446125d7713623e692f9817d3b4cc5a34eafb2cbe3930509e1b20d07bdcd7a06dbf701
7
- data.tar.gz: 3e4e9141247f501ca1c7a7aeb0fff50a63c27020e5035b4a3c21d5e0ebcd482ab6ce4146c1d1c7834144e630ec39b574973ec1721b3d96e541d468d8c6df6ea7
6
+ metadata.gz: 8b716fee4967f50c59a19a4becfffa8097593bb22cd5ffbe60bea6dcae0d6a0fe4bc1be017e91c38c4f1f465f87a765e51d097e14ac8bb02ba344887a5bd9565
7
+ data.tar.gz: 21b5db159bc50e44c42b67b8ccbbb4d7b8f7f650f52f273983afcf4bc9ac5c5cc5be2553ed9ce3a2190fbc97e16c4b2dc521ae5c3f943cc1186a0e3093ba6494
@@ -27,7 +27,7 @@ module Statisfy
27
27
  def count(args = {})
28
28
  raise ArgumentError, "You must provide at least one event" if args[:every].blank?
29
29
 
30
- catch_events(*args[:every], if: args[:if] || -> { true })
30
+ catch_events(*args[:every])
31
31
  apply_default_counter_options(args)
32
32
  const_set(:COUNTER_TYPE, args[:type] || :increment)
33
33
  class_eval(&Statisfy.configuration.append_to_counters) if Statisfy.configuration.append_to_counters.present?
@@ -45,6 +45,8 @@ module Statisfy
45
45
  define_method(:decrement?, args[:decrement_if] || -> { false })
46
46
  define_method(:value, args[:value] || -> {})
47
47
  define_method(:should_run?, args[:if] || -> { true })
48
+ define_method(:on_destroy, args[:on_destroy]) if args[:on_destroy].present?
49
+ define_method(:decrement_on_destroy?, args[:decrement_on_destroy].is_a?(Proc) ? args[:decrement_on_destroy] : -> { args[:decrement_on_destroy] || false })
48
50
  end
49
51
 
50
52
  #
@@ -168,6 +170,7 @@ module Statisfy
168
170
  end
169
171
 
170
172
  def process_event
173
+ return if destroy_event_handled?
171
174
  return unless if_async
172
175
 
173
176
  if value.present?
@@ -177,6 +180,20 @@ module Statisfy
177
180
  end
178
181
  end
179
182
 
183
+ def destroy_event_handled?
184
+ return false unless params[:statisfy_trigger] == :destroy
185
+
186
+ if decrement_on_destroy?
187
+ decrement
188
+ return true
189
+ elsif respond_to?(:on_destroy)
190
+ on_destroy
191
+ return true
192
+ end
193
+
194
+ false
195
+ end
196
+
180
197
  #
181
198
  # This allows to iterate over all the counters that need to be updated
182
199
  # (in general the Department(s) and Organisation(s) for both the current month and the global counter)
@@ -5,8 +5,7 @@ module Statisfy
5
5
  end
6
6
 
7
7
  module ClassMethods
8
- def catch_events(*event_names, **options)
9
- define_method(:should_run?, &options[:if] || -> { true })
8
+ def catch_events(*event_names)
10
9
  [*event_names].flatten.map do |event_name|
11
10
  model_and_event_from_event_name(event_name).tap do |model, event|
12
11
  append_callback_to_model(model, event)
@@ -17,18 +16,34 @@ module Statisfy
17
16
 
18
17
  def append_callback_to_model(model, event)
19
18
  listener = self
19
+
20
+ statisfy_counter = lambda {
21
+ counter = listener.new
22
+ counter.subject = self
23
+ counter.params = attributes
24
+ counter
25
+ }
26
+
27
+ trigger_event = lambda { |statisfy_trigger|
28
+ if listener.respond_to?(Statisfy.configuration.default_async_method) && statisfy_trigger != :destroy
29
+ listener.send(Statisfy.configuration.default_async_method, attributes.merge(statisfy_trigger:))
30
+ else
31
+ instance_exec(&statisfy_counter).perform(attributes.merge(statisfy_trigger:))
32
+ end
33
+ }
34
+
20
35
  model.class_eval do
21
- after_commit on: event do
22
- counter = listener.new
23
- counter.subject = self
36
+ after_commit on: [:destroy] do
37
+ counter = instance_exec(&statisfy_counter)
38
+ next unless counter.decrement_on_destroy? || counter.respond_to?(:on_destroy)
39
+
40
+ instance_exec(:destroy, &trigger_event)
41
+ end
24
42
 
25
- next unless counter.should_run?
43
+ after_commit on: [event] do
44
+ next unless instance_exec(&statisfy_counter).should_run?
26
45
 
27
- if listener.respond_to?(Statisfy.configuration.default_async_method)
28
- listener.send(Statisfy.configuration.default_async_method, attributes)
29
- else
30
- counter.perform(attributes)
31
- end
46
+ instance_exec(event, &trigger_event)
32
47
  end
33
48
  end
34
49
  end
@@ -64,7 +79,6 @@ module Statisfy
64
79
  # This is the method that will be called when an event is triggered
65
80
  # It will be executed in the background by Sidekiq
66
81
  #
67
- # @resource_or_hash [Hash] The attributes of the model that triggered the event + the previous_changes
68
82
  #
69
83
  def perform(resource_or_hash)
70
84
  @params = resource_or_hash
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statisfy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve