Class: Optionable::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/optionable/validator.rb

Overview

This is responsible for the actual validation of the options.

Since:

  • 0.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Validator

Initialize the new validator.

Examples:

Initialize the validator.

Optionable::Validator.new(:test)

Parameters:

  • key (Symbol)

    The name of the option.

Since:

  • 0.0.0



35
36
37
# File 'lib/optionable/validator.rb', line 35

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject

Since:

  • 0.0.0



11
12
13
# File 'lib/optionable/validator.rb', line 11

def key
  @key
end

Instance Method Details

#allow(*values) ⇒ Array<Object>

Tells the validator what values are acceptable for the option.

Examples:

Specify allowed values.

validator.allow(:test, :testing)

Parameters:

  • values (Array<Object>)

    The allowed values.

Returns:

  • (Array<Object>)

    The full list of allowed values.

Since:

  • 0.0.0



23
24
25
# File 'lib/optionable/validator.rb', line 23

def allow(*values)
  allowed_values.concat(values)
end

#validate!(value) ⇒ Object

Validate the provided value against the acceptable values.

Examples:

Validate the provided value.

validator.validate!("test")

Parameters:

  • value (Object)

    The value for the option.

Raises:

Since:

  • 0.0.0



49
50
51
# File 'lib/optionable/validator.rb', line 49

def validate!(value)
  raise Invalid.new(key, value, allowed_values) unless match?(value)
end