Class: Randumb::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/randumb/syntax.rb

Class Method Summary collapse

Class Method Details

.random_order_clause(opts = {}) ⇒ Object

builds the order clause to be appended in where clause



6
7
8
# File 'lib/randumb/syntax.rb', line 6

def random_order_clause(opts={})
  random_for(opts)
end

.random_weighted_order_clause(ranking_column, opts = {}) ⇒ Object

builds the order clause to be appended in where clause



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/randumb/syntax.rb', line 11

def random_weighted_order_clause(ranking_column, opts={})
  connection = opts[:connection]

  if connection.adapter_name =~ /sqlite/i
    # computer multiplication is faster than division I was once taught...so translate here
    max_int = 9223372036854775807.0
    multiplier = 1.0 / max_int
    "(#{ranking_column} * ABS(#{random_for(opts)} * #{multiplier}) ) DESC"
  else
    "(#{ranking_column} * #{random_for(opts)}) DESC"
  end
end