active_record_samplooper 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_record_samplooper.rb +20 -5
- data/lib/active_record_samplooper/version.rb +1 -1
- data/spec/models/sample_model_spec.rb +21 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f6520d84d931e0a006a6f52cb97f6891c098396
|
4
|
+
data.tar.gz: 78970ac41e0551eb96cb6837ab614c009f51b691
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5522057491651ba49888a06e112af13b16006761e9fad23ef885a6ecef71efbbb324c5576ef204e430c81ca63730ee9cf72115439b0f41b94df0bfcf12b0f4e
|
7
|
+
data.tar.gz: a37ea8835a03a14ba6926f5cc3962ecff77354a58a654a0c8a7cd7196eea5eb8c77a113769e8a89ca61d7b9c7c661e6b64fefcb905b6add294836205ce1c7cbe
|
@@ -17,28 +17,43 @@ module ActiveRecordSamplooper
|
|
17
17
|
|
18
18
|
def initialize(klass)
|
19
19
|
self.klass = klass
|
20
|
-
|
20
|
+
init!
|
21
21
|
end
|
22
22
|
|
23
23
|
|
24
|
-
def
|
25
|
-
|
26
|
-
klass.find(id = rest.shift)
|
24
|
+
def find(id)
|
25
|
+
klass.find(id)
|
27
26
|
rescue ActiveRecord::RecordNotFound => e
|
28
27
|
raise Gone, id
|
29
28
|
end
|
30
29
|
|
31
30
|
|
31
|
+
def sample
|
32
|
+
find(id_store.sample)
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def pick
|
37
|
+
return if rest.blank?
|
38
|
+
find(rest.shift)
|
39
|
+
end
|
40
|
+
|
41
|
+
|
32
42
|
def loop
|
33
43
|
reset! if rest.blank?
|
34
44
|
sample
|
35
45
|
end
|
36
46
|
|
37
47
|
|
38
|
-
def
|
48
|
+
def init!
|
39
49
|
self.id_store = klass.pluck(:id).shuffle!
|
40
50
|
self.rest = id_store.dup
|
41
51
|
end
|
52
|
+
|
53
|
+
|
54
|
+
def reset!
|
55
|
+
self.rest = id_store.dup
|
56
|
+
end
|
42
57
|
end
|
43
58
|
|
44
59
|
class Gone < StandardError
|
@@ -20,12 +20,19 @@ RSpec.describe SampleModel, type: :model do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
|
24
|
+
describe 'from sampler' do
|
24
25
|
let(:sampler) { SampleModel.sampler }
|
25
26
|
|
27
|
+
describe 'sampler pick' do
|
28
|
+
it 'any times, get SampleModel instance' do
|
29
|
+
36.times { expect(sampler.sample).to be_a(SampleModel) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
26
33
|
it 'sample get once each' do
|
27
|
-
10.times { expect(sampler.
|
28
|
-
expect(sampler.
|
34
|
+
10.times { expect(sampler.pick).to be_a(SampleModel) }
|
35
|
+
expect(sampler.pick).to be_nil
|
29
36
|
end
|
30
37
|
|
31
38
|
it 'loop get any times each' do
|
@@ -41,7 +48,7 @@ RSpec.describe SampleModel, type: :model do
|
|
41
48
|
SampleModel.first.destroy
|
42
49
|
SampleModel.first.destroy
|
43
50
|
expect {
|
44
|
-
10.times { sampler.
|
51
|
+
10.times { sampler.pick }
|
45
52
|
}.to raise_exception(ActiveRecordSamplooper::Gone)
|
46
53
|
end
|
47
54
|
|
@@ -53,9 +60,17 @@ RSpec.describe SampleModel, type: :model do
|
|
53
60
|
10.times { sampler.loop }
|
54
61
|
}.to raise_exception(ActiveRecordSamplooper::Gone)
|
55
62
|
end
|
63
|
+
|
64
|
+
it 'sampler not include new instance' do
|
65
|
+
sampler
|
66
|
+
new_id = create(:sample_model).id
|
67
|
+
1000.times { expect(sampler.sample.id).not_to eq(new_id) }
|
68
|
+
SampleModel.destroy(new_id)
|
69
|
+
end
|
56
70
|
end
|
57
71
|
end
|
58
72
|
|
73
|
+
|
59
74
|
describe 'no record' do
|
60
75
|
it 'direct sample get nil' do
|
61
76
|
expect(SampleModel.sample).to be_nil
|
@@ -64,11 +79,11 @@ RSpec.describe SampleModel, type: :model do
|
|
64
79
|
let(:sampler) { SampleModel.sampler }
|
65
80
|
|
66
81
|
it 'sampler sample get nil' do
|
67
|
-
expect(sampler.
|
82
|
+
expect(sampler.pick).to be_nil
|
68
83
|
end
|
69
84
|
|
70
85
|
it 'sampler loop get nil' do
|
71
|
-
expect(sampler.
|
86
|
+
expect(sampler.pick).to be_nil
|
72
87
|
end
|
73
88
|
end
|
74
89
|
end
|