statisfy 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e3a10099a0ae5de62cc548fc5eed38c6d05c06b3bc04f4e32846627024260325
4
- data.tar.gz: bc282b5f30c8ae07565ae24f8352f2724860a76b763c8be4d1e18484c76d4a3e
3
+ metadata.gz: b5d8c7561ef32841eadac25ae7b662fce6ef93d5979b2c0a1c5945d1c608dc09
4
+ data.tar.gz: 480b87c6c4134b0cc0f16ba509920530067c3b7d3b1de8fbfa85ec2c6a381344
5
5
  SHA512:
6
- metadata.gz: bfc687b9a1f56733ccb8c484e39f6b7d79c7844bedc5f46963eeb478568ee45c7c8278d3725aec189058d2076aff4deb304a905c23d770d22b9d76a95f3dbf3e
7
- data.tar.gz: 4115d693fda17370ebe46a34088159ac39986b77280c7e8894bc39b1cf060562dd0c00adfac6743e0429918cdfc164a5f1ccee3dee0463a9246bc35f87409bed
6
+ metadata.gz: 141624d3043afab8642a513f1a93cf1a378d2412ae75f68154c4fa720ff86597b9fca54ccd13e3aa76c3414aacb462a7cdb647628f7db970aee7fae500a7c97c
7
+ data.tar.gz: e71caf7bdfee33bc954ede9ca8bd1a09b1fc20d1336bc0f7786d24d2cb27ceabbb7a9e362f4b06ff0b8c42ca65de9e0b3887501c7002cf7567283d050d0aecf2
@@ -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
 
@@ -0,0 +1,34 @@
1
+ require "ostruct"
2
+
3
+ module Statisfy
4
+ module Model
5
+ def self.included(klass)
6
+ klass.extend(ClassMethods)
7
+ klass.class_eval do
8
+ @statisfy = {}
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def count(params)
14
+ a_bloc = params.fetch(:scopes, nil) || params.fetch(:uniq_by, nil) || params.fetch(:if, nil)
15
+ source = a_bloc&.source
16
+
17
+ class_name = params[:as].to_s.camelize
18
+
19
+ eval <<-RUBY, binding, __FILE__, __LINE__ + 1
20
+ class ::Statisfy::#{class_name}
21
+ include Statisfy::Counter
22
+
23
+ #{source || "count(#{params})"}
24
+ end
25
+ RUBY
26
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
27
+ def self.#{params[:as]}
28
+ ::Statisfy::#{class_name}
29
+ end
30
+ RUBY
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/statisfy.rb CHANGED
@@ -4,6 +4,7 @@ require_relative "statisfy/configuration"
4
4
  require_relative "statisfy/counter"
5
5
  require_relative "statisfy/aggregate"
6
6
  require_relative "statisfy/monthly"
7
+ require_relative "statisfy/model"
7
8
 
8
9
  module Statisfy
9
10
  class Error < StandardError; end
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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve
@@ -119,6 +119,7 @@ files:
119
119
  - lib/statisfy/aggregate.rb
120
120
  - lib/statisfy/configuration.rb
121
121
  - lib/statisfy/counter.rb
122
+ - lib/statisfy/model.rb
122
123
  - lib/statisfy/monthly.rb
123
124
  - lib/statisfy/subscriber.rb
124
125
  homepage: https://github.com/Michaelvilleneuve/statisfy