Module: ProductTaxonomy::AlphanumericSorter

Defined in:
lib/product_taxonomy/alphanumeric_sorter.rb

Defined Under Namespace

Modules: RegexPattern

Class Method Summary collapse

Class Method Details

.normalize_value(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/product_taxonomy/alphanumeric_sorter.rb', line 16

def normalize_value(value)
  @normalized_values ||= {}
  @normalized_values[value] ||= if (numerical = value.match(RegexPattern::NUMERIC_PATTERN))
    [0, *normalize_numerical(numerical)]
  elsif (sequential = value.match(RegexPattern::SEQUENTIAL_TEXT_PATTERN))
    [1, *normalize_sequential(sequential)]
  else
    [1, normalize_text(value)]
  end
end

.sort(values, other_last: false) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/product_taxonomy/alphanumeric_sorter.rb', line 6

def sort(values, other_last: false)
  values.sort_by.with_index do |value, idx|
    [
      other_last && value.to_s.downcase == "other" ? 1 : 0,
      *normalize_value(value),
      idx,
    ]
  end
end