Class: TTY::Prompt::Question::Checks::CheckRange
- Inherits:
-
Object
- Object
- TTY::Prompt::Question::Checks::CheckRange
- Defined in:
- lib/tty/prompt/question/checks.rb
Overview
Check if value is within range
Class Method Summary collapse
- .call(question, value) ⇒ Object
- .cast(value) ⇒ Object
- .float?(value) ⇒ Boolean
- .int?(value) ⇒ Boolean
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.(: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
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
24 25 26 |
# File 'lib/tty/prompt/question/checks.rb', line 24 def self.int?(value) !/^[-+]?\d+$/.match(value.to_s).nil? end |