statisfy 0.0.1 → 0.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/lib/statisfy/counter.rb +18 -1
- data/lib/statisfy/subscriber.rb +26 -12
- 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: 42017075ea6e11814c69273dad495fed1143fe56c84cca652f52c45e44f58be6
|
4
|
+
data.tar.gz: 855f430c83b255b10228675c387947689cfd4dc3147073f65e56c15acb2bd485
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b716fee4967f50c59a19a4becfffa8097593bb22cd5ffbe60bea6dcae0d6a0fe4bc1be017e91c38c4f1f465f87a765e51d097e14ac8bb02ba344887a5bd9565
|
7
|
+
data.tar.gz: 21b5db159bc50e44c42b67b8ccbbb4d7b8f7f650f52f273983afcf4bc9ac5c5cc5be2553ed9ce3a2190fbc97e16c4b2dc521ae5c3f943cc1186a0e3093ba6494
|
data/lib/statisfy/counter.rb
CHANGED
@@ -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]
|
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)
|
data/lib/statisfy/subscriber.rb
CHANGED
@@ -5,8 +5,7 @@ module Statisfy
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
-
def catch_events(*event_names
|
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:
|
22
|
-
counter =
|
23
|
-
counter.
|
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
|
-
|
43
|
+
after_commit on: [event] do
|
44
|
+
next unless instance_exec(&statisfy_counter).should_run?
|
26
45
|
|
27
|
-
|
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
|