Class: Toller::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/toller/sort.rb

Overview

Sort

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameter, type, options) ⇒ Sort

Returns a new instance of Sort.



10
11
12
13
14
15
16
17
18
# File 'lib/toller/sort.rb', line 10

def initialize(parameter, type, options)
  @parameter = parameter
  @type = type
  @properties = options.reverse_merge(
    field: parameter,
    default: false,
    scope_name: nil
  )
end

Instance Attribute Details

#parameterObject (readonly)

Returns the value of attribute parameter.



8
9
10
# File 'lib/toller/sort.rb', line 8

def parameter
  @parameter
end

#propertiesObject (readonly)

Returns the value of attribute properties.



8
9
10
# File 'lib/toller/sort.rb', line 8

def properties
  @properties
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/toller/sort.rb', line 8

def type
  @type
end

Instance Method Details

#apply!(collection, direction = :asc) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/toller/sort.rb', line 20

def apply!(collection, direction = :asc)
  if type == :scope
    Sorts::ScopeHandler.new.call(collection, direction, properties)
  else
    Sorts::OrderHandler.new.call(collection, direction, properties)
  end
end

#defaultObject



28
29
30
# File 'lib/toller/sort.rb', line 28

def default
  properties[:default]
end