Module: Optionable

Defined in:
lib/optionable.rb,
lib/optionable/any.rb,
lib/optionable/dsl.rb,
lib/optionable/invalid.rb,
lib/optionable/unknown.rb,
lib/optionable/version.rb,
lib/optionable/validator.rb

Overview

This module is to be included in objects that require some nifty options validation.

Since:

  • 0.0.0

Defined Under Namespace

Modules: DSL Classes: Any, Invalid, Unknown, Validator

Constant Summary collapse

VERSION =

Since:

  • 0.0.0

"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.any(type) ⇒ Object

Provides a convenience method for creating a new option acceptance for values of a certain type.

Examples:

Create the any type.

Optionable.any(Integer)

Parameters:

  • type (Class)

    The type of supported value.

Since:

  • 0.0.0



59
60
61
# File 'lib/optionable.rb', line 59

def any(type)
  Any.new(type)
end

Instance Method Details

#validate_strict(options) ⇒ Object

Validate the provided options against the defined acceptable values.

Examples:

Validate the options.

optionable.validate_strict(read: "test")

Parameters:

  • options (Hash)

    The options to validate.

Raises:

Since:

  • 0.0.0



26
27
28
29
30
31
32
33
34
35
# File 'lib/optionable.rb', line 26

def validate_strict(options)
  (options || {}).each_pair do |key, value|
    validator = optionable_validators[key]
    if validator
      validator.validate!(value)
    else
      raise Unknown.new(key, optionable_validators.keys)
    end
  end
end