Class: GearedPagination::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/geared_pagination/order.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, direction: :asc) ⇒ Order

Returns a new instance of Order.



26
27
28
29
30
# File 'lib/geared_pagination/order.rb', line 26

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

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



5
6
7
# File 'lib/geared_pagination/order.rb', line 5

def attribute
  @attribute
end

#directionObject (readonly)

Returns the value of attribute direction.



5
6
7
# File 'lib/geared_pagination/order.rb', line 5

def direction
  @direction
end

Class Method Details

.wrap(object) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/geared_pagination/order.rb', line 12

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

.wrap_many(objects) ⇒ Object



8
9
10
# File 'lib/geared_pagination/order.rb', line 8

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

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/geared_pagination/order.rb', line 40

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

#asc?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/geared_pagination/order.rb', line 32

def asc?
  direction == :asc
end

#desc?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/geared_pagination/order.rb', line 36

def desc?
  !asc?
end