Class: Dry::Types::Enum

Inherits:
Object
  • Object
show all
Includes:
Builder, Decorator, Type
Defined in:
lib/dry/types/enum.rb

Overview

Enum types can be used to define an enum on top of an existing type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, **options) ⇒ Enum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Enum.

Parameters:

Options Hash (**options):



28
29
30
31
32
33
34
# File 'lib/dry/types/enum.rb', line 28

def initialize(type, **options)
  super
  @mapping = options.fetch(:mapping).freeze
  @values = @mapping.keys.freeze
  @inverted_mapping = @mapping.invert.freeze
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator

Instance Attribute Details

#inverted_mappingHash (readonly)

Returns:



21
22
23
# File 'lib/dry/types/enum.rb', line 21

def inverted_mapping
  @inverted_mapping
end

#mappingHash (readonly)

Returns:



18
19
20
# File 'lib/dry/types/enum.rb', line 18

def mapping
  @mapping
end

#optionsHash (readonly) Originally defined in module Options

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

#typeType (readonly) Originally defined in module Decorator

Returns:

#valuesArray (readonly)

Returns:



15
16
17
# File 'lib/dry/types/enum.rb', line 15

def values
  @values
end

Instance Method Details

#&(other) ⇒ Intersection, Intersection::Constrained Originally defined in module Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compose two types into an Intersection type

Parameters:

Returns:

#>(other) ⇒ Implication, Implication::Constrained Originally defined in module Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compose two types into an Implication type

Parameters:

Returns:

#call(input = Undefined) ⇒ Object #call(input = Undefined) {|output| ... } ⇒ Object Also known as: [] Originally defined in module Type

Apply type to a value

Overloads:

  • #call(input = Undefined) ⇒ Object

    Possibly unsafe coercion attempt. If a value doesn't match the type, an exception will be raised.

    Parameters:

    • input (Object) (defaults to: Undefined)

    Returns:

    • (Object)
  • #call(input = Undefined) {|output| ... } ⇒ Object

    When a block is passed, #call will never throw an exception on failed coercion, instead it will call the block.

    Parameters:

    • input (Object) (defaults to: Undefined)

    Yield Parameters:

    • output (Object)

      Partially coerced value

    Returns:

    • (Object)

#call_safe(input, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Object)


46
47
48
# File 'lib/dry/types/enum.rb', line 46

def call_safe(input, &block)
  type.call_safe(map_value(input), &block)
end

#call_unsafe(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Object)


39
40
41
# File 'lib/dry/types/enum.rb', line 39

def call_unsafe(input)
  type.call_unsafe(map_value(input))
end

#constrained(options) ⇒ Constrained Originally defined in module Builder

Turn a type into a constrained type

Parameters:

Returns:

#constrained?Boolean Originally defined in module Decorator

Returns:

  • (Boolean)

#constrained_typeClass Originally defined in module Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Class)

#constructor(constructor = nil, **options, &block) ⇒ Constructor Also known as: append, prepend, >>, << Originally defined in module Builder

Define a constructor for the type

Parameters:

  • constructor (#call, nil) (defaults to: nil)
  • options (Hash)
  • block (#call, nil)

Returns:

#constructor_typeClass Originally defined in module Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Class)

#defaultObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
# File 'lib/dry/types/enum.rb', line 58

def default(*)
  raise ".enum(*values).default(value) is not supported. Call "\
        ".default(value).enum(*values) instead"
end

#default?Boolean Originally defined in module Decorator

Returns:

  • (Boolean)

#enum(*values) ⇒ Enum Originally defined in module Builder

Define an enum on top of the existing type

Parameters:

Returns:

#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder

Use the given value on type mismatch

Parameters:

  • value (Object) (defaults to: Undefined)
  • fallback (#call, nil)
  • [Boolean] (Hash)

    a customizable set of options

Returns:

#laxLax Originally defined in module Builder

Turn a type into a lax type that will rescue from type-errors and return the original input

Returns:

#maybeMaybe Originally defined in module Builder

Turn a type into a maybe type

Returns:

#optionalSum Originally defined in module Builder

Turn a type into an optional type

Returns:

#respond_to_missing?(meth, include_private = false) ⇒ Boolean Originally defined in module Decorator

Parameters:

  • meth (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)

#to_ast(meta: true) ⇒ Object

See Also:



69
70
71
# File 'lib/dry/types/enum.rb', line 69

def to_ast(meta: true)
  [:enum, [type.to_ast(meta: meta), mapping]]
end

#to_procProc Originally defined in module Decorator

Wrap the type with a proc

Returns:

  • (Proc)

#to_sString Also known as: inspect

Returns:

  • (String)


76
77
78
# File 'lib/dry/types/enum.rb', line 76

def to_s
  PRINTER.(self)
end

#try(input) ⇒ Object

See Also:



53
54
55
# File 'lib/dry/types/enum.rb', line 53

def try(input)
  super(map_value(input))
end

#valid?(input = Undefined) ⇒ Boolean Also known as: === Originally defined in module Type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether a value is a valid member of the type

Returns:

  • (Boolean)

#with(**new_options) ⇒ Type Originally defined in module Options

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • new_options (Hash)

Returns:

#|(other) ⇒ Sum, Sum::Constrained Originally defined in module Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compose two types into a Sum type

Parameters:

Returns: