Sha256: 9cd44ed4cc2fb0da03225381c353d321877a7b915a5bd865aa6dea2718b1b6d2

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require_relative 'metadata_optionable'
require_relative 'namespace_optionable'

module MadFlatter
  # Methods to validate options.
  module OptionsValidatable
    include MadFlatter::MetadataOptionable
    include MadFlatter::NamespaceOptionable

    OPTIONS = [OPTION_METADATA, OPTION_NAMESPACE].freeze

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

      validate_options_present! options: options

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

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

    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

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

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

    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 ' \
        "with the acceptable values #{OPTION_METADATA_VALUES} " \
        "but '#{metadata}' (#{metadata.class}) was received."
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mad_flatter-1.0.1.pre.beta lib/mad_flatter/options_validatable.rb
mad_flatter-1.0.0.pre.beta lib/mad_flatter/options_validatable.rb