Module: GuessMethod::GuessConstant

Included in:
Module
Defined in:
lib/guessmethod.rb

Overview

The GuessConstant module aliases out const_missing and replaces it with its own, which attempts to find a similarly named constant for the object at hand if the original method_missing fails.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
66
# File 'lib/guessmethod.rb', line 59

def self.extended(base)   #:nodoc:
  base.class_eval do
    class << self
      alias_method :unguessed_const_missing, :const_missing
      alias_method :const_missing, :guess_const_missing
    end
  end
end

Instance Method Details

#guess_const_missing(sym) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/guessmethod.rb', line 68

def guess_const_missing(sym)
  if GuessMethodOptions[:active]
    begin
      return unguessed_const_missing(sym)
    rescue NameError => e
      possible_consts = GuessMethodGuesser.find_closest(self.constants, sym)
      case possible_consts.size
      when 1
        call_const = possible_consts.first
        $stderr.puts GuessMethodOutputter.replacing_const(sym, call_const, self)
        self.const_get(call_const)
      when 0
        $stderr.puts GuessMethodOutputter.no_const_in_threshold(sym, self)
        raise e
      else
        $stderr.puts GuessMethodOutputter.ambigious_const(sym, possible_consts, self)
        raise e
      end
    end
  else
    unguessed_const_missing(sym)
  end
end