Sha256: be26cbd5ed6a364eb5c7f50cff663c81966529b639272790508a5984204be7eb

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Atacama
  # The type namespace to interact with DRY::Types
  module Types
    include Dry::Types.module
    Boolean = Types::True | Types::False

    # Defines a type which checks that the Option value contains a valid
    # data structure.
    #
    # @param map [Hash] schema definition of the option value
    #
    # @return [Dry::Type]
    def self.Option(**map)
      Instance(Values::Option).constructor do |value_object|
        if value_object.is_a? Values::Option
          map.each do |key, type|
            Atacama.check(type, value_object.value[key]) do |e|
              raise OptionTypeMismatchError, "Invalid Option value type: #{e.message}"
            end
          end
        end

        value_object
      end
    end

    # Defines a type which checks that the Return value contains a valid
    # object
    #
    # @param type [Dry::Type]
    #
    # @return [Dry::Type]
    def self.Return(type)
      Instance(Values::Return).constructor do |value_object|
        if value_object.is_a?(Values::Return)
          Atacama.check(type, value_object.value) do |e|
            raise ReturnTypeMismatchError, "Invalid Return Value type: #{e.message}"
          end
        end

        value_object
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
atacama-0.1.8 lib/atacama/types.rb
atacama-0.1.7 lib/atacama/types.rb