Class: JSONAPI::Support::Sort::Field
- Inherits:
-
Object
- Object
- JSONAPI::Support::Sort::Field
- Defined in:
- lib/json_api/support/sort/field.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #apply(scope) ⇒ Object
-
#initialize(model_class, sort_field) ⇒ Field
constructor
A new instance of Field.
- #virtual? ⇒ Boolean
Constructor Details
#initialize(model_class, sort_field) ⇒ Field
Returns a new instance of Field.
11 12 13 14 15 |
# File 'lib/json_api/support/sort/field.rb', line 11 def initialize(model_class, sort_field) @model_class = model_class @name = sort_field.delete_prefix("-") @direction = sort_field.start_with?("-") ? :desc : :asc end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
9 10 11 |
# File 'lib/json_api/support/sort/field.rb', line 9 def direction @direction end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/json_api/support/sort/field.rb', line 9 def name @name end |
Instance Method Details
#apply(scope) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/json_api/support/sort/field.rb', line 19 def apply(scope) raise Error, "cannot chain a virtual sort" if virtual? if column? scope.order(name => direction) else scope.public_send(name.to_sym, direction) end end |
#virtual? ⇒ Boolean
17 |
# File 'lib/json_api/support/sort/field.rb', line 17 def virtual? = !(column? || scope?) |