active_record_samplooper 0.0.4 → 0.0.6

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: 93d485e74aa6069680edeb330cc7ce8c4c8a376c
4
- data.tar.gz: ab4b473a2b67acc840bbbf937af758f49e0b82b0
3
+ metadata.gz: 7a6fc2ce8bdbe8e29e1c01a4d10244b54cc105e6
4
+ data.tar.gz: ccdc37e1c5d8f6307e405efdfda20b3fd0068b8a
5
5
  SHA512:
6
- metadata.gz: 360c1d888bbf00a5704752c3e7c75a641e9f33286612c847bf59cee523d39e90ef4d5706d3571760d29c51c3c557bd088e9f228be2c46d33b84c8d7b5af557db
7
- data.tar.gz: a5621a045cf40a4cc1d1fbe504e0d68cb4a8a771cbc7e2c28d7ea548670e44126141be7f37a9db7cc7fd93588d5521dc1d76802526e015565cf89ce2dd7afd7c
6
+ metadata.gz: 338b2826f9e46c468e7849b658344531e81b625e549ab1848756b71d8170e0b2dbce2991f5dab58f99bfcc94c02b085f81b455ede0b32746f3c9d3b6f9274824
7
+ data.tar.gz: 2ed837bad209238a913e839abdb5326512f511327dbb52f48e65f0b5812f7b4cbb55dff3aebc4651d4e1b36d49c8f027609404b9c22f390ddd4c6ce076bbf11f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_record_samplooper (0.0.3)
4
+ active_record_samplooper (0.0.6)
5
5
  rails (~> 4.2.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,2 +1,121 @@
1
1
  [![Build Status](https://travis-ci.org/mmmpa/active_record_samplooper.svg)](https://travis-ci.org/mmmpa/active_record_samplooper)
2
- [![Coverage Status](https://coveralls.io/repos/mmmpa/yorisoi/badge.svg?branch=master)](https://coveralls.io/r/mmmpa/active_record_samplooper?branch=master)
2
+ [![Coverage Status](https://coveralls.io/repos/mmmpa/yorisoi/badge.svg?branch=master)](https://coveralls.io/r/mmmpa/active_record_samplooper?branch=master)
3
+
4
+ # ActiveRecordSamplooper
5
+
6
+ ActiveRecordSamplooperはArray#sampleをActiveRecordでもやりたくて書かれました。
7
+
8
+ おもにテストデータ作成に使っています。
9
+
10
+ ```ruby
11
+ SampleModel.sample
12
+ => #<SampleModel:0x007ff4e2077068 id: 40, ast_name: "松尾", first_name: "寛治", last_name_reading: "まつお", first_name_reading: "かんじ", email: "matsuo_kanji@example.com", gender: "male", age: 74, birth: Wed, 26 Jun 1940, tel: "090-">
13
+ ```
14
+ あるいは
15
+ ```ruby
16
+ SampleModel.sample(3)
17
+ => [
18
+ #<SampleModel:0x007fb73ec99030 id: 16, last_name: "七瀬", first_name: "希", last_name_reading: "ななせ", first_name_reading: "のぞみ", email: "nanase_nozomi@example.com", gender: "female", age: 64, birth: Tue, 23 Jan 1951, tel: "080-9991-7001">,
19
+ #<SampleModel:0x007fb73d78c9f8 id: 29, last_name: "長谷部", first_name: "樹里", last_name_reading: "はせべ", first_name_reading: "じゅり", email: "hasebe_juri@example.com", gender: "female", age: 43, birth: Sat, 09 Oct 1971, tel: "090-4943-9297">,
20
+ #<SampleModel:0x007fb73d786d00 id: 29, last_name: "長谷部", first_name: "樹里", last_name_reading: "はせべ", first_name_reading: "じゅり", email: "hasebe_juri@example.com", gender: "female", age: 43, birth: Sat, 09 Oct 1971, tel: "090-4943-9297">
21
+ ]
22
+ ```
23
+ `where`で絞って
24
+ ```ruby
25
+ SampleModel.where(gender: :male).sample(10).map{|m| [m.first_name, m.gender].join(' : ')}
26
+ => ["洋介 : male", "勇一 : male", "良介 : male", "晃司 : male", "仁晶 : male", "良介 : male", "一樹 : male", "明 : male", "禄郎 : male", "晃司 : male"]
27
+ ```
28
+ ただしダイレクトに`sample`するより(Samplooper)[#Samplooper]を介した方がよさそう。
29
+
30
+ ## Installation
31
+ ```ruby
32
+ gem 'active_record_samplooper'
33
+ ```
34
+ $ bundle install
35
+
36
+ ## Usage
37
+
38
+ `Samplooper`を介すと軽かったり多少便利だったりします。
39
+
40
+ ***
41
+
42
+ ### Samplooper
43
+
44
+ 直接の`sample`のほかに、`sampler`メソッドで`Samplooper`インスタンスを取得、`Samplooper#sample` `Samplooper#pick` `Samplooper#loop`が行えます。
45
+ それぞれ引数に1以上の整数をとり、1の場合はレコードを、2以上の場合はレコードを含む配列を返します。
46
+ * **sample(n = 1)**
47
+ 先行取得したidたちを元にレコードを取得。被りあり。
48
+ * **pick(n = 1)**
49
+ 先行取得したidたちを元にレコードを取得。各レコードは1度のみ出現する。1周すると`nil`が帰ってくる。
50
+ * **loop(n = 1)**
51
+ 先行取得したidたちを元にレコードを取得。各レコードは1周に1度のみ出現する。
52
+
53
+ ***
54
+
55
+ ### sample
56
+
57
+ ```ruby
58
+ sampler = SampleModel.limit(3).sampler
59
+ sampler.sample.id
60
+ => 3
61
+ sampler.sample.id
62
+ => 1
63
+ sampler.sample(15).map(&:id)
64
+ => [3, 3, 2, 3, 1, 3, 3, 3, 1, 3, 1, 3, 2, 1, 1]
65
+ ```
66
+
67
+ ***
68
+
69
+ ### pick
70
+
71
+ ```ruby
72
+ sampler = SampleModel.limit(3).sampler
73
+ sampler.pick(5).map{ |m| m.try(:id)}
74
+ => [2, 1, 3, nil, nil]
75
+
76
+ sampler = SampleModel.limit(3).sampler
77
+ sampler.pick.id
78
+ => 1
79
+ sampler.pick(5).map{ |m| m.try(:id)}
80
+ => [2, 3, nil, nil, nil]
81
+ ```
82
+
83
+ ***
84
+
85
+ ### loop
86
+
87
+ ```ruby
88
+ sampler = SampleModel.limit(3).sampler
89
+ sampler.loop.id
90
+ => 2
91
+ sampler.loop.id
92
+ => 1
93
+ sampler.loop.id
94
+ => 3
95
+ sampler.loop(15).map(&:id)
96
+ => [
97
+ 3, 1, 2,
98
+ 1, 3, 2,
99
+ 3, 1, 2,
100
+ 1, 2, 3,
101
+ 2, 3, 1
102
+ ]
103
+ ```
104
+
105
+ ***
106
+
107
+ `where`とかも併用できます。
108
+ ```ruby
109
+ SampleModel.where(gender: :male).sample(10).map{|m| [m.first_name, m.gender].join(' : ')}
110
+ => ["洋介 : male", "勇一 : male", "良介 : male", "晃司 : male", "仁晶 : male", "良介 : male", "一樹 : male", "明 : male", "禄郎 : male", "晃司 : male"]
111
+ ```
112
+ 上は毎回ActiveRecord_Relation取ってくるのでちょっと遅いので`sampler`でidを先行取得して`sample`するとちょっと早い。
113
+ ```ruby
114
+ sampler = SampleModel.where(gender: :male).sampler
115
+ sampler.sample(10).map{|m| [m.first_name, m.gender].join(' : ')}
116
+ => ["悟志 : male", "良介 : male", "慎之介 : male", "寛治 : male", "浩正 : male", "隆之介 : male", "禄郎 : male", "浩正 : male", "一樹 : male", "良介 : male"]
117
+ ```
118
+
119
+ ## License
120
+
121
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -55,17 +55,17 @@ module ActiveRecordSamplooper
55
55
 
56
56
 
57
57
  def sample(count = 1)
58
- count > 1 ? count.times.map { do_sample } : do_sample
58
+ count > 1 ? count.times.map { do_sampling } : do_sampling
59
59
  end
60
60
 
61
61
 
62
62
  def pick(count = 1)
63
- count > 1 ? count.times.map { do_pick } : do_pick
63
+ count > 1 ? count.times.map { do_picking } : do_picking
64
64
  end
65
65
 
66
66
 
67
67
  def loop(count = 1)
68
- count > 1 ? count.times.map { do_loop } : do_loop
68
+ count > 1 ? count.times.map { do_looping } : do_looping
69
69
  end
70
70
 
71
71
 
@@ -76,23 +76,23 @@ module ActiveRecordSamplooper
76
76
 
77
77
 
78
78
  def reset!
79
- self.rest = id_store.dup
79
+ self.rest = id_store.dup.shuffle
80
80
  end
81
81
 
82
82
 
83
83
  private
84
- def do_sample
84
+ def do_sampling
85
85
  find(id_store.sample)
86
86
  end
87
87
 
88
88
 
89
- def do_pick
89
+ def do_picking
90
90
  return if rest.blank?
91
91
  find(rest.shift)
92
92
  end
93
93
 
94
94
 
95
- def do_loop
95
+ def do_looping
96
96
  reset! if rest.blank?
97
97
  pick
98
98
  end
@@ -118,13 +118,18 @@ end
118
118
 
119
119
  class ::ActiveRecord::Base
120
120
  class << self
121
- def sample
122
- offset(rand(count(:all))).first
121
+ def sample(count = 1)
122
+ count > 1 ? count.times.map { do_sampling } : do_sampling
123
123
  end
124
124
 
125
125
 
126
126
  def sampler
127
127
  ActiveRecordSamplooper.(self)
128
128
  end
129
+
130
+ private
131
+ def do_sampling
132
+ offset(rand(count(:all))).first
133
+ end
129
134
  end
130
135
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSamplooper
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -34,7 +34,7 @@ RSpec.describe SampleModel, type: :model do
34
34
  end
35
35
  end
36
36
 
37
- it 'sample get once each' do
37
+ it 'pick get once each' do
38
38
  10.times { expect(sampler.pick).to be_a(SampleModel) }
39
39
  expect(sampler.pick).to be_nil
40
40
  end
@@ -70,6 +70,32 @@ RSpec.describe SampleModel, type: :model do
70
70
  new_id = create(:sample_model).id
71
71
  1000.times { expect(sampler.sample.id).not_to eq(new_id) }
72
72
  end
73
+
74
+ context 'fixnum parameter get multiple element' do
75
+ it 'pick(n) get multiple element' do
76
+ re = sampler.pick(3)
77
+ expect(re.size).to eq(3)
78
+ re.each do |element|
79
+ expect(element).to be_a(SampleModel)
80
+ end
81
+ end
82
+
83
+ it 'loop(n) get multiple element' do
84
+ re = sampler.loop(3)
85
+ expect(re.size).to eq(3)
86
+ re.each do |element|
87
+ expect(element).to be_a(SampleModel)
88
+ end
89
+ end
90
+
91
+ it 'sample(n) get multiple element' do
92
+ re = sampler.sample(3)
93
+ expect(re.size).to eq(3)
94
+ re.each do |element|
95
+ expect(element).to be_a(SampleModel)
96
+ end
97
+ end
98
+ end
73
99
  end
74
100
  end
75
101
 
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.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmmpa