Class: Believer::OrderBy

Inherits:
Object
  • Object
show all
Defined in:
lib/believer/order_by.rb

Overview

Encapsulates the CQL ORDER BY clause

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, dir = :asc) ⇒ OrderBy

Returns a new instance of OrderBy.

Parameters:

  • field (Symbol)

    the field to order by

  • dir (Symbol) (defaults to: :asc)

    the order direction. Can be :asc or :desc. Default is :asc



10
11
12
13
14
15
# File 'lib/believer/order_by.rb', line 10

def initialize(field, dir = :asc)
  raise "Invalid field: #{field}" unless field.is_a?(Symbol) || field.is_a?(String)
  raise "Direction must be one of (:asc|:desc): #{dir}" unless dir == :asc || dir == :desc
  @field = field
  @dir = dir
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



6
7
8
# File 'lib/believer/order_by.rb', line 6

def dir
  @dir
end

#fieldObject (readonly)

Returns the value of attribute field.



6
7
8
# File 'lib/believer/order_by.rb', line 6

def field
  @field
end

Instance Method Details

#inverseObject

Inverts the direction of the order



23
24
25
# File 'lib/believer/order_by.rb', line 23

def inverse
  OrderBy.new(@field, @dir == :asc ? :desc : :asc)
end

#to_cqlObject

Creates the CQL ORDER BY clause



18
19
20
# File 'lib/believer/order_by.rb', line 18

def to_cql
  "ORDER BY #{@field} #{@dir.to_s.upcase}"
end