progressrus 0.1.8 → 1.0.0
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 +4 -4
- data/lib/progressrus.rb +46 -10
- data/lib/progressrus/version.rb +1 -1
- data/tasks/redis_store.rake +1 -1
- data/test/integration_test.rb +1 -1
- data/test/progressrus_test.rb +35 -36
- metadata +1 -2
- data/lib/progressrus/store.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cb4b7ee45de1db397c2d3f3cfb720507d8d0cec3f868ae5bf9c206b6f3d0dcf
|
4
|
+
data.tar.gz: 624910bddff5383982e2b24f9a179470b17d0191b503c7c111e6199bbbfa47a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a8e7967dff752a13e2f4a914283855568a5f443782b5d0b146ca78fa425b100597e7d8be282ac6e90a1f3e83a8aa015294df56e2d690d15b61df1a2b65171aa
|
7
|
+
data.tar.gz: 5ccbeffcf9168949764092b5a3f2b3b024302f40852e81ee217ba8480f6b1c688303d895a461fa355e285329461463d078518b331d4eb9d79e5f1a6f298c8843
|
data/lib/progressrus.rb
CHANGED
@@ -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
|
-
|
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
|
18
|
-
|
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: :
|
23
|
-
stores
|
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
|
-
|
27
|
-
|
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.
|
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.
|
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
|
data/lib/progressrus/version.rb
CHANGED
data/tasks/redis_store.rake
CHANGED
@@ -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.
|
10
|
+
Progressrus.stores[:redis].flush(scope) if Progressrus.stores[:redis]
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
data/test/integration_test.rb
CHANGED
data/test/progressrus_test.rb
CHANGED
@@ -6,16 +6,23 @@ class ProgressrusTest < Minitest::Test
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def teardown
|
9
|
-
Progressrus.
|
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
|
14
|
+
assert_instance_of Progressrus::Store::Redis, Progressrus.stores[:redis]
|
14
15
|
end
|
15
16
|
|
16
17
|
def test_add_to_store
|
17
|
-
Progressrus.
|
18
|
-
assert_instance_of Progressrus::Store::Base, Progressrus.stores[
|
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 =
|
206
|
+
mysql = mock_store
|
200
207
|
mysql.expects(:persist).once
|
201
208
|
|
202
|
-
redis = Progressrus.stores
|
209
|
+
redis = Progressrus.stores[:redis]
|
203
210
|
redis.expects(:persist).once
|
204
211
|
|
205
|
-
Progressrus.
|
212
|
+
Progressrus.add_store(:mysql, mysql)
|
206
213
|
|
207
|
-
|
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
|
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
|
238
|
-
Progressrus.
|
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
|
-
|
250
|
-
|
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.
|
259
|
-
Progressrus.
|
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"]
|
257
|
+
Progressrus.scope(["oemg"])
|
265
258
|
end
|
266
259
|
|
267
260
|
def test_support_scope_by_name
|
268
|
-
Progressrus.
|
261
|
+
Progressrus.clear_stores
|
269
262
|
|
270
|
-
mysql =
|
271
|
-
redis =
|
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.
|
277
|
-
Progressrus.
|
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.
|
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
|
data/lib/progressrus/store.rb
DELETED
@@ -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
|