class NxtSupport::Enum

Attributes

store[R]

Public Class Methods

[](*keys) click to toggle source
# File lib/nxt_support/util/enum.rb, line 15
def self.[](*keys)
  new(*keys)
end
new(*keys) click to toggle source
# File lib/nxt_support/util/enum.rb, line 3
def initialize(*keys)
  @store = ActiveSupport::HashWithIndifferentAccess.new

  keys.each do |key|
    normalized_key = normalized_key(key)
    store[normalized_key] = NxtSupport::Enum::Value.new(key)
    define_getter(normalized_key)
  end

  freeze
end

Public Instance Methods

[](key) click to toggle source
# File lib/nxt_support/util/enum.rb, line 19
def [](key)
  store[key] || (raise KeyError, "No value for key '#{key}' in #{store}")
end
to_h() click to toggle source
# File lib/nxt_support/util/enum.rb, line 23
def to_h
  store
end

Private Instance Methods

define_getter(normalized_key) click to toggle source
# File lib/nxt_support/util/enum.rb, line 37
def define_getter(normalized_key)
  define_singleton_method normalized_key do
    store[normalized_key]
  end
end
normalized_key(key) click to toggle source
# File lib/nxt_support/util/enum.rb, line 33
def normalized_key(key)
  key.to_s.downcase.underscore.gsub(/\s+/, '_')
end