Module: Lemans::Results::Sorting

Defined in:
lib/lemans/results/sorting.rb

Overview

One sorting rule for every results view: validate the column name and keep rows that never measured the value at the bottom.

Class Method Summary collapse

Class Method Details

.call(rows, descending: false) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/lemans/results/sorting.rb', line 15

def self.call(rows, descending: false)
  keyed = rows.map { [yield(_1), _1] }
  present, missing = keyed.partition { |value, _| value }
  sorted = present.sort_by { |value, _| value }
  sorted.reverse! if descending
  (sorted + missing).map(&:last)
end

.column(name, allowed:) ⇒ Object

Raises:



8
9
10
11
12
13
# File 'lib/lemans/results/sorting.rb', line 8

def self.column(name, allowed:)
  column = name.to_s.to_sym
  return column if allowed.include?(column)

  raise ConfigError, "--sort: unknown column #{name.inspect} (try #{allowed.join(", ")})"
end