diff options
Diffstat (limited to 'rubocop/todo_dir.rb')
-rw-r--r-- | rubocop/todo_dir.rb | 63 |
1 files changed, 45 insertions, 18 deletions
diff --git a/rubocop/todo_dir.rb b/rubocop/todo_dir.rb index 4aca4454a06..57b9895a925 100644 --- a/rubocop/todo_dir.rb +++ b/rubocop/todo_dir.rb @@ -6,22 +6,36 @@ require 'active_support/inflector/inflections' module RuboCop # Helper class to manage file access to RuboCop TODOs in .rubocop_todo directory. class TodoDir - DEFAULT_TODO_DIR = File.expand_path('../.rubocop_todo', __dir__) + # Suffix a TODO file. + SUFFIX_YAML = '.yml' # Suffix to indicate TODOs being inspected right now. SUFFIX_INSPECT = '.inspect' - attr_reader :directory - + # Instantiates a TodoDir. + # + # @param directory [String] base directory where all TODO YAML files are written to. + # @param inflector [ActiveSupport::Inflector, #underscore] an object which supports + # converting a string to its underscored version. def initialize(directory, inflector: ActiveSupport::Inflector) @directory = directory @inflector = inflector end - def read(cop_name, suffix = nil) - read_suffixed(cop_name) + # Reads content of TODO YAML for given +cop_name+. + # + # @param cop_name [String] name of the cop rule + # + # @return [String, nil] content of the TODO YAML file if it exists + def read(cop_name) + path = path_for(cop_name) + + File.read(path) if File.exist?(path) end + # Saves +content+ for given +cop_name+ to TODO YAML file. + # + # @return [String] path of the written TODO YAML file def write(cop_name, content) path = path_for(cop_name) @@ -31,6 +45,10 @@ module RuboCop path end + # Marks a TODO YAML file for inspection by renaming the original TODO YAML + # and appending the suffix +.inspect+ to it. + # + # @return [Boolean] +true+ a file was marked for inspection successfully. def inspect(cop_name) path = path_for(cop_name) @@ -42,38 +60,47 @@ module RuboCop end end + # Marks all TODO YAML files for inspection. + # + # @return [Integer] number of renamed YAML TODO files. + # + # @see inspect def inspect_all - pattern = File.join(@directory, '**/*.yml') + pattern = File.join(@directory, "**/*#{SUFFIX_YAML}") Dir.glob(pattern).count do |path| FileUtils.mv(path, "#{path}#{SUFFIX_INSPECT}") end end + # Returns a list of TODO YAML files which are marked for inspection. + # + # @return [Array<String>] list of paths + # + # @see inspect + # @see inspect_all def list_inspect - pattern = File.join(@directory, "**/*.yml.inspect") + pattern = File.join(@directory, "**/*#{SUFFIX_YAML}#{SUFFIX_INSPECT}") Dir.glob(pattern) end + # Deletes a list of TODO yaml files which were marked for inspection. + # + # @return [Integer] number of deleted YAML TODO files. + # + # @see #inspect + # @see #inspect_all def delete_inspected - pattern = File.join(@directory, '**/*.yml.inspect') - - Dir.glob(pattern).count do |path| + list_inspect.count do |path| File.delete(path) end end private - def read_suffixed(cop_name, suffix = nil) - path = path_for(cop_name, suffix) - - File.read(path) if File.exist?(path) - end - - def path_for(cop_name, suffix = nil) - todo_path = "#{@inflector.underscore(cop_name)}.yml#{suffix}" + def path_for(cop_name) + todo_path = "#{@inflector.underscore(cop_name)}#{SUFFIX_YAML}" File.join(@directory, todo_path) end |