Class: JSONAPIonify::Api::SortFieldSet
- Inherits:
-
Object
- Object
- JSONAPIonify::Api::SortFieldSet
- Includes:
- Enumerable
- Defined in:
- lib/jsonapionify/api/sort_field_set.rb
Instance Method Summary collapse
- #<<(field) ⇒ Object
-
#initialize ⇒ SortFieldSet
constructor
A new instance of SortFieldSet.
- #invert ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ SortFieldSet
Returns a new instance of SortFieldSet.
6 7 8 9 |
# File 'lib/jsonapionify/api/sort_field_set.rb', line 6 def initialize @list = [] freeze end |
Instance Method Details
#<<(field) ⇒ Object
30 31 32 33 |
# File 'lib/jsonapionify/api/sort_field_set.rb', line 30 def <<(field) raise TypeError unless field.is_a? SortField @list << field unless @list.include? field end |
#invert ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jsonapionify/api/sort_field_set.rb', line 15 def invert self.class.new.tap do |set| each do |field| name = case field.order when :asc "-#{field.name}" when :desc "#{field.name}" end set << SortField.new(name) end end end |
#to_h ⇒ Object
11 12 13 |
# File 'lib/jsonapionify/api/sort_field_set.rb', line 11 def to_h map(&:to_h).reduce(:merge) end |