progressrus 0.1.8 → 1.0.0

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: a1e63f1c18708a23b56833afb791eaf7bc27e05243fd4abbd65290e3100d5bf8
4
- data.tar.gz: 5387f3c957800d8292ea2f5ebb81b7e5fdacb3a4d045c0f9b2fb14686256f97f
3
+ metadata.gz: 3cb4b7ee45de1db397c2d3f3cfb720507d8d0cec3f868ae5bf9c206b6f3d0dcf
4
+ data.tar.gz: 624910bddff5383982e2b24f9a179470b17d0191b503c7c111e6199bbbfa47a8
5
5
  SHA512:
6
- metadata.gz: a5039104d899d32e43c908213170555ef5ab1b7031af6099497328195d6985643725c4b09cbd5411edbce03769cd98fcd368e2eca54a79ad78e86a471ba99a01
7
- data.tar.gz: 5ff5e57e00cd6ff54bc35479047a0d40ae41691fb34324697600f5c4c47b983128bc64e1165102f6e3d187f76aeaffa4e6d6920ca92c5adffd3b573d88711b77
6
+ metadata.gz: 5a8e7967dff752a13e2f4a914283855568a5f443782b5d0b146ca78fa425b100597e7d8be282ac6e90a1f3e83a8aa015294df56e2d690d15b61df1a2b65171aa
7
+ data.tar.gz: 5ccbeffcf9168949764092b5a3f2b3b024302f40852e81ee217ba8480f6b1c688303d895a461fa355e285329461463d078518b331d4eb9d79e5f1a6f298c8843
@@ -2,29 +2,65 @@ require 'json'
2
2
  require 'securerandom'
3
3
  require 'redis'
4
4
  require 'time'
5
- require_relative "progressrus/store"
6
5
  require_relative "progressrus/store/base"
7
6
  require_relative "progressrus/store/redis"
8
7
  require_relative "progressrus/store/progressbar"
9
8
  require_relative "progressrus/core_ext/enumerable"
10
9
 
11
10
  class Progressrus
11
+ @mutex = Mutex.new
12
+
13
+ class InvalidStoreError < StandardError
14
+ def initialize
15
+ message = <<~MSG
16
+ The store needs to implement `persist`, `scope`, `find` and `flush`
17
+ We have a base class that your store can inherit from:
18
+ Progressrus::Store::Base
19
+ MSG
20
+ super(message)
21
+ end
22
+ end
23
+
12
24
  class << self
25
+ attr_reader :mutex
26
+
27
+ def clear_stores
28
+ @stores = {}
29
+ end
30
+
13
31
  def stores
14
- @@stores ||= Store.new(Store::Redis.new(::Redis.new(host: ENV["PROGRESSRUS_REDIS_HOST"] || "localhost")))
32
+ mutex.synchronize do
33
+ @stores ||= {
34
+ redis: Store::Redis.new(::Redis.new(host: ENV["PROGRESSRUS_REDIS_HOST"] || "localhost"))
35
+ }
36
+ end
15
37
  end
16
38
 
17
- def scope(scope, store: :first)
18
- stores.find_by_name(store).scope(scope)
39
+ def add_store(name, store)
40
+ validate_store!(store)
41
+ stores[name] = store
42
+ end
43
+
44
+ def scope(scope, store: :redis)
45
+ stores[store].scope(scope)
19
46
  end
20
47
  alias_method :all, :scope
21
48
 
22
- def find(scope, id, store: :first)
23
- stores.find_by_name(store).find(scope, id)
49
+ def find(scope, id, store: :redis)
50
+ stores[store].find(scope, id)
51
+ end
52
+
53
+ def flush(scope, id = nil, store: :redis)
54
+ stores[store].flush(scope, id)
24
55
  end
25
56
 
26
- def flush(scope, id = nil, store: :first)
27
- stores.find_by_name(store).flush(scope, id)
57
+ private
58
+
59
+ def validate_store!(store)
60
+ valid = Store::Base.new.public_methods(false).all? do |method|
61
+ store.respond_to?(method)
62
+ end
63
+ raise InvalidStoreError unless valid
28
64
  end
29
65
  end
30
66
 
@@ -84,7 +120,7 @@ class Progressrus
84
120
  end
85
121
 
86
122
  def flush
87
- stores.each { |store| store.flush(scope, id) }
123
+ stores.each_value { |store| store.flush(scope, id) }
88
124
  end
89
125
 
90
126
  def status
@@ -161,7 +197,7 @@ class Progressrus
161
197
  private
162
198
 
163
199
  def persist(force: false)
164
- stores.each do |store|
200
+ stores.each_value do |store|
165
201
  begin
166
202
  store.persist(self, force: force, expires_at: expires_at)
167
203
  rescue Progressrus::Store::BackendError
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Progressrus
4
- VERSION = '0.1.8'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -7,7 +7,7 @@ namespace :progressrus do
7
7
  task :flush, :environment do |t, args|
8
8
  scope = *args
9
9
  raise ArgumentError.new("Must pass [scope] to progressrus:store:flush[scope(,parts)] task.") unless scope.length > 0
10
- Progressrus.stores.first.flush(scope) if Progressrus.stores.first
10
+ Progressrus.stores[:redis].flush(scope) if Progressrus.stores[:redis]
11
11
  end
12
12
  end
13
13
  end
@@ -6,7 +6,7 @@ class IntegrationTest < Minitest::Test
6
6
  end
7
7
 
8
8
  def teardown
9
- Progressrus.stores.first.flush(@progress.scope)
9
+ Progressrus.stores[:redis].flush(@progress.scope)
10
10
  end
11
11
 
12
12
  def test_create_tick_and_see_status_in_redis
@@ -6,16 +6,23 @@ class ProgressrusTest < Minitest::Test
6
6
  end
7
7
 
8
8
  def teardown
9
- Progressrus.stores.default!
9
+ Progressrus.clear_stores
10
+ Progressrus.add_store(:redis, Progressrus::Store::Redis.new(::Redis.new(host: ENV["PROGRESSRUS_REDIS_HOST"] || "localhost")))
10
11
  end
11
12
 
12
13
  def test_defaults_to_redis_store
13
- assert_instance_of Progressrus::Store::Redis, Progressrus.stores.first
14
+ assert_instance_of Progressrus::Store::Redis, Progressrus.stores[:redis]
14
15
  end
15
16
 
16
17
  def test_add_to_store
17
- Progressrus.stores << Progressrus::Store::Base.new
18
- assert_instance_of Progressrus::Store::Base, Progressrus.stores[1]
18
+ Progressrus.add_store(:test, Progressrus::Store::Base.new)
19
+ assert_instance_of Progressrus::Store::Base, Progressrus.stores[:test]
20
+ end
21
+
22
+ def test_add_store_raise_error_when_invalid_store
23
+ assert_raises(Progressrus::InvalidStoreError) do
24
+ Progressrus.add_store(:test, mock)
25
+ end
19
26
  end
20
27
 
21
28
  def test_scope_should_initialize_with_symbol_or_string
@@ -196,19 +203,20 @@ class ProgressrusTest < Minitest::Test
196
203
  end
197
204
 
198
205
  def test_persist_yields_persist_to_each_store
199
- mysql = mock()
206
+ mysql = mock_store
200
207
  mysql.expects(:persist).once
201
208
 
202
- redis = Progressrus.stores.first
209
+ redis = Progressrus.stores[:redis]
203
210
  redis.expects(:persist).once
204
211
 
205
- Progressrus.stores << mysql
212
+ Progressrus.add_store(:mysql, mysql)
206
213
 
207
- @progress.tick
214
+ progress = Progressrus.new(scope: :progressrus, total: 100)
215
+ progress.tick
208
216
  end
209
217
 
210
218
  def test_tick_and_complete_dont_raise_if_store_is_unavailable
211
- store = Progressrus.stores.first
219
+ store = Progressrus.stores[:redis]
212
220
  store.redis.expects(:hset).at_least_once.raises(::Redis::BaseError)
213
221
  @progress.tick
214
222
  @progress.complete
@@ -234,47 +242,32 @@ class ProgressrusTest < Minitest::Test
234
242
  end
235
243
 
236
244
 
237
- def test_default_scope_on_first
238
- Progressrus.stores.clear
239
-
240
- mysql = mock()
241
- redis = mock()
242
-
243
- Progressrus.stores << mysql
244
- Progressrus.stores << redis
245
-
246
- mysql.expects(:scope).once
247
- redis.expects(:scope).never
245
+ def test_default_scope_redis_store
246
+ Progressrus.clear_stores
248
247
 
249
- Progressrus.scope(["oemg"])
250
- end
251
-
252
- def test_support_scope_last
253
- Progressrus.stores.clear
254
-
255
- mysql = mock()
256
- redis = mock()
248
+ mysql = mock_store
249
+ redis = mock_store
257
250
 
258
- Progressrus.stores << mysql
259
- Progressrus.stores << redis
251
+ Progressrus.add_store(:mysql, mysql)
252
+ Progressrus.add_store(:redis, redis)
260
253
 
261
254
  mysql.expects(:scope).never
262
255
  redis.expects(:scope).once
263
256
 
264
- Progressrus.scope(["oemg"], store: :last)
257
+ Progressrus.scope(["oemg"])
265
258
  end
266
259
 
267
260
  def test_support_scope_by_name
268
- Progressrus.stores.clear
261
+ Progressrus.clear_stores
269
262
 
270
- mysql = mock()
271
- redis = mock()
263
+ mysql = mock_store
264
+ redis = mock_store
272
265
 
273
266
  mysql.stubs(:name).returns(:mysql)
274
267
  redis.stubs(:name).returns(:redis)
275
268
 
276
- Progressrus.stores << mysql
277
- Progressrus.stores << redis
269
+ Progressrus.add_store(:mysql, mysql)
270
+ Progressrus.add_store(:redis, redis)
278
271
 
279
272
  mysql.expects(:scope).never
280
273
  redis.expects(:scope).once
@@ -427,4 +420,10 @@ class ProgressrusTest < Minitest::Test
427
420
 
428
421
  refute progress.expired?(now: time)
429
422
  end
423
+
424
+ private
425
+
426
+ def mock_store
427
+ Progressrus::Store::Base.new
428
+ end
430
429
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progressrus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Eskildsen
@@ -139,7 +139,6 @@ files:
139
139
  - lib/progressrus/core_ext/enumerable.rb
140
140
  - lib/progressrus/railtie.rb
141
141
  - lib/progressrus/server.rb
142
- - lib/progressrus/store.rb
143
142
  - lib/progressrus/store/base.rb
144
143
  - lib/progressrus/store/progressbar.rb
145
144
  - lib/progressrus/store/redis.rb
@@ -1,24 +0,0 @@
1
- class Progressrus
2
- class Store < Array
3
- def initialize(default)
4
- @default = default
5
- self << default
6
- end
7
-
8
- def default
9
- @default
10
- end
11
-
12
- def default!
13
- clear
14
- self << default
15
- end
16
-
17
- def find_by_name(name)
18
- return first if name == :first
19
- return last if name == :last
20
-
21
- find { |store| store.name == name }
22
- end
23
- end
24
- end