Sha256: fab41f2d4d1fe68fd128184514ab16c7aa6849147dd2ef192ee49570cf4d45da

Contents?: true

Size: 683 Bytes

Versions: 25

Compression:

Stored size: 683 Bytes

Contents

module Incline
  class ConstantEnum
    
    attr_reader :value, :name
    
    def initialize(value)
      raise ArgumentError, 'Invalid value' unless self.class.named?(value)
      @value = value
      @name = self.class.name_for(value)
    end
    
    def to_s
      name
    end
    
    
    
    def self.named?(value)
      !name_for(value).blank?
    end
    
    def self.name_for(value)
      names.key(value) || ''
    end
    
    private
    
    def self.names
      @names ||=
          begin
            ret = {}
            constants(false).each do |nm|
              ret[nm.to_s] = const_get(nm)
            end
            ret
          end
    end
    
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
incline-0.3.14 lib/incline/constant_enum.rb
incline-0.3.13 lib/incline/constant_enum.rb
incline-0.3.12 lib/incline/constant_enum.rb
incline-0.3.11 lib/incline/constant_enum.rb
incline-0.3.10 lib/incline/constant_enum.rb
incline-0.3.9 lib/incline/constant_enum.rb
incline-0.3.8 lib/incline/constant_enum.rb
incline-0.3.7 lib/incline/constant_enum.rb
incline-0.3.6 lib/incline/constant_enum.rb
incline-0.3.5 lib/incline/constant_enum.rb
incline-0.3.4 lib/incline/constant_enum.rb
incline-0.3.3 lib/incline/constant_enum.rb
incline-0.3.2 lib/incline/constant_enum.rb
incline-0.3.1 lib/incline/constant_enum.rb
incline-0.3.0 lib/incline/constant_enum.rb
incline-0.2.36 lib/incline/constant_enum.rb
incline-0.2.35 lib/incline/constant_enum.rb
incline-0.2.34 lib/incline/constant_enum.rb
incline-0.2.28 lib/incline/constant_enum.rb
incline-0.2.27 lib/incline/constant_enum.rb