Module: MadFlatter::OptionsValidatable

Includes:
MetadataOptionable, NamespaceOptionable
Included in:
Optionable, Options
Defined in:
lib/mad_flatter/options_validatable.rb

Overview

Methods to validate options.

Constant Summary collapse

OPTIONS =
[OPTION_METADATA, OPTION_NAMESPACE].freeze

Constants included from NamespaceOptionable

NamespaceOptionable::OPTION_NAMESPACE, NamespaceOptionable::OPTION_NAMESPACE_DEFAULT

Constants included from MetadataOptionable

MetadataOptionable::OPTION_METADATA, MetadataOptionable::OPTION_METADATA_DEFAULT, MetadataOptionable::OPTION_METADATA_EXCLUDE, MetadataOptionable::OPTION_METADATA_INCLUDE, MetadataOptionable::OPTION_METADATA_VALUES

Instance Method Summary collapse

Instance Method Details

#validate_option_keys!(options:) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
# File 'lib/mad_flatter/options_validatable.rb', line 28

def validate_option_keys!(options:)
  invalid_options = options.except(*OPTIONS)&.keys

  return if invalid_options.blank?

  raise ArgumentError, 'One or more option keys were unrecognized. ' \
                       "#{OPTIONS} was expected but '#{invalid_options}' was received."
end

#validate_option_metadata!(metadata:) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
# File 'lib/mad_flatter/options_validatable.rb', line 37

def validate_option_metadata!(metadata:)
  return if [TrueClass, FalseClass].include? .class

  raise ArgumentError, "option :#{OPTION_METADATA} value is invalid. " \
                       'A TrueClass/FalseClass was expected ' \
                       "with the acceptable values #{OPTION_METADATA_VALUES} " \
                       "but '#{}' (#{.class}) was received."
end

#validate_option_namespace!(namespace:) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
# File 'lib/mad_flatter/options_validatable.rb', line 46

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

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

#validate_options!(options:) ⇒ Object



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

def validate_options!(options:)
  validate_options_type! options: options

  return if options.blank?

  validate_option_keys! options: options
  validate_option_metadata! metadata: options[:metadata]
  validate_option_namespace! namespace: options[:namespace]
end

#validate_options_type!(options:) ⇒ Object

Raises:

  • (ArgumentError)


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

def validate_options_type!(options:)
  raise ArgumentError, "options (#{options.class}) is not a Hash" unless options.is_a? Hash
end