Sha256: e82c64a13c865c88ffa6cad3ef8898a74a2fafffaa17b6d7ba77aab5753e7eef

Contents?: true

Size: 741 Bytes

Versions: 2

Compression:

Stored size: 741 Bytes

Contents

# frozen_string_literal: true

module Virtus
  class Attribute

    # Type to be used inside attributes as enums.
    class Enum < Attribute

      primitive Axiom::Types::Symbol

      class << self

        attr_accessor :values

        # @param values [Array<Symbol>]
        def [](*values)
          klass = Class.new(self)
          klass.values = values
          klass
        end

        # :nodoc:
        def build_type(*)
          Axiom::Types::Symbol
        end

      end

      def coercion_error_message
        "Enum (#{self.class.values.join(',')})"
      end

      # @param value [Object]
      # @return [Boolean]
      def value_coerced?(value)
        self.class.values.include?(value)
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
virtus2-2.1.0 lib/virtus/attribute/enum.rb
virtus2-2.0.2 lib/virtus/attribute/enum.rb