class SequelMapper::QueryOrder

Attributes

direction_function[R]
fields[R]

Public Class Methods

new(fields:, direction:) click to toggle source
# File lib/sequel_mapper/query_order.rb, line 3
def initialize(fields:, direction:)
  @fields = fields
  @direction_function = get_direction_function(direction.to_s.upcase)
end

Public Instance Methods

apply(dataset) click to toggle source
# File lib/sequel_mapper/query_order.rb, line 10
def apply(dataset)
  if fields.any?
    apply_direction(dataset.order(fields))
  else
    dataset
  end
end

Private Instance Methods

apply_direction(dataset) click to toggle source
# File lib/sequel_mapper/query_order.rb, line 20
def apply_direction(dataset)
  direction_function.call(dataset)
end
get_direction_function(direction) click to toggle source

TODO: Consider a nicer API for this and push this into SequelAdapter

# File lib/sequel_mapper/query_order.rb, line 25
def get_direction_function(direction)
  {
    "ASC" => ->(x){x},
    "DESC" => :reverse.to_proc,
  }.fetch(direction) { raise "Unsupported sort option #{direction}. Choose one of [ASC, DESC]." }
end