Class: ApiMe::Sorting

Inherits:
Object
  • Object
show all
Defined in:
lib/api_me/sorting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, sort_params:) ⇒ Sorting

Returns a new instance of Sorting.



7
8
9
10
11
12
# File 'lib/api_me/sorting.rb', line 7

def initialize(scope:, sort_params:)
  self.scope = scope
  return unless sort_params
  self.sort_criteria = sort_params[:criteria] || default_sort_criteria
  self.sort_reverse = sort_params[:reverse]
end

Instance Attribute Details

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/api_me/sorting.rb', line 5

def scope
  @scope
end

#sort_criteriaObject

Returns the value of attribute sort_criteria.



5
6
7
# File 'lib/api_me/sorting.rb', line 5

def sort_criteria
  @sort_criteria
end

#sort_reverseObject

Returns the value of attribute sort_reverse.



5
6
7
# File 'lib/api_me/sorting.rb', line 5

def sort_reverse
  @sort_reverse
end

Instance Method Details

#resultsObject



14
15
16
# File 'lib/api_me/sorting.rb', line 14

def results
  sorting? ? sort(sort_criteria).scope : scope
end

#sort_metaObject



18
19
20
21
22
23
24
25
26
# File 'lib/api_me/sorting.rb', line 18

def sort_meta
  return {} unless sorting?
  {
    criteria: sort_meta_criteria,
    reverse: sort_reverse,
    record_count: scope.size,
    total_records: scope.total_count
  }
end