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


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

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:


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

def direction
  @direction
end

#fieldSymbol

Returns the name of the field

Returns:

  • (Symbol)

    the name of the field


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

def field
  @field
end

Instance Method Details

#polarityString

Returns the sort direction

Returns:

  • (String)

    the sort direction


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

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

#to_sString

Returns the ordering as a string

Returns:

  • (String)

    the ordering as a string


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

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