Module: GuessMethod

Included in:
Object
Defined in:
lib/guessmethod.rb,
lib/guessmethod/version.rb

Overview

:nodoc:

Defined Under Namespace

Modules: GuessConstant, VERSION Classes: GuessMethodGuesser, GuessMethodOutputter

Constant Summary collapse

GuessMethodOptions =
{
  :insert_weight => 1,
  :delete_weight => 1,
  :substitution_weight => 1,
  :threshold => 2,
  :max_inspect_length => 25,
  :active => true
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/guessmethod.rb', line 21

def self.included(base)
  base.class_eval do
    unless method_defined? :method_missing
      def method_missing(meth, *args, &block); super; end
    end
    alias_method :unguessed_method_missing, :method_missing
    alias_method :method_missing, :guess_method_missing
    
    base.extend(GuessConstant)
  end
end

Instance Method Details

#guess_method_missing(meth, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/guessmethod.rb', line 33

def guess_method_missing(meth, *args, &block)
  if GuessMethodOptions[:active]
    begin
      unguessed_method_missing(meth, *args, &block)
    rescue NoMethodError, NameError => e
      possible_methods = GuessMethodGuesser.find_closest(self.methods, meth)
      case possible_methods.size
      when 1
        call_method = possible_methods.first
        $stderr.puts GuessMethodOutputter.replacing_method(meth, call_method, self)
        self.send(call_method, *args, &block)
      when 0
        $stderr.puts GuessMethodOutputter.no_method_in_threshold(meth, self)
        raise e
      else
        $stderr.puts GuessMethodOutputter.ambiguous_method(meth, possible_methods, self)
        raise e
      end
    end
  else
    unguessed_method_missing(meth, *args, &block)
  end
end