class Leftovers::TodoReporter

Public Instance Methods

prepare() click to toggle source
# File lib/leftovers/todo_reporter.rb, line 7
def prepare
  return unless path.exist?

  puts "Removing previous #{path.basename} file"
  puts ''
  path.delete
end
report(only_test:, none:) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 15
def report(only_test:, none:)
  path.write(generate_file_body(only_test, none))
  report_instructions

  0
end
report_success() click to toggle source
# File lib/leftovers/todo_reporter.rb, line 22
def report_success
  puts "No #{path.basename} file generated, everything is used"

  0
end

Private Instance Methods

generate_file_body(only_test, none) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 46
    def generate_file_body(only_test, none)
      <<~YML.chomp
        #{generation_message.chomp}
        #
        #{resolution_instructions}
        #{todo_data(only_test, none).chomp}
      YML
    end
generate_list(title, list) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 108
    def generate_list(title, list)
      <<~YML
          # #{title}
        #{list.map { |d| print_definition(d) }.join("\n")}

      YML
    end
generation_message() click to toggle source
# File lib/leftovers/todo_reporter.rb, line 55
    def generation_message
      <<~YML
        # This file was generated by `leftovers --write-todo`
        # Generated at: #{Time.now.utc.strftime('%F %T')} UTC
      YML
    end
keep_data(only_test, none_non_test) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 87
    def keep_data(only_test, none_non_test)
      return if only_test.empty? && none_non_test.empty?

      <<~YML.chomp
        keep:
        #{keep_test_called_data(only_test)}#{keep_never_called_data(none_non_test)}
      YML
    end
keep_never_called_data(list) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 102
def keep_never_called_data(list)
  return if list.empty?

  generate_list('Not directly called at all:', list)
end
keep_test_called_data(list) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 96
def keep_test_called_data(list)
  return if list.empty?

  generate_list('Only directly called in tests:', list)
end
path() click to toggle source
# File lib/leftovers/todo_reporter.rb, line 42
def path
  Leftovers.pwd.join('.leftovers_todo.yml')
end
print_definition(definition) click to toggle source
puts(string) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 120
def puts(string)
  Leftovers.puts(string)
end
report_instructions() click to toggle source
# File lib/leftovers/todo_reporter.rb, line 30
    def report_instructions
      puts <<~MESSAGE
        generated #{path.basename}.
        running leftovers again will read this file and
        not alert you to any unused items mentioned in it.

        commit this file so you/your team can gradually
        address these items while still having leftovers
        alert you to any newly unused items.
      MESSAGE
    end
resolution_instructions() click to toggle source
# File lib/leftovers/todo_reporter.rb, line 62
    def resolution_instructions
      <<~YML
        # for instructions on how to address these
        # see #{Leftovers.resolution_instructions_link}
      YML
    end
test_only_data(list) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 78
    def test_only_data(list)
      return if list.empty?

      <<~YML
        test_only:
        #{generate_list('Defined in tests:', list).chomp}
      YML
    end
todo_data(only_test, none) click to toggle source
# File lib/leftovers/todo_reporter.rb, line 69
def todo_data(only_test, none)
  none_test = none.select(&:test?)
  none_non_test = none.reject(&:test?)
  [
    test_only_data(none_test),
    keep_data(only_test, none_non_test)
  ].compact.join
end