Class: Parse::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/query/ordering.rb

Overview

This class adds support for describing ordering for Parse queries. You can either order by ascending (asc) or descending (desc) order.

Ordering is implemented similarly to constraints in which we add special methods to the Symbol class. The developer can then pass one or an array of fields (as symbols) and call the particular ordering polarity (ex. :name.asc would create a Parse::Order where we want things to be sortd by the name field in ascending order) For more information about the query design pattern from DataMapper that inspired this, see datamapper.org/docs/find.html'

Examples:

:name.asc # => Parse::Order by ascending :name
:like_count.desc # => Parse::Order by descending :like_count

Constant Summary collapse

ORDERING =

The Parse operators to indicate ordering direction.

{ asc: "", desc: "-" }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, order = :asc) ⇒ Order

Returns a new instance of Order.



32
33
34
35
# File 'lib/parse/query/ordering.rb', line 32

def initialize(field, order = :asc)
  @field = field.to_sym || :objectId
  @direction = order
end

Instance Attribute Details

#directionSymbol

The direction of the sorting. This is either `:asc` or `:desc`.

Returns:



30
31
32
# File 'lib/parse/query/ordering.rb', line 30

def direction
  @direction
end

#fieldSymbol

Returns the name of the field.

Returns:

  • (Symbol)

    the name of the field



25
26
27
# File 'lib/parse/query/ordering.rb', line 25

def field
  @field
end

Instance Method Details

#polarityString

Returns the sort direction.

Returns:

  • (String)

    the sort direction



42
43
44
# File 'lib/parse/query/ordering.rb', line 42

def polarity
  ORDERING[@direction] || ORDERING[:asc]
end

#to_sString

Returns the ordering as a string.

Returns:

  • (String)

    the ordering as a string



47
48
49
50
# File 'lib/parse/query/ordering.rb', line 47

def to_s
  "" if @field.nil?
  polarity + @field.to_s
end