statisfy 0.0.3 → 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: e3a10099a0ae5de62cc548fc5eed38c6d05c06b3bc04f4e32846627024260325
4
- data.tar.gz: bc282b5f30c8ae07565ae24f8352f2724860a76b763c8be4d1e18484c76d4a3e
3
+ metadata.gz: b0232129b80c1b67a7e31c77256fcfea55d199e8bddb41d5acc75738290b54ed
4
+ data.tar.gz: f9172db49c79f90d71e49907220db46772991bacda7356eac9dea539e900b115
5
5
  SHA512:
6
- metadata.gz: bfc687b9a1f56733ccb8c484e39f6b7d79c7844bedc5f46963eeb478568ee45c7c8278d3725aec189058d2076aff4deb304a905c23d770d22b9d76a95f3dbf3e
7
- data.tar.gz: 4115d693fda17370ebe46a34088159ac39986b77280c7e8894bc39b1cf060562dd0c00adfac6743e0429918cdfc164a5f1ccee3dee0463a9246bc35f87409bed
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,7 +39,7 @@ 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 })
@@ -66,10 +66,18 @@ module Statisfy
66
66
  if const_get(:COUNTER_TYPE) == :aggregate
67
67
  average(scope:, month:)
68
68
  else
69
- elements_in(scope:, month:).uniq.size
69
+ size(scope:, month:)
70
70
  end
71
71
  end
72
72
 
73
+ def size(scope: nil, month: nil)
74
+ redis_client.scard(key_for(scope:, month:))
75
+ end
76
+
77
+ def members(scope: nil, month: nil)
78
+ redis_client.smembers(key_for(scope:, month:))
79
+ end
80
+
73
81
  #
74
82
  # Returns the list of elements in the set (in case you use .append and not .increment)
75
83
  #
@@ -102,12 +110,13 @@ module Statisfy
102
110
  #
103
111
  # This is the name of the Redis key that will be used to store the counter
104
112
  #
105
- def key_for(scope:, month: nil)
113
+ def key_for(scope:, month: nil, key_value: nil)
106
114
  {
107
115
  counter: name.demodulize.underscore,
108
116
  month:,
109
117
  scope_type: scope&.class&.name,
110
- scope_id: scope&.id
118
+ scope_id: scope&.id,
119
+ key_value:
111
120
  }.to_json
112
121
  end
113
122
 
@@ -171,11 +180,15 @@ module Statisfy
171
180
  return if destroy_event_handled?
172
181
  return unless if_async
173
182
 
174
- decrement? ? decrement : increment
183
+ if value.present?
184
+ append(value:)
185
+ else
186
+ decrement? ? decrement : increment
187
+ end
175
188
  end
176
189
 
177
190
  def destroy_event_handled?
178
- return false unless params[:statisfy_trigger] == :destroy
191
+ return false unless params[:statisfy_trigger] == :destroy && value.blank?
179
192
 
180
193
  if decrement_on_destroy?
181
194
  decrement
@@ -188,6 +201,14 @@ module Statisfy
188
201
  false
189
202
  end
190
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
+
191
212
  #
192
213
  # This allows to iterate over all the counters that need to be updated
193
214
  # (in general the Department(s) and Organisation(s) for both the current month and the global counter)
@@ -202,15 +223,36 @@ module Statisfy
202
223
 
203
224
  def increment
204
225
  all_counters do |key|
205
- self.class.redis_client.rpush(key, value || identifier)
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?
206
228
  end
207
229
  end
208
230
 
231
+ #
232
+ # To be used to store a list of values instead of a basic counter
233
+ #
234
+ def append(value:)
235
+ all_counters do |key|
236
+ self.class.redis_client.rpush(key, value)
237
+ end
238
+ end
239
+
240
+ # rubocop:disable Metrics/AbcSize
209
241
  def decrement
210
242
  all_counters do |key|
211
- self.class.redis_client.lrem(key, 1, value || identifier)
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
212
249
  end
213
250
  end
251
+ # rubocop:enable Metrics/AbcSize
252
+
253
+ def uniq_by_ids(key)
254
+ "#{key};#{key_value}"
255
+ end
214
256
  end
215
257
  end
216
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve