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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35dfea447fa08cfc82b123c5bc06d6ad2caf5c38
4
- data.tar.gz: ea450c35639d51348a2cc0a781951f1be61917ed
3
+ metadata.gz: 1f6520d84d931e0a006a6f52cb97f6891c098396
4
+ data.tar.gz: 78970ac41e0551eb96cb6837ab614c009f51b691
5
5
  SHA512:
6
- metadata.gz: 609e7bb9a16eeb8f42106f4da8696a3074dfdf3cb52b4ec7dcb6c962294534fc3b731c498258f52630dfffee45fdf8a26ef76996f941edc7724adf1dd5b1000a
7
- data.tar.gz: 8cd1c674c0a696da9437e7383892ae6a4659d10086acefb406e509dd77e9270131f223a0f83d040f5a11824b356b6f55a40e148074fa7791495fdfd50e4d15cf
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
- reset!
20
+ init!
21
21
  end
22
22
 
23
23
 
24
- def sample
25
- return if rest.blank?
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 reset!
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
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSamplooper
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -20,12 +20,19 @@ RSpec.describe SampleModel, type: :model do
20
20
  end
21
21
  end
22
22
 
23
- describe 'sampler sample' do
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.sample).to be_a(SampleModel) }
28
- expect(sampler.sample).to be_nil
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.sample }
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.sample).to be_nil
82
+ expect(sampler.pick).to be_nil
68
83
  end
69
84
 
70
85
  it 'sampler loop get nil' do
71
- expect(sampler.sample).to be_nil
86
+ expect(sampler.pick).to be_nil
72
87
  end
73
88
  end
74
89
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_samplooper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmmpa