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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bf901bb209f43e88099e395db0b4651e1c9b49aa119fdcbe0d941b8434e058a
4
- data.tar.gz: 5330f8c09b29c28c16117c9ef633cd94cd9af12df70f299a4ef71bb730cde91c
3
+ metadata.gz: e3a10099a0ae5de62cc548fc5eed38c6d05c06b3bc04f4e32846627024260325
4
+ data.tar.gz: bc282b5f30c8ae07565ae24f8352f2724860a76b763c8be4d1e18484c76d4a3e
5
5
  SHA512:
6
- metadata.gz: 4d2ad92ebf235e4114f424e770487d29d78940cba1d6b590840bb15610446125d7713623e692f9817d3b4cc5a34eafb2cbe3930509e1b20d07bdcd7a06dbf701
7
- data.tar.gz: 3e4e9141247f501ca1c7a7aeb0fff50a63c27020e5035b4a3c21d5e0ebcd482ab6ce4146c1d1c7834144e630ec39b574973ec1721b3d96e541d468d8c6df6ea7
6
+ metadata.gz: bfc687b9a1f56733ccb8c484e39f6b7d79c7844bedc5f46963eeb478568ee45c7c8278d3725aec189058d2076aff4deb304a905c23d770d22b9d76a95f3dbf3e
7
+ data.tar.gz: 4115d693fda17370ebe46a34088159ac39986b77280c7e8894bc39b1cf060562dd0c00adfac6743e0429918cdfc164a5f1ccee3dee0463a9246bc35f87409bed
@@ -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,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
- size(scope:, month:)
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
- if value.present?
174
- append(value:)
175
- else
176
- decrement? ? decrement : increment
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:), identifier
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, id|
194
- self.class.redis_client.sadd?(key, id)
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.rpush(key, value)
211
+ self.class.redis_client.lrem(key, 1, value || identifier)
210
212
  end
211
213
  end
212
214
  end
@@ -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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve