Sha256: 601af1ff9e905da1710cea4388add570a3eef78926007b7d9430fd6fb62af6b7

Contents?: true

Size: 868 Bytes

Versions: 1

Compression:

Stored size: 868 Bytes

Contents

module Ruby
  module Enum

    attr_reader :key, :value
    
    def initialize(key, value)
      @key = key
      @value = value
    end

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods

      def define(key, value)
        @hash ||= {}
        @hash[key] = self.new(key, value)
      end

      def const_missing(key)
        if @hash[key]
          @hash[key].value
        else
          raise Ruby::Enum::Errors::UninitializedConstantError.new({ :name => name, :key => key })
        end
      end

      def each(&block)
        @hash.each do |key, value|
          yield key, value
        end
      end

      def parse(s)
        s = s.to_s.upcase
        each do |key, value|
          if key.to_s.upcase == s
            return value.value
          end
        end
        nil
      end      

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-enum-0.1.0 lib/ruby-enum/enum.rb