Class: TTY::Shell::Question::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/shell/question/validation.rb

Overview

A class representing question validation.

Instance Method Summary collapse

Constructor Details

#initialize(validation = nil) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a Validation

Parameters:

  • validation (Object) (defaults to: nil)


19
20
21
# File 'lib/tty/shell/question/validation.rb', line 19

def initialize(validation = nil)
  @validation = validation ? coerce(validation) : validation
end

Instance Method Details

#coerce(validation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert validation into known type.

Parameters:

  • validation (Object)

Raises:



30
31
32
33
34
35
36
37
38
39
# File 'lib/tty/shell/question/validation.rb', line 30

def coerce(validation)
  case validation
  when Proc
    validation
  when Regexp, String
    Regexp.new(validation.to_s)
  else
    fail TTY::ValidationCoercion, "Wrong type, got #{validation.class}"
  end
end

#valid_value?(value) ⇒ undefined

Test if the value matches the validation

Examples:

validation.valid_value?(value) # => true or false

Parameters:

  • value (Object)

    the value to validate

Returns:

  • (undefined)


61
62
63
# File 'lib/tty/shell/question/validation.rb', line 61

def valid_value?(value)
  check_validation(value)
end

#validate?Boolean

Check if validation is required

Returns:

  • (Boolean)


46
47
48
# File 'lib/tty/shell/question/validation.rb', line 46

def validate?
  !!validation
end