| 1 |
module DidYouMean |
| 2 |
module Correctable |
| 3 |
if Exception.method_defined?(:detailed_message) |
| 4 |
# just for compatibility |
| 5 |
def original_message |
| 6 |
# we cannot use alias here because |
| 7 |
to_s |
| 8 |
end |
| 9 |
|
| 10 |
def detailed_message(highlight: true, did_you_mean: true, **) |
| 11 |
msg = super.dup |
| 12 |
|
| 13 |
return msg unless did_you_mean |
| 14 |
|
| 15 |
suggestion = DidYouMean.formatter.message_for(corrections) |
| 16 |
|
| 17 |
if highlight |
| 18 |
suggestion = suggestion.gsub(/.+/) { "\e[1m" + $& + "\e[m" } |
| 19 |
end |
| 20 |
|
| 21 |
msg << suggestion |
| 22 |
msg |
| 23 |
rescue |
| 24 |
super |
| 25 |
end |
| 26 |
else |
| 27 |
SKIP_TO_S_FOR_SUPER_LOOKUP = true |
| 28 |
private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP |
| 29 |
|
| 30 |
def original_message |
| 31 |
meth = method(:to_s) |
| 32 |
while meth.owner.const_defined?(:SKIP_TO_S_FOR_SUPER_LOOKUP) |
| 33 |
meth = meth.super_method |
| 34 |
end |
| 35 |
meth.call |
| 36 |
end |
| 37 |
|
| 38 |
def to_s |
| 39 |
msg = super.dup |
| 40 |
suggestion = DidYouMean.formatter.message_for(corrections) |
| 41 |
|
| 42 |
msg << suggestion if !msg.include?(suggestion) |
| 43 |
msg |
| 44 |
rescue |
| 45 |
super |
| 46 |
end |
| 47 |
end |
| 48 |
|
| 49 |
def corrections |
| 50 |
@corrections ||= spell_checker.corrections |
| 51 |
end |
| 52 |
|
| 53 |
def spell_checker |
| 54 |
DidYouMean.spell_checkers[self.class.to_s].new(self) |
| 55 |
end |
| 56 |
end |
| 57 |
end |