Class: Parse::Order
- Inherits:
-
Object
- Object
- Parse::Order
- Defined in:
- lib/parse/query/ordering.rb,
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 sorted by the name field in ascending order) For more information about the query design pattern from DataMapper that inspired this, see http://datamapper.org/docs/find.html'
Defined Under Namespace
Modules: SymbolMethods
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.
Class Method Summary collapse
-
.from_foreign(value) ⇒ Parse::Order?
Converts another library's sort key (Mongoid's
:field.descwhen Mongoid ownsSymbol#desc) into a Order.
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 |
Class Method Details
.from_foreign(value) ⇒ Parse::Order?
Converts another library's sort key (Mongoid's :field.desc when
Mongoid owns Symbol#desc) into a Parse::Order.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/parse/query/ordering.rb', line 72 def self.from_foreign(value) return value if value.is_a?(Parse::Order) if defined?(::Mongoid::Criteria::Queryable::Key) && value.is_a?(::Mongoid::Criteria::Queryable::Key) case value.operator when 1, "1" then return new(value.name, :asc) when -1, "-1" then return new(value.name, :desc) end end nil 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 |