Class: Cassanity::ArgumentGenerators::WhereClause

Inherits:
Object
  • Object
show all
Defined in:
lib/cassanity/argument_generators/where_clause.rb

Instance Method Summary collapse

Instance Method Details

#call(args = {}) ⇒ Object

Internal



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cassanity/argument_generators/where_clause.rb', line 13

def call(args = {})
  where = args[:where]
  cql = ''
  return [cql] if where.nil? || where.empty?

  variables, wheres = [], []

  where.each do |key, value|
    case value
    when Array
      wheres << "#{key} IN (?)"
      variables << value
    when Range
      start, finish = value.begin, value.end
      end_operator = value.exclude_end? ? '<' : '<='
      wheres << "#{key} >= ?"
      wheres << "#{key} #{end_operator} ?"
      variables << start
      variables << finish
    when Cassanity::Operator
      wheres << "#{key} #{value.symbol} ?"
      variables << value.value
    else
      wheres << "#{key} = ?"
      variables << value
    end
  end

  cql << " WHERE #{wheres.join(' AND ')}"

  [cql, *variables]
end