Module: GuessMethod

Included in:
Object
Defined in:
lib/guessmethod.rb,
lib/guessmethod/method.rb,
lib/guessmethod/guesser.rb,
lib/guessmethod/options.rb,
lib/guessmethod/version.rb,
lib/guessmethod/constant.rb,
lib/guessmethod/outputter.rb

Overview

The GuessMethod module aliases out method_missing and replaces it with its own, which attempts to find a similarly named method callable on the object at hand if the objects original method_missing fails.

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



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/guessmethod.rb', line 7

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



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/guessmethod/method.rb', line 3

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