Module: DecoLite::OptionsValidatable

Includes:
FieldsOptionable, NamespaceOptionable
Included in:
Optionable, Options
Defined in:
lib/deco_lite/options_validatable.rb

Overview

Methods to validate options.

Constant Summary collapse

OPTIONS =
[OPTION_FIELDS, OPTION_NAMESPACE].freeze

Constants included from NamespaceOptionable

NamespaceOptionable::OPTION_NAMESPACE, NamespaceOptionable::OPTION_NAMESPACE_DEFAULT

Constants included from FieldsOptionable

FieldsOptionable::OPTION_FIELDS, FieldsOptionable::OPTION_FIELDS_DEFAULT, FieldsOptionable::OPTION_FIELDS_MERGE, FieldsOptionable::OPTION_FIELDS_STRICT, FieldsOptionable::OPTION_FIELDS_VALUES

Instance Method Summary collapse

Instance Method Details

#validate_option_fields!(fields:) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
# File 'lib/deco_lite/options_validatable.rb', line 33

def validate_option_fields!(fields:)
  return if OPTION_FIELDS_VALUES.include?(fields)

  raise ArgumentError,
    "option :fields value or type is invalid. #{OPTION_FIELDS_VALUES} (Symbol) " \
    "was expected, but '#{fields}' (#{fields.class}) was received."
end

#validate_option_keys!(options:) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
# File 'lib/deco_lite/options_validatable.rb', line 28

def validate_option_keys!(options:)
  invalid_options = options.except(*OPTIONS)&.keys
  raise ArgumentError, "One or more option keys were unrecognized: #{invalid_options}" unless invalid_options.blank?
end

#validate_option_namespace!(namespace:) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
# File 'lib/deco_lite/options_validatable.rb', line 41

def validate_option_namespace!(namespace:)
  # :namespace is optional.
  return if namespace.blank? || namespace.is_a?(Symbol)

  raise ArgumentError, 'option :namespace value or type is invalid. A Symbol was expected, ' \
                       "but '#{namespace}' (#{namespace.class}) was received."
end

#validate_options!(options:) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/deco_lite/options_validatable.rb', line 14

def validate_options!(options:)
  raise ArgumentError, 'options is not a Hash' unless options.is_a? Hash

  validate_options_present!(options:)

  validate_option_keys!(options:)
  validate_option_fields! fields: options[OPTION_FIELDS]
  validate_option_namespace! namespace: options[OPTION_NAMESPACE]
end

#validate_options_present!(options:) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
# File 'lib/deco_lite/options_validatable.rb', line 24

def validate_options_present!(options:)
  raise ArgumentError, 'options is blank?' if options.blank?
end