Sha256: 2b97287ef8313132a8ab796d919661bfede8a32bec4b704fd2119f3b624effbd

Contents?: true

Size: 1022 Bytes

Versions: 4

Compression:

Stored size: 1022 Bytes

Contents

# -*- coding: utf-8 -*-
=begin rdoc
Range extensions to parse text.
=end

class Range

  # Parse the text to a pair of tokens and return these as a new range object.
  #
  # The separator may be two dots or three dots:
  #
  #   * Two dots ".." includes the stop item.
  #   * Three dots "..." excludes the stop item.
  #
  # This method delegates parsing of each tokens to Range.parse_helper
  #
  def self.parse(text)
    begin
      text=text.to_s
      if text=~/(\.\.\.?)/
        start_token = $`
        stop_token = $'
        separator = $1
        exclude_end = (separator == "...")
        return self.new(self.parse_helper(start_token), self.parse_helper(stop_token), exclude_end)
      end
    rescue
    end
    raise ArgumentError.new("#parse text must have a start token, two or three dots, and a stop token; this text does not parse: \"#{text}\"")
  end
    
  # Parse one item of the pair of items.
  # Subclasses will likely want to override this.
  #
  def self.parse_helper(text)
    text
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sixarm_ruby_range_parse-2.0.0 lib/sixarm_ruby_range_parse/range.rb
sixarm_ruby_range_parse-1.0.5 lib/sixarm_ruby_range_parse/range.rb
sixarm_ruby_range_parse-1.0.4 lib/sixarm_ruby_range_parse/range.rb
sixarm_ruby_range_parse-1.0.3 lib/sixarm_ruby_range_parse/range.rb