class TTY::Prompt::Question::Checks::CheckRange

Check if value is within range

Public Class Methods

call(question, value) click to toggle source
# File lib/tty/prompt/question/checks.rb, line 39
def self.call(question, value)
  if !question.in? ||
    (question.in? && question.in.include?(cast(value)))
    [value]
  else
    tokens = { value: value, in: question.in }
    [value, question.message_for(:range?, tokens)]
  end
end
cast(value) click to toggle source
# File lib/tty/prompt/question/checks.rb, line 29
def self.cast(value)
  if float?(value)
    value.to_f
  elsif int?(value)
    value.to_i
  else
    value
  end
end
float?(value) click to toggle source
# File lib/tty/prompt/question/checks.rb, line 21
def self.float?(value)
  !/[-+]?(\d*[.])?\d+/.match(value.to_s).nil?
end
int?(value) click to toggle source
# File lib/tty/prompt/question/checks.rb, line 25
def self.int?(value)
  !/^[-+]?\d+$/.match(value.to_s).nil?
end