statisfy 0.0.1 → 0.0.3
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 +25 -23
- 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: e3a10099a0ae5de62cc548fc5eed38c6d05c06b3bc04f4e32846627024260325
|
4
|
+
data.tar.gz: bc282b5f30c8ae07565ae24f8352f2724860a76b763c8be4d1e18484c76d4a3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfc687b9a1f56733ccb8c484e39f6b7d79c7844bedc5f46963eeb478568ee45c7c8278d3725aec189058d2076aff4deb304a905c23d770d22b9d76a95f3dbf3e
|
7
|
+
data.tar.gz: 4115d693fda17370ebe46a34088159ac39986b77280c7e8894bc39b1cf060562dd0c00adfac6743e0429918cdfc164a5f1ccee3dee0463a9246bc35f87409bed
|
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,10 @@ 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] : lambda {
|
50
|
+
args[:decrement_on_destroy] || true
|
51
|
+
})
|
48
52
|
end
|
49
53
|
|
50
54
|
#
|
@@ -62,14 +66,10 @@ module Statisfy
|
|
62
66
|
if const_get(:COUNTER_TYPE) == :aggregate
|
63
67
|
average(scope:, month:)
|
64
68
|
else
|
65
|
-
|
69
|
+
elements_in(scope:, month:).uniq.size
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
69
|
-
def size(scope: nil, month: nil)
|
70
|
-
redis_client.scard(key_for(scope:, month:))
|
71
|
-
end
|
72
|
-
|
73
73
|
#
|
74
74
|
# Returns the list of elements in the set (in case you use .append and not .increment)
|
75
75
|
#
|
@@ -168,13 +168,24 @@ module Statisfy
|
|
168
168
|
end
|
169
169
|
|
170
170
|
def process_event
|
171
|
+
return if destroy_event_handled?
|
171
172
|
return unless if_async
|
172
173
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
174
|
+
decrement? ? decrement : increment
|
175
|
+
end
|
176
|
+
|
177
|
+
def destroy_event_handled?
|
178
|
+
return false unless params[:statisfy_trigger] == :destroy
|
179
|
+
|
180
|
+
if decrement_on_destroy?
|
181
|
+
decrement
|
182
|
+
return true
|
183
|
+
elsif respond_to?(:on_destroy)
|
184
|
+
on_destroy
|
185
|
+
return true
|
177
186
|
end
|
187
|
+
|
188
|
+
false
|
178
189
|
end
|
179
190
|
|
180
191
|
#
|
@@ -184,29 +195,20 @@ module Statisfy
|
|
184
195
|
def all_counters
|
185
196
|
[month_to_set, nil].each do |month|
|
186
197
|
scopes_with_global.each do |scope|
|
187
|
-
yield self.class.key_for(scope:, month:)
|
198
|
+
yield self.class.key_for(scope:, month:)
|
188
199
|
end
|
189
200
|
end
|
190
201
|
end
|
191
202
|
|
192
203
|
def increment
|
193
|
-
all_counters do |key
|
194
|
-
self.class.redis_client.
|
204
|
+
all_counters do |key|
|
205
|
+
self.class.redis_client.rpush(key, value || identifier)
|
195
206
|
end
|
196
207
|
end
|
197
208
|
|
198
209
|
def decrement
|
199
|
-
all_counters do |key, id|
|
200
|
-
self.class.redis_client.srem?(key, id)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
#
|
205
|
-
# To be used to store a list of values instead of a basic counter
|
206
|
-
#
|
207
|
-
def append(value:)
|
208
210
|
all_counters do |key|
|
209
|
-
self.class.redis_client.
|
211
|
+
self.class.redis_client.lrem(key, 1, value || identifier)
|
210
212
|
end
|
211
213
|
end
|
212
214
|
end
|
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
|