quick_random_records 0.1.0 → 0.2.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/Gemfile +0 -1
- data/lib/quick_random_records/version.rb +1 -1
- data/lib/quick_random_records.rb +49 -15
- data/quick_random_records.gemspec +0 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8460dba37c8d16afde91b5f8ed2e85edb122c5ef
|
4
|
+
data.tar.gz: 3ff4a0cdc2df474bfb3e11718e25669582b75797
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33fe51141b1fb07b2a0fdd77d7f188c69da47bcee9854de1513446a205d59b5e2fdad410764840211a8a38d573ad82f8bf13b9c83d656735cf0f2d0d2f8c24db
|
7
|
+
data.tar.gz: 2019ea12883b3bc1c6e848709b5c9ba2c44e95a933242a8231e0886a9c91842113d20801b2764b94a55be4a1fef49973b65ef9f4f166c3d013e25596a7e9f360
|
data/Gemfile
CHANGED
data/lib/quick_random_records.rb
CHANGED
@@ -1,27 +1,61 @@
|
|
1
1
|
require 'quick_random_records/version'
|
2
2
|
require 'active_record'
|
3
|
-
require 'rails_or'
|
4
3
|
|
5
4
|
class ActiveRecord::Base
|
6
|
-
def self.random_records(quantity)
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
def self.random_records(quantity, strategy: 1, multiple: 1.25, loop_limit: 3)
|
6
|
+
case strategy
|
7
|
+
when 1
|
8
|
+
self.sample_complement_records(quantity, multiple, loop_limit)
|
9
|
+
when 2
|
10
|
+
self.order_rand_limit_records(quantity)
|
11
|
+
when 3
|
12
|
+
self.pluck_sample_records(quantity)
|
13
|
+
else
|
14
|
+
"this gem doesn't support strategy other than 1, 2, 3"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def self.sample_complement_records(quantity, multiple, loop_limit)
|
21
|
+
min_max = self.pluck('MIN(id), MAX(id)').first
|
22
|
+
id_range = (min_max[0]..min_max[1])
|
10
23
|
|
11
|
-
|
12
|
-
|
24
|
+
samples = [*id_range].sample(quantity * multiple)
|
25
|
+
exist_samples = self.where(id: samples).pluck(:id)
|
26
|
+
exist_samples_size = exist_samples.size
|
27
|
+
deficit = quantity - exist_samples_size
|
28
|
+
exist_samples_size = 1 if exist_samples_size.zero?
|
29
|
+
deficit_weight = quantity * multiple / exist_samples_size
|
30
|
+
n = 1
|
13
31
|
|
14
|
-
|
15
|
-
|
16
|
-
|
32
|
+
while deficit > 0 && n <= loop_limit
|
33
|
+
complements = ([*id_range] - samples).sample(deficit * multiple * deficit_weight * n)
|
34
|
+
exist_complements = self.where(id: complements).pluck(:id)
|
17
35
|
|
18
|
-
|
19
|
-
|
20
|
-
|
36
|
+
deficit -= exist_complements.size
|
37
|
+
samples += complements
|
38
|
+
exist_samples += exist_complements
|
39
|
+
n += 1
|
40
|
+
end
|
41
|
+
|
42
|
+
self.where(id: exist_samples[0...quantity])
|
43
|
+
end
|
21
44
|
|
22
|
-
|
45
|
+
def self.order_rand_limit_records(quantity)
|
46
|
+
adapter_type = connection.adapter_name.downcase.to_sym
|
47
|
+
case adapter_type
|
48
|
+
when :mysql, :mysql2
|
49
|
+
self.order("RAND()").limit(quantity)
|
50
|
+
when :sqlite, :postgresql
|
51
|
+
self.order("RANDOM()").limit(quantity)
|
52
|
+
else
|
53
|
+
raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
|
23
54
|
end
|
55
|
+
end
|
24
56
|
|
25
|
-
|
57
|
+
def self.pluck_sample_records(quantity)
|
58
|
+
ids = self.pluck(:id).sample(quantity)
|
59
|
+
self.where(id: ids)
|
26
60
|
end
|
27
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quick_random_records
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- derekfan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: rails_or
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :runtime
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
83
|
description: Returns random records for Ruby Models fast and quick
|
98
84
|
email:
|
99
85
|
- haoping.fan@gmail.com
|