class NaturalSort::SegmentedString

Constants

TOKENIZER

Attributes

input[R]

Public Class Methods

new(input) click to toggle source
# File lib/natural_sort/segmented_string.rb, line 10
def initialize(input)
  @input = input.to_s
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/natural_sort/segmented_string.rb, line 18
def <=>(other)
  raise ArgumentError unless SegmentedString === other

  other_segments = other.segments

  segments.each_with_index do |segment, index|
    other_segment = other_segments[index]
    return 1 if other_segment.nil?
    result = compare_segments(segment, other_segment)
    return result unless result.zero?
  end

  other_segments.length > segments.length ? -1 : 0
end
segments() click to toggle source
# File lib/natural_sort/segmented_string.rb, line 14
def segments
  @segments ||= tokens.map { |token| Segment.new(token) }
end

Private Instance Methods

compare_segments(segment, other) click to toggle source
# File lib/natural_sort/segmented_string.rb, line 39
def compare_segments(segment, other)
  segment <=> other
end
tokens() click to toggle source
# File lib/natural_sort/segmented_string.rb, line 35
def tokens
  @tokens ||= input.scan(TOKENIZER)
end