Class: Parse::Order
- Inherits:
- Object
- Object
- Parse::Order
- 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'
Constant Summary collapse
- ORDERING =
The Parse operators to indicate ordering direction.
{ asc: "", desc: "-" }.freeze
Instance Attribute Summary collapse
- #direction ⇒ Symbol
The direction of the sorting.
- #field ⇒ Symbol
The name of the field.
Instance Method Summary collapse
- #initialize(field, order = :asc) ⇒ Order constructor
A new instance of Order.
- #polarity ⇒ String
The sort direction.
- #to_s ⇒ String
The ordering as a string.
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
#direction ⇒ Symbol
The direction of the sorting. This is either `:asc` or `:desc`.
30 31 32 | # File 'lib/parse/query/ordering.rb', line 30 def direction @direction end |
#field ⇒ Symbol
Returns the name of the field.
25 26 27 | # File 'lib/parse/query/ordering.rb', line 25 def field @field end |
Instance Method Details
#polarity ⇒ String
Returns the sort direction.
42 43 44 | # File 'lib/parse/query/ordering.rb', line 42 def polarity ORDERING[@direction] || ORDERING[:asc] end |
#to_s ⇒ String
Returns 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 |