class Dry::Types::Enum
def call(input = Undefined)
-
(Object)
-
Parameters:
-
input
(Object
) --
def call(input = Undefined) type[map_value(input)] end
def default(*)
def default(*) raise '.enum(*values).default(value) is not supported. Call '\ '.default(value).enum(*values) instead' end
def initialize(type, options)
(**options)
-
:values
(Array
) --
Parameters:
-
options
(Hash
) -- -
type
(Type
) --
def initialize(type, options) super @mapping = options.fetch(:mapping).freeze @values = @mapping.keys.freeze @inverted_mapping = @mapping.invert.freeze freeze end
def map_value(input)
- Api: - private
Returns:
-
(Object)
-
def map_value(input) if input.equal?(Undefined) type.call elsif mapping.key?(input) input else inverted_mapping.fetch(input, input) end end
def to_ast(meta: true)
- See: Definition#to_ast -
Other tags:
- Api: - public
def to_ast(meta: true) [:enum, [type.to_ast(meta: meta), mapping, meta ? self.meta : EMPTY_HASH]] end
def try(input)
-
(Object)
- if coercion fails and a block is given -
(Logic::Result)
-
Other tags:
- Yieldreturn: -
Other tags:
- Yieldparam: failure -
Parameters:
-
input
(Object
) --
def try(input) super(map_value(input)) end