Sha256: fc61cc9ef92affbca95a066165885da8c5cf3ac9cc4f8f665f9193ce4901dbf9

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

module GearedPagination
  class Order
    INVALID_DIRECTION = "Invalid direction for order on %p: expected :asc or :desc, got %p"

    attr_reader :attribute, :direction

    class << self
      def wrap_many(objects)
        Array.wrap(objects).flat_map { |object| wrap object }
      end

      def wrap(object)
        case object
        when Symbol
          new attribute: object
        when Hash
          object.collect { |key, value| new attribute: key, direction: value }
        when self
          object
        else
          raise ArgumentError, "Invalid orders: expected Symbol, Hash, or GearedPagination::Order, got #{object.inspect}"
        end
      end
    end

    def initialize(attribute:, direction: :asc)
      @attribute = attribute.to_sym
      @direction = direction.presence_in(%i[ asc desc ]) ||
        raise(ArgumentError, INVALID_DIRECTION % [ attribute, direction ])
    end

    def asc?
      direction == :asc
    end

    def desc?
      !asc?
    end

    def ==(other)
      other.is_a?(Order) && attribute == other.attribute && direction == other.direction
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
geared_pagination-1.2.0 lib/geared_pagination/order.rb
geared_pagination-1.1.2 lib/geared_pagination/order.rb
geared_pagination-1.1.1 lib/geared_pagination/order.rb
geared_pagination-1.1.0 lib/geared_pagination/order.rb
geared_pagination-1.0.2 lib/geared_pagination/order.rb
geared_pagination-1.0.1 lib/geared_pagination/order.rb
geared_pagination-1.0.0 lib/geared_pagination/order.rb