module EagleClaw::ConstantMatcher

Mixin to allow an object to act as a case-insensitive hash against its own constants. This should be used with singleton metaclasses, like so:

class MyClass
  class << self
    include EagleClaw::ConstantMatcher
  end
end

Public Instance Methods

[](constant) click to toggle source

Retrieve the constant for the given symbol. Matching will be performed case-insensitively.

@param [Symbol] constant

A symbol referring to the constant to get.

@example Get the JSON EagleClaw formatter

EagleClaw::Formatters[:json] => EagleClaw::Formatters::JSON
# File lib/eagleclaw/constmatch.rb, line 23
def [](constant)
  constants.each do |const|
    return const_get(const) if const.to_s.downcase == constant.to_s.downcase
  end
  raise NameError, "Constant #{constant.inspect} not found"
end