Module: Atacama::Types

Defined in:
lib/atacama/types.rb

Overview

The type namespace to interact with DRY::Types

Constant Summary collapse

Boolean =
Types::True | Types::False

Class Method Summary collapse

Class Method Details

.Option(**map) ⇒ Dry::Type

Defines a type which checks that the Option value contains a valid data structure.

Parameters:

  • map (Hash)

    schema definition of the option value

Returns:

  • (Dry::Type)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/atacama/types.rb', line 16

def self.Option(**map)
  Instance(Values::Option).constructor do |value_object|
    if value_object.is_a? Values::Option
      map.each do |key, type|
        value = value_object.value[key]
        Atacama.check(type, value) do |e|
          raise OptionTypeMismatchError, Atacama.format_exception(self, e,
            "The Option() #{key.inspect} value #{value.inspect} is the incorrect type."
          )
        end
      end
    end

    value_object
  end
end

.Return(type) ⇒ Dry::Type

Defines a type which checks that the Return value contains a valid object

Parameters:

  • type (Dry::Type)

Returns:

  • (Dry::Type)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/atacama/types.rb', line 39

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, Atacama.format_exception(self, e,
          "The Return() value #{value_object.value.inspect} does not match the declared type."
        )
      end
    end

    value_object
  end
end