Class: TypicalSort::Sorter

Inherits:
Object
  • Object
show all
Defined in:
lib/typical_sort/sorter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records:, definition:, direction:, configuration:) ⇒ Sorter

Returns a new instance of Sorter.



10
11
12
13
14
15
# File 'lib/typical_sort/sorter.rb', line 10

def initialize(records:, definition:, direction:, configuration:)
  @records = records
  @definition = definition
  @direction = direction.to_sym
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/typical_sort/sorter.rb', line 8

def configuration
  @configuration
end

#definitionObject (readonly)

Returns the value of attribute definition.



8
9
10
# File 'lib/typical_sort/sorter.rb', line 8

def definition
  @definition
end

#directionObject (readonly)

Returns the value of attribute direction.



8
9
10
# File 'lib/typical_sort/sorter.rb', line 8

def direction
  @direction
end

#recordsObject (readonly)

Returns the value of attribute records.



8
9
10
# File 'lib/typical_sort/sorter.rb', line 8

def records
  @records
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/typical_sort/sorter.rb', line 17

def call
  return records.public_send(definition.name, direction) if definition.scope?

  resolver = PathResolver.new(records: records, path: definition.key)
  validate_resolver!(resolver)

  if resolver.collection_association? || definition.aggregate?
    AggregateSorter.new(
      records: records,
      relation_name: resolver.association_name,
      reflection: resolver.reflection,
      column_name: resolver.column_name,
      direction: direction,
      definition: definition,
      configuration: configuration
    ).call
  elsif resolver.relation_association?
    sort_relation_association(resolver)
  else
    sort_base_column(resolver.column_name)
  end
end