Sha256: b439bfe1d1c7c8dc4dda5cae7e4d284cdc23c63956d99a57b15749c020de17a4

Contents?: true

Size: 487 Bytes

Versions: 9

Compression:

Stored size: 487 Bytes

Contents

module NxtSupport
  class EnumHash < ActiveSupport::HashWithIndifferentAccess
    def initialize(*keys)
      super()

      keys.each do |key|
        self[normalized_key(key)] = key.to_s
      end

      freeze
    end

    def self.[](*keys)
      new(*keys)
    end

    def [](key)
      super(key) || (raise KeyError, "No value for key '#{key}' in #{inspect}")
    end

    private

    def normalized_key(key)
      key.to_s.downcase.underscore.gsub(/\s+/, '_')
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
nxt_support-0.1.9 lib/nxt_support/util/enum_hash.rb
nxt_support-0.1.8 lib/nxt_support/util/enum_hash.rb
nxt_support-0.1.7 lib/nxt_support/util/enum_hash.rb
nxt_support-0.1.6 lib/nxt_support/util/enum_hash.rb
nxt_support-0.1.5 lib/nxt_support/util/enum_hash.rb
nxt_support-0.1.4 lib/nxt_support/util/enum_hash.rb
nxt_support-0.1.3 lib/nxt_support/util/enum_hash.rb
nxt_support-0.1.2 lib/nxt_support/util/enum_hash.rb
nxt_support-0.1.1 lib/nxt_support/util/enum_hash.rb