Sha256: 6bd7f49b1455ecb1a51998ecad3aed4024d8fe4dc0c27e7f9f5a75f05737717a

Contents?: true

Size: 769 Bytes

Versions: 3

Compression:

Stored size: 769 Bytes

Contents

module Terrestrial
  class QueryOrder
    def initialize(fields:, direction:)
      @fields = fields
      @direction_function = get_direction_function(direction.to_s.upcase)
    end

    attr_reader :fields, :direction_function

    def apply(dataset)
      if fields.any?
        apply_direction(dataset.order(fields))
      else
        dataset
      end
    end

    private

    def apply_direction(dataset)
      direction_function.call(dataset)
    end

    # TODO: Consider a nicer API for this and push this into SequelAdapter
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
terrestrial-0.5.0 lib/terrestrial/query_order.rb
terrestrial-0.3.0 lib/terrestrial/query_order.rb
terrestrial-0.1.1 lib/terrestrial/query_order.rb