Class: TTY2::Prompt::Question::Checks::CheckRange

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

Overview

Check if value is within range

Class Method Summary collapse

Class Method Details

.call(question, value) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/tty2/prompt/question/checks.rb', line 40

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



30
31
32
33
34
35
36
37
38
# File 'lib/tty2/prompt/question/checks.rb', line 30

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)


22
23
24
# File 'lib/tty2/prompt/question/checks.rb', line 22

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

.int?(value) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/tty2/prompt/question/checks.rb', line 26

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