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)


21
22
23
# File 'lib/tty/shell/question/validation.rb', line 21

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:



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

def coerce(validation)
  case validation
  when Proc
    validation
  when Regexp, String
    Regexp.new(validation.to_s)
  else
    raise 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)


63
64
65
# File 'lib/tty/shell/question/validation.rb', line 63

def valid_value?(value)
  check_validation(value)
end

#validate?Boolean

Check if validation is required

Returns:

  • (Boolean)


48
49
50
# File 'lib/tty/shell/question/validation.rb', line 48

def validate?
  !!validation
end