Sha256: 4c4d54ff47890f2c3209fbf482f57988f22c4065ed967ab7c1a5f4c6b68797c5

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

#
# Abstraction over WebMock to reduce duplication
#
# @author Mikael Henriksson <mikael@zoolutions.se>
# @since 0.1.0
#
module StubRequests
  module Concerns
    #
    # Module ArgumentValidation provides validation of method arguments
    #
    # @author Mikael Henriksson <mikael@zoolutions.se>
    #
    module ArgumentValidation
      extend self

      #
      # Require the value to be any of the types past in
      #
      #
      # @param [Symbol] name the name of the argument
      # @param [Object] value the actual value of the argument
      # @param [Array, Class, Module] type nil the expected argument value class
      #
      # @raise [InvalidArgumentType] when the value is disallowed
      #
      # @return [void]
      #
      def validate!(name:, value:, type:)
        validate_type!(:name, name, [Symbol, String]) unless name
        validate_type!(name, value, type) if type
      end

      def validate_type!(name, value, type)
        expected_types = Array(type).flatten
        return if expected_types.any? { |is_a| value.is_a?(is_a) }

        raise StubRequests::InvalidArgumentType,
              name: name,
              actual: value.class,
              expected: expected_types
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stub_requests-0.1.11 lib/stub_requests/concerns/argument_validation.rb
stub_requests-0.1.10 lib/stub_requests/concerns/argument_validation.rb