diff options
author | Yuki Nishijima <yk.nishijima@gmail.com> | 2020-05-11 23:24:39 -0400 |
---|---|---|
committer | Yuki Nishijima <yk.nishijima@gmail.com> | 2020-05-11 23:25:04 -0400 |
commit | 946dadd3f479198e87873a863d15c7660a8e2b56 (patch) | |
tree | 08997192ddd8890ff9109d6e4902cdb7625c1030 /lib/did_you_mean/spell_checkers/require_path_checker.rb | |
parent | 7cc55f4bc4d836e8edcae05f1b500417fc2b71a3 (diff) |
Sync did_you_mean
Diffstat (limited to 'lib/did_you_mean/spell_checkers/require_path_checker.rb')
-rw-r--r-- | lib/did_you_mean/spell_checkers/require_path_checker.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/did_you_mean/spell_checkers/require_path_checker.rb b/lib/did_you_mean/spell_checkers/require_path_checker.rb new file mode 100644 index 0000000000..10239947dd --- /dev/null +++ b/lib/did_you_mean/spell_checkers/require_path_checker.rb @@ -0,0 +1,33 @@ +# frozen-string-literal: true + +require_relative "../spell_checker" +require_relative "../tree_spell_checker" + +module DidYouMean + class RequirePathChecker + attr_reader :path + + INITIAL_LOAD_PATH = $LOAD_PATH.dup.freeze + ENV_SPECIFIC_EXT = ".#{RbConfig::CONFIG["DLEXT"]}" + + private_constant :INITIAL_LOAD_PATH, :ENV_SPECIFIC_EXT + + def self.requireables + @requireables ||= INITIAL_LOAD_PATH + .flat_map {|path| Dir.glob("**/???*{.rb,#{ENV_SPECIFIC_EXT}}", base: path) } + .map {|path| path.chomp!(".rb") || path.chomp!(ENV_SPECIFIC_EXT) } + end + + def initialize(exception) + @path = exception.path + end + + def corrections + threshold = path.size * 2 + dictionary = self.class.requireables.reject {|str| str.size >= threshold } + spell_checker = path.include?("/") ? TreeSpellChecker : SpellChecker + + spell_checker.new(dictionary: dictionary).correct(path) + end + end +end |