Sha256: 23ee7ab1e4b8a66fe8ee83bb470835da23b3d5dfaf59d0f9d5712e8e61419ff7

Contents?: true

Size: 770 Bytes

Versions: 1

Compression:

Stored size: 770 Bytes

Contents

module SequelMapper
  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

1 entries across 1 versions & 1 rubygems

Version Path
sequel_mapper-0.0.3 lib/sequel_mapper/query_order.rb