class SmartSortAtom

Attributes

value[R]

Public Class Methods

new(value) click to toggle source
# File lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb, line 5
def initialize(value)
  @value = value
end
parse(string) click to toggle source
# File lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb, line 22
def self.parse(string)
  # Loosely based on http://stackoverflow.com/a/4079031
  string.scan(/[^\d\.]+|[\d\.]+/).collect do |atom|
    if atom.match(/\d+(\.\d+)?/)
      atom = atom.to_f
    else
      atom = normalize_string(atom)
    end
    new(atom)
  end

end

Private Class Methods

normalize_string(string) click to toggle source
# File lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb, line 37
def self.normalize_string(string)
  string = ActiveSupport::Inflector.transliterate(string)
  string = string.downcase
  string
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb, line 9
def <=>(other)
  other.is_a?(self.class) or raise "Can only smart compare with other SmartSortAtom"
  left_value = value
  right_value = other.value
  if left_value.class == right_value.class
    left_value <=> right_value
  elsif left_value.is_a?(Float)
    -1
  else
    1
  end
end