Sha256: b2bb5ec0af4984b8a8f414bc73c310da57137604d888d7e50ccc0370b5976f6e

Contents?: true

Size: 678 Bytes

Versions: 5

Compression:

Stored size: 678 Bytes

Contents

module Rasti
  module Types
    class TypedEnum

      include Castable

      def self.[](enum)
        new enum
      end

      def initialize(enum)
        @enum = enum
        define_getters
      end

      def values
        enum.values
      end

      def to_s
        "#{self.class}[#{enum}]"
      end
      alias_method :inspect, :to_s

      private

      attr_reader :enum

      def valid?(value)
        enum.include? value
      end

      def transform(value)
        enum[value]
      end

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

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rasti-types-2.0.1 lib/rasti/types/typed_enum.rb
rasti-types-2.0.0 lib/rasti/types/typed_enum.rb
rasti-types-1.1.2 lib/rasti/types/typed_enum.rb
rasti-types-1.1.1 lib/rasti/types/typed_enum.rb
rasti-types-1.1.0 lib/rasti/types/typed_enum.rb