Sha256: 581b838d2fdebeecc4c67a414dfa04027addb8dcd6aec633650dd151fb2f2999
Contents?: true
Size: 1.04 KB
Versions: 1
Compression:
Stored size: 1.04 KB
Contents
require 'dry/types/decorator' module Dry module Types class Enum include Type include Dry::Equalizer(:type, :options, :values) include Decorator # @return [Array] attr_reader :values # @return [Hash] attr_reader :mapping # @param [Type] type # @param [Hash] options # @option options [Array] :values def initialize(type, options) super @values = options.fetch(:values).freeze @values.each(&:freeze) @mapping = values.each_with_object({}) { |v, h| h[values.index(v)] = v }.freeze end # @param [Object] input # @return [Object] def call(input) value = if values.include?(input) input elsif mapping.key?(input) mapping[input] end type[value || input] end alias_method :[], :call def default(*) raise '.enum(*values).default(value) is not supported. Call '\ '.default(value).enum(*values) instead' end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-types-0.10.3 | lib/dry/types/enum.rb |