Sha256: 2244178f7aeae60c2414b4b7a01c8946a82c374b435d79ff75e62e62fdeba46e

Contents?: true

Size: 958 Bytes

Versions: 1

Compression:

Stored size: 958 Bytes

Contents

# encoding: utf-8

module TTY
  module Conversion
    class RangeConverter

      attr_reader :target

      attr_reader :source

      def initialize(source = String)
        @source = source
        @target = Range
      end

      # Convert value to Range type with possible ranges
      #
      # @param [Object] value
      #
      # @example
      #   coerce('0,9')  # => (0..9)
      #
      # @example
      #   coerce('0-9')  # => (0..9)
      #
      # @api public
      def convert(value)
        case value.to_s
        when /\A(\-?\d+)\Z/
          ::Range.new($1.to_i, $1.to_i)
        when /\A(-?\d+?)(\.{2}\.?|-|,)(-?\d+)\Z/
          ::Range.new($1.to_i, $3.to_i, $2 == '...')
        when /\A(\w)(\.{2}\.?|-|,)(\w)\Z/
          ::Range.new($1.to_s, $3.to_s, $2 == '...')
        else
          fail InvalidArgument, "#{value} could not be coerced into Range type"
        end
      end
    end # RangeConverter
  end # Conversion
end # TTY

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tty-0.1.1 lib/tty/conversion/converter/range.rb