statisfy 0.0.2 → 0.0.4

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: 42017075ea6e11814c69273dad495fed1143fe56c84cca652f52c45e44f58be6
4
- data.tar.gz: 855f430c83b255b10228675c387947689cfd4dc3147073f65e56c15acb2bd485
3
+ metadata.gz: b0232129b80c1b67a7e31c77256fcfea55d199e8bddb41d5acc75738290b54ed
4
+ data.tar.gz: f9172db49c79f90d71e49907220db46772991bacda7356eac9dea539e900b115
5
5
  SHA512:
6
- metadata.gz: 8b716fee4967f50c59a19a4becfffa8097593bb22cd5ffbe60bea6dcae0d6a0fe4bc1be017e91c38c4f1f465f87a765e51d097e14ac8bb02ba344887a5bd9565
7
- data.tar.gz: 21b5db159bc50e44c42b67b8ccbbb4d7b8f7f650f52f273983afcf4bc9ac5c5cc5be2553ed9ce3a2190fbc97e16c4b2dc521ae5c3f943cc1186a0e3093ba6494
6
+ metadata.gz: 1a7329d1ccd0da87a4eee455a885dc46b8e6bbcc24f1805348b77558114b8d946430ea33a443b432aba59f4e4a60314b4b53336539e1240e4afaffe4b1d33b51
7
+ data.tar.gz: 5cc4383205544ea34da8bf25b1683237f7f89e241dc393376cc1d4ac0f478ecf2cc2bfc91e885db4d0b89d2337af7adc9eeea30766b253f41949cdbfc4287d41
@@ -26,7 +26,6 @@ module Statisfy
26
26
  # @param month: the month for which you want the value of the counter (optional)
27
27
  #
28
28
  def value(scope: nil, month: nil)
29
- p "HEIN???"
30
29
  month = month&.strftime("%Y-%m") if month.present?
31
30
  average(scope:, month:)
32
31
  end
@@ -39,14 +39,16 @@ module Statisfy
39
39
  # but the `count` DSL defines them automatically based on the options provided
40
40
  #
41
41
  def apply_default_counter_options(args)
42
- define_method(:identifier, args[:uniq_by] || -> { params["id"] })
42
+ define_method(:identifier, args[:uniq_by] || -> { nil })
43
43
  define_method(:scopes, args[:scopes] || Statisfy.configuration.default_scopes || -> { [] })
44
44
  define_method(:if_async, args[:if_async] || -> { true })
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
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 })
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
+ })
50
52
  end
51
53
 
52
54
  #
@@ -72,6 +74,10 @@ module Statisfy
72
74
  redis_client.scard(key_for(scope:, month:))
73
75
  end
74
76
 
77
+ def members(scope: nil, month: nil)
78
+ redis_client.smembers(key_for(scope:, month:))
79
+ end
80
+
75
81
  #
76
82
  # Returns the list of elements in the set (in case you use .append and not .increment)
77
83
  #
@@ -104,12 +110,13 @@ module Statisfy
104
110
  #
105
111
  # This is the name of the Redis key that will be used to store the counter
106
112
  #
107
- def key_for(scope:, month: nil)
113
+ def key_for(scope:, month: nil, key_value: nil)
108
114
  {
109
115
  counter: name.demodulize.underscore,
110
116
  month:,
111
117
  scope_type: scope&.class&.name,
112
- scope_id: scope&.id
118
+ scope_id: scope&.id,
119
+ key_value:
113
120
  }.to_json
114
121
  end
115
122
 
@@ -181,7 +188,7 @@ module Statisfy
181
188
  end
182
189
 
183
190
  def destroy_event_handled?
184
- return false unless params[:statisfy_trigger] == :destroy
191
+ return false unless params[:statisfy_trigger] == :destroy && value.blank?
185
192
 
186
193
  if decrement_on_destroy?
187
194
  decrement
@@ -194,6 +201,14 @@ module Statisfy
194
201
  false
195
202
  end
196
203
 
204
+ def key_value
205
+ value || identifier || params["id"]
206
+ end
207
+
208
+ def custom_key_value?
209
+ identifier.present? || value.present?
210
+ end
211
+
197
212
  #
198
213
  # This allows to iterate over all the counters that need to be updated
199
214
  # (in general the Department(s) and Organisation(s) for both the current month and the global counter)
@@ -201,20 +216,15 @@ module Statisfy
201
216
  def all_counters
202
217
  [month_to_set, nil].each do |month|
203
218
  scopes_with_global.each do |scope|
204
- yield self.class.key_for(scope:, month:), identifier
219
+ yield self.class.key_for(scope:, month:)
205
220
  end
206
221
  end
207
222
  end
208
223
 
209
224
  def increment
210
- all_counters do |key, id|
211
- self.class.redis_client.sadd?(key, id)
212
- end
213
- end
214
-
215
- def decrement
216
- all_counters do |key, id|
217
- self.class.redis_client.srem?(key, id)
225
+ all_counters do |key|
226
+ self.class.redis_client.sadd?(key, key_value)
227
+ self.class.redis_client.sadd?(uniq_by_ids(key), params["id"]) if custom_key_value?
218
228
  end
219
229
  end
220
230
 
@@ -226,6 +236,23 @@ module Statisfy
226
236
  self.class.redis_client.rpush(key, value)
227
237
  end
228
238
  end
239
+
240
+ # rubocop:disable Metrics/AbcSize
241
+ def decrement
242
+ all_counters do |key|
243
+ if custom_key_value?
244
+ self.class.redis_client.srem?(uniq_by_ids(key), params["id"])
245
+ self.class.redis_client.srem?(key, key_value) if self.class.redis_client.scard(uniq_by_ids(key)).zero?
246
+ else
247
+ self.class.redis_client.srem?(key, key_value)
248
+ end
249
+ end
250
+ end
251
+ # rubocop:enable Metrics/AbcSize
252
+
253
+ def uniq_by_ids(key)
254
+ "#{key};#{key_value}"
255
+ end
229
256
  end
230
257
  end
231
258
 
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.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve