Sha256: 756de4ba0d2e3ba745e50c01d9e35121864d017a52c60a2f8d1b0ef76860dd24

Contents?: true

Size: 699 Bytes

Versions: 2

Compression:

Stored size: 699 Bytes

Contents

module Rasti
  module Types
    class Enum

      include Castable

      attr_reader :values

      def self.[](*values)
        new values
      end

      def to_s
        "#{self.class}[#{values.map(&:inspect).join(', ')}]"
      end
      alias_method :inspect, :to_s

      private

      def initialize(values)
        @values = values.map(&:to_s)
        define_getters
      end

      def valid?(value)
        values.include? String.cast(value)
      end

      def transform(value)
        String.cast value
      end

      def define_getters
        values.each do |value|
          define_singleton_method(Inflecto.underscore(value)) { value }
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rasti-types-2.0.1 lib/rasti/types/enum.rb
rasti-types-2.0.0 lib/rasti/types/enum.rb