active_sample 1.0.0 → 1.0.1

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: 6dd6c0bf33766ec1d797c6f4feb61388e0e7c65d
4
- data.tar.gz: 5e3717c5e1739c29eaaf1fdd79a4cf2817ceb5bc
3
+ metadata.gz: f1513c164798f757b3fc73c5e68c2f35d05af054
4
+ data.tar.gz: a6a0d345d62422af604a2e343c1976e56cd2e5c3
5
5
  SHA512:
6
- metadata.gz: 214b99ef168631ff5636ea41ff42959304ecf9b20628f7d93d30fa87490c39179fa71256629c91ac53faf5273e021478ed940d1a567ff68f22372ab7133f9f24
7
- data.tar.gz: d8f3b7ec8c4ae6e24a97832feec7715388e558db6acf64f2e3501ba24974259ad687a98ec0371149e4fee1913ba7941854bef2cad3bc85313bf6a4d921a2eb9b
6
+ metadata.gz: beea2ea92ce72d877f58e0ce30f0a4219970d216fc4e8c539756e99d0f16e01aebe2380d7bc6478097e4bc10d0fe52ec8161bc5a2ed53376152b283a8027ddd9
7
+ data.tar.gz: 6e11efeb2582aa9b16771b39822f95b0f9e767d36055effd9a6acc74d1551c7b08931a67795b50e9f581c93697ce70c05d51feed37841bccc70dc0d4843675da
data/.travis.yml CHANGED
@@ -2,3 +2,5 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.3.0
5
+ before_script:
6
+ - psql -c "create database test_active_sample;" -U postgres
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## 1.0.1 - 2016.05.07
4
+
5
+ - Improves `Model.sample(1)` case.
6
+
7
+ ## 1.0.0 - 2016.05.05
8
+
9
+ - Initial release :tada:.
data/README.md CHANGED
@@ -1,9 +1,14 @@
1
- # ActiveSample
1
+ # Active Sample
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/active_sample.svg)](https://badge.fury.io/rb/active_sample)
4
+ [![Build Status](https://travis-ci.org/JuanitoFatas/active_sample.svg?branch=master)](https://travis-ci.org/JuanitoFatas/active_sample)
2
5
 
3
6
  Sample functionality for Active Record.
4
7
 
5
8
  ## Usage
6
9
 
10
+ **Don't use this for large set of data for now (See [#1](https://github.com/JuanitoFatas/active_sample/issues/1)).**
11
+
7
12
  ```ruby
8
13
  User.count # => 0
9
14
  User.sample => nil
@@ -1,3 +1,3 @@
1
1
  module ActiveSample
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/lib/active_sample.rb CHANGED
@@ -10,16 +10,18 @@ module ActiveSample
10
10
  raise NegativeSampleError.new("negative sample number".freeze)
11
11
  end
12
12
 
13
- return nil if count == 0
13
+ max_id = maximum(:id)
14
14
 
15
- if n > count
16
- where(id: ids.shuffle)
17
- elsif n > 1
18
- where(id: ids.sample(n))
15
+ return nil if max_id == nil
16
+
17
+ if n == 1
18
+ while !(found = find_by(id: 1 + rand(max_id))); end
19
+
20
+ found
19
21
  elsif n == 0
20
- where(id: [])
22
+ none
21
23
  else
22
- find ids.sample
24
+ where(id: ids.sample(n))
23
25
  end
24
26
  end
25
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_sample
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - JuanitoFatas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-04 00:00:00.000000000 Z
11
+ date: 2016-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -34,6 +34,7 @@ files:
34
34
  - ".gitignore"
35
35
  - ".rspec"
36
36
  - ".travis.yml"
37
+ - CHANGELOG.md
37
38
  - Gemfile
38
39
  - LICENSE.txt
39
40
  - README.md