Sha256: 2eb9db9d510b6da55dbe68991cc8e2089e46468a7ce4330111b71f520f3cbfb9

Contents?: true

Size: 774 Bytes

Versions: 1

Compression:

Stored size: 774 Bytes

Contents

require 'inflecto'
require_relative 'enum/version'

module Rasti
  module Enum

    class Value < String

      def initialize
        super text_value
      end

      private

      def text_value
        Inflecto.underscore(Inflecto.demodulize(self.class.name)).upcase
      end

    end

    def values
      @values ||= included_classes.map(&:new)
    end

    def [](value)
      values.detect { |e| e == value.to_s } || raise("Invalid value #{value} for #{name}")
    end

    def include?(value)
      values.any? { |e| e == value.to_s }
    end

    private

    def class_constants
      constants.select { |c| const_get(c).is_a? Class }.sort
    end

    def included_classes
      @included_classes ||= class_constants.map { |c| const_get(c) }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rasti-enum-1.0.1 lib/rasti/enum.rb