randumb 0.5.0 → 0.5.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: b7a8de94fe998a9315531dc1785ccec096ed4a7e
4
- data.tar.gz: b7e3d1440cc41ecd3231f8f9cde03361414cc329
3
+ metadata.gz: 7dfd32d6c13a988c2fb72afea69f8e7d2824b2cf
4
+ data.tar.gz: 77ad6781fc9a3618757d916ed815e3356d1b14a0
5
5
  SHA512:
6
- metadata.gz: b215658a671a55d23fa68db3807ba2f2b6b204263ecb200f1a664f2f25bb58c0137132b1a67d4bbacaebf73ab7025f5a1348c7d2c722dc9f58837fa92faf024c
7
- data.tar.gz: 7ea4b738b0cca2b7544bda4a55eb0e74c0e662cab6d85a46322086ddae7fa32def04afad55e82b5cb3fec8542a86116ca0ae00551803fdcc002695e665d0316b
6
+ metadata.gz: a0a9f3960ff23cf85fd19283ebed1027382841b19f1c4a55292ce15fccce324c0f95b1400f3e17af952bebb7da89894ca8026f117a018e88f4836d3bc4175088
7
+ data.tar.gz: d01853119a813ed7f26933ce15db02a1b52ca2be5c74e9fef131facef27a9c4ab417819ebe4cc2b4acdbdd1a144ad98385e53ef8a5944d0936de4d5abe85c599
@@ -57,14 +57,21 @@ module Randumb
57
57
  # get the records and shuffle since the order of the ids
58
58
  # passed to where() isn't retained in the result set
59
59
  rng = random_number_generator(opts)
60
- records = the_scope.where(:id => ids).shuffle!(:random => rng)
60
+ records = the_scope.where(:id => ids).to_a.shuffle!(:random => rng)
61
61
 
62
62
  # return first record if method was called without parameters
63
63
  return_first_record ? records.first : records
64
64
  end
65
65
 
66
- def order_by_rand(opts={})
67
- build_order_scope(opts)
66
+ def order_by_rand(opts = {})
67
+ if opts.is_a?(Hash)
68
+ build_order_scope(opts)
69
+ else
70
+ raise ArgumentError.new(
71
+ "order_by_rand() expects a hash of options. If you need to limit "\
72
+ "results simply add a limit to your scope ex: Artist.order_by_rand.limit(1)"
73
+ )
74
+ end
68
75
  end
69
76
 
70
77
  def order_by_rand_weighted(ranking_column, opts={})
@@ -89,7 +96,7 @@ module Randumb
89
96
  order(order_clause)
90
97
  else
91
98
  # keep prior orders and append random
92
- all_orders = (orders + [order_clause]).join(", ")
99
+ all_orders = (orders + [order_clause])
93
100
  # override all previous orders
94
101
  reorder(all_orders)
95
102
  end
@@ -118,7 +125,7 @@ module Randumb
118
125
 
119
126
  # Returns all matching ids from the db, shuffles them,
120
127
  # then returns an array containing at most max_ids
121
- def fetch_random_ids(relation, max_ids, opts={})
128
+ def fetch_random_ids(relation, max_ids, opts = {})
122
129
  # clear these for our id only query
123
130
  relation.select_values = []
124
131
  relation.includes_values = []
@@ -131,11 +138,11 @@ module Randumb
131
138
  rng = random_number_generator(opts)
132
139
  if max_ids == 1 && id_results.count > 0
133
140
  rand_index = rng.rand(id_results.count)
134
- [ id_results[ rand_index ]['id'] ]
141
+ [id_results[rand_index]["id"]]
135
142
  else
136
- # ActiveRecord 4 requires .to_ary
137
- arr = id_results.respond_to?(:to_ary) ? id_results.to_ary : id_results
138
- arr.shuffle!(:random => rng)[0,max_ids].collect!{ |h| h['id'] }
143
+ # ActiveRecord 4 requires .to_a
144
+ arr = id_results.respond_to?(:to_a) ? id_results.to_a : id_results
145
+ arr.shuffle!(random: rng)[0, max_ids].collect! { |h| h["id"] }
139
146
  end
140
147
  end
141
148
 
@@ -1,3 +1,3 @@
1
1
  module Randumb
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -1,10 +1,15 @@
1
1
  $:.unshift '.'; require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
- class RandumbTest < Test::Unit::TestCase
3
+ class RandumbTest < Minitest::Test
4
4
 
5
5
  def assert_equal_for_both_methods(expected, obj, *params)
6
- assert_equal expected, obj.send(:random, *params), "when calling random"
7
- assert_equal expected, obj.send(:random_by_id_shuffle, *params), "when calling random_by_id_shuffle"
6
+ if expected.nil?
7
+ assert_nil obj.send(:random, *params), "when calling random"
8
+ assert_nil obj.send(:random_by_id_shuffle, *params), "when calling random_by_id_shuffle"
9
+ else
10
+ assert_equal expected, obj.send(:random, *params), "when calling random"
11
+ assert_equal expected, obj.send(:random_by_id_shuffle, *params), "when calling random_by_id_shuffle"
12
+ end
8
13
  end
9
14
 
10
15
  should "should return empty when no record in table" do
@@ -25,6 +30,13 @@ class RandumbTest < Test::Unit::TestCase
25
30
  assert_equal_for_both_methods nil, Artist.limit(50)
26
31
  end
27
32
 
33
+ should "raise helpful error if non-hash passed" do
34
+ error = assert_raises ArgumentError do
35
+ Artist.order_by_rand(10)
36
+ end
37
+ assert error.message.include?("order_by_rand")
38
+ end
39
+
28
40
  context "1 record in the table" do
29
41
  setup do
30
42
  @high_on_fire = FactoryGirl.create(:artist, :name => "High On Fire", :views => 1)
@@ -65,19 +77,22 @@ class RandumbTest < Test::Unit::TestCase
65
77
  end
66
78
 
67
79
  should "respect selecting certain columns" do
80
+ # this version doesn't throw for some reason...
81
+ skip_throw_missing = ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
82
+
68
83
  assert_equal 3, Artist.find(@fiona_apple.id).views
69
84
 
70
85
  artists = Artist.select(:name).random(3)
71
86
  assert_equal false, artists.first.name.nil?
72
- assert_raise (ActiveModel::MissingAttributeError) {artists.first.views}
87
+ assert_raises (ActiveModel::MissingAttributeError) { artists.first.views } unless skip_throw_missing
73
88
 
74
89
  artists = Artist.select(:name).order_by_rand.limit(3)
75
90
  assert_equal false, artists.first.name.nil?
76
- assert_raise (ActiveModel::MissingAttributeError) {artists.first.views}
91
+ assert_raises (ActiveModel::MissingAttributeError) { artists.first.views } unless skip_throw_missing
77
92
 
78
93
  artists = Artist.select(:name).random_by_id_shuffle(3)
79
94
  assert_equal false, artists.first.name.nil?
80
- assert_raise (ActiveModel::MissingAttributeError) {artists.first.views}
95
+ assert_raises (ActiveModel::MissingAttributeError) { artists.first.views } unless skip_throw_missing
81
96
  end
82
97
 
83
98
  should "respect scopes" do
@@ -153,8 +168,8 @@ class RandumbTest < Test::Unit::TestCase
153
168
  should "work with uniq" do
154
169
  assert_equal 2, Artist.uniq.random(2).length
155
170
  assert_equal 2, Artist.uniq.random_by_id_shuffle(2).length
156
- assert_not_nil Artist.uniq.random
157
- assert_not_nil Artist.uniq.random_by_id_shuffle
171
+ assert !Artist.uniq.random.nil?
172
+ assert !Artist.uniq.random_by_id_shuffle.nil?
158
173
  end
159
174
  end
160
175
 
@@ -211,7 +226,7 @@ class RandumbTest < Test::Unit::TestCase
211
226
  assert_equal seeded_order, Artist.random(2, seed: @seed)
212
227
  end
213
228
 
214
- 10.times do
229
+ 10.times do
215
230
  assert_equal seeded_order, Artist.order_by_rand(seed: @seed).limit(2)
216
231
  end
217
232
  end
@@ -1,6 +1,6 @@
1
1
  $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
2
  require 'rubygems'
3
- require 'test/unit'
3
+ require "minitest/autorun"
4
4
  require 'shoulda'
5
5
  require 'factory_girl'
6
6
  require 'faker'
@@ -14,14 +14,14 @@ config = YAML::load(File.open(File.expand_path("../databases.yml", __FILE__)))
14
14
  version = ActiveRecord::VERSION::STRING
15
15
  driver = (ENV["DB"] or "sqlite3").downcase
16
16
  in_memory = config[driver]["database"] == ":memory:"
17
-
17
+
18
18
  # http://about.travis-ci.org/docs/user/database-setup/
19
19
  commands = {
20
20
  "mysql" => "mysql -e 'create database randumb_test;'",
21
21
  "postgres" => "psql -c 'create database randumb_test;' -U postgres"
22
22
  }
23
23
  %x{#{commands[driver] || true}}
24
-
24
+
25
25
  ActiveRecord::Base.establish_connection config[driver]
26
26
  puts "Using #{RUBY_VERSION} AR #{version} with #{driver}"
27
27
 
@@ -33,7 +33,7 @@ ActiveRecord::Base.connection.create_table(:artists, :force => true) do |t|
33
33
  t.datetime "created_at"
34
34
  t.datetime "updated_at"
35
35
  end
36
-
36
+
37
37
  ActiveRecord::Base.connection.create_table(:albums, :force => true) do |t|
38
38
  t.string "name"
39
39
  t.integer "views"
@@ -41,7 +41,7 @@ ActiveRecord::Base.connection.create_table(:albums, :force => true) do |t|
41
41
  t.datetime "created_at"
42
42
  t.datetime "updated_at"
43
43
  end
44
-
44
+
45
45
  # setup models for lazy load
46
46
  dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
47
47
  dep.autoload_paths.unshift MODELS_PATH
@@ -50,7 +50,7 @@ dep.autoload_paths.unshift MODELS_PATH
50
50
  require 'test/models/factories'
51
51
 
52
52
  # clear db for every test
53
- class Test::Unit::TestCase
53
+ class Minitest::Test
54
54
 
55
55
  def setup
56
56
  Artist.delete_all
@@ -1,6 +1,6 @@
1
1
  $:.unshift '.'; require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
- class WeightedTest < Test::Unit::TestCase
3
+ class WeightedTest < Minitest::Test
4
4
 
5
5
  should "raise exception when called with a non-existent column" do
6
6
  assert_raises(ArgumentError) do
@@ -66,7 +66,7 @@ class WeightedTest < Test::Unit::TestCase
66
66
  assert_hits_per_views do
67
67
  Artist.random_weighted("views").views
68
68
  end
69
- assert_hits_per_views do
69
+ assert_hits_per_views do
70
70
  Artist.order_by_rand_weighted("views").first.views
71
71
  end
72
72
  end
@@ -84,7 +84,7 @@ class WeightedTest < Test::Unit::TestCase
84
84
  result.first.views
85
85
  end
86
86
 
87
- assert_hits_per_views do
87
+ assert_hits_per_views do
88
88
  result = Artist.order_by_rand_weighted("views").limit(5).all
89
89
  assert(result.size == 5)
90
90
  result.first.views
@@ -120,13 +120,13 @@ class WeightedTest < Test::Unit::TestCase
120
120
  should "order by ranking column with method_missing using 1 max_items" do
121
121
  assert_hits_per_views do
122
122
  result = Artist.random_weighted_by_views(1)
123
- assert(result.size == 1)
123
+ assert_equal 1, result.length
124
124
  result.first.views
125
125
  end
126
126
 
127
127
  assert_hits_per_views do
128
128
  result = Artist.order_by_rand_weighted(:views).limit(1)
129
- assert(result.size == 1)
129
+ assert_equal 1, result.length
130
130
  result.first.views
131
131
  end
132
132
  end
@@ -135,7 +135,7 @@ class WeightedTest < Test::Unit::TestCase
135
135
  def assert_hits_per_views
136
136
  hits_per_views = Hash.new
137
137
  @view_counts.each { |views| hits_per_views[views] = 0 }
138
-
138
+
139
139
  1000.times do
140
140
  hits_per_views[yield] += 1
141
141
  end
@@ -148,4 +148,4 @@ class WeightedTest < Test::Unit::TestCase
148
148
  end
149
149
 
150
150
 
151
- end
151
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: randumb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Kloepping
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-22 00:00:00.000000000 Z
11
+ date: 2017-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -30,42 +30,56 @@ dependencies:
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: 3.0.0
33
+ version: 3.0.20
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: 3.0.0
40
+ version: 3.0.20
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activerecord
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '>='
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0
47
+ version: 3.0.20
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0
54
+ version: 3.0.20
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: sqlite3
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '='
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 1.3.7
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '='
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
67
74
  - !ruby/object:Gem::Version
68
- version: 1.3.7
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: shoulda
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -84,16 +98,16 @@ dependencies:
84
98
  name: factory_girl
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - ~>
101
+ - - '>='
88
102
  - !ruby/object:Gem::Version
89
- version: '3.0'
103
+ version: '0'
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - ~>
108
+ - - '>='
95
109
  - !ruby/object:Gem::Version
96
- version: '3.0'
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: faker
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +122,20 @@ dependencies:
108
122
  - - '>='
109
123
  - !ruby/object:Gem::Version
110
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
111
139
  description:
112
140
  email:
113
141
  executables: []
@@ -144,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
172
  version: '0'
145
173
  requirements: []
146
174
  rubyforge_project:
147
- rubygems_version: 2.1.10
175
+ rubygems_version: 2.0.14.1
148
176
  signing_key:
149
177
  specification_version: 4
150
178
  summary: Adds the ability to pull random records from ActiveRecord