Sha256: 7f8a23f536652abc19f47cfb69b607efbb830559e991ae623a5ec14099b575c4
Contents?: true
Size: 1.96 KB
Versions: 1
Compression:
Stored size: 1.96 KB
Contents
require 'dry/types/decorator' module Dry module Types class Enum include Type include Dry::Equalizer(:type, :options, :mapping, inspect: false) include Decorator # @return [Array] attr_reader :values # @return [Hash] attr_reader :mapping # @return [Hash] attr_reader :inverted_mapping # @param [Type] type # @param [Hash] options # @option options [Array] :values def initialize(type, options) super @mapping = options.fetch(:mapping).freeze @values = @mapping.keys.freeze @inverted_mapping = @mapping.invert.freeze freeze end # @param [Object] input # @return [Object] def call(input = Undefined) type[map_value(input)] end alias_method :[], :call # @param [Object] input # @yieldparam [Failure] failure # @yieldreturn [Result] # @return [Logic::Result] # @return [Object] if coercion fails and a block is given def try(input) super(map_value(input)) end def default(*) raise '.enum(*values).default(value) is not supported. Call '\ '.default(value).enum(*values) instead' end # Check whether a value is in the enum alias_method :include?, :valid? # @api public # # @see Nominal#to_ast def to_ast(meta: true) [:enum, [type.to_ast(meta: meta), mapping, meta ? self.meta : EMPTY_HASH]] end # @return [String] # @api public def to_s PRINTER.(self) end alias_method :inspect, :to_s private # Maps a value # # @param [Object] # @return [Object] # @api private def map_value(input) if input.equal?(Undefined) type.call elsif mapping.key?(input) input else inverted_mapping.fetch(input, input) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-types-0.15.0 | lib/dry/types/enum.rb |