aglushkov/serega

View on GitHub
lib/serega/validations/utils/check_opt_is_string_or_symbol.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

class Serega
  module SeregaValidations
    module Utils
      #
      # Utility to check hash key value is String or Symbol
      #
      class CheckOptIsStringOrSymbol
        # Checks hash key has String or Symbol value
        #
        # @param opts [Hash] Some options Hash
        # @param key [Object] Hash key
        #
        # @raise [Serega::SeregaError] error when provided key exists and value is not String or Symbol
        #
        # @return [void]
        def self.call(opts, key)
          return unless opts.key?(key)

          value = opts[key]
          return if value.is_a?(String) || value.is_a?(Symbol)

          raise SeregaError, "Invalid option #{key.inspect} => #{value.inspect}. Must be a String or a Symbol"
        end
      end
    end
  end
end