Class: TTY::Prompt::Question::Checks::CheckRange

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/prompt/question/checks.rb

Overview

Check if value is within range

Class Method Summary collapse

Class Method Details

.call(question, value) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/tty/prompt/question/checks.rb', line 38

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) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/tty/prompt/question/checks.rb', line 28

def self.cast(value)
  if float?(value)
    value.to_f
  elsif int?(value)
    value.to_i
  else
    value
  end
end

.float?(value) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/tty/prompt/question/checks.rb', line 20

def self.float?(value)
  !/[-+]?(\d*[.])?\d+/.match(value.to_s).nil?
end

.int?(value) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/tty/prompt/question/checks.rb', line 24

def self.int?(value)
  !/^[-+]?\d+$/.match(value.to_s).nil?
end