| 1 |
# frozen-string-literal: true |
| 2 |
|
| 3 |
module DidYouMean |
| 4 |
# The +DidYouMean::Formatter+ is the basic, default formatter for the |
| 5 |
# gem. The formatter responds to the +message_for+ method and it returns a |
| 6 |
# human readable string. |
| 7 |
class Formatter |
| 8 |
|
| 9 |
# Returns a human readable string that contains +corrections+. This |
| 10 |
# formatter is designed to be less verbose to not take too much screen |
| 11 |
# space while being helpful enough to the user. |
| 12 |
# |
| 13 |
# @example |
| 14 |
# |
| 15 |
# formatter = DidYouMean::Formatter.new |
| 16 |
# |
| 17 |
# # displays suggestions in two lines with the leading empty line |
| 18 |
# puts formatter.message_for(["methods", "method"]) |
| 19 |
# |
| 20 |
# Did you mean? methods |
| 21 |
# method |
| 22 |
# # => nil |
| 23 |
# |
| 24 |
# # displays an empty line |
| 25 |
# puts formatter.message_for([]) |
| 26 |
# |
| 27 |
# # => nil |
| 28 |
# |
| 29 |
def self.message_for(corrections) |
| 30 |
corrections.empty? ? "" : "\nDid you mean? #{corrections.join("\n ")}" |
| 31 |
end |
| 32 |
|
| 33 |
def message_for(corrections) |
| 34 |
warn "The instance method #message_for has been deprecated. Please use the class method " \ |
| 35 |
"DidYouMean::Formatter.message_for(...) instead." |
| 36 |
|
| 37 |
self.class.message_for(corrections) |
| 38 |
end |
| 39 |
end |
| 40 |
|
| 41 |
PlainFormatter = Formatter |
| 42 |
|
| 43 |
deprecate_constant :PlainFormatter |
| 44 |
end |