Sha256: 73745ab9b02c8c47d7554226b7759691ffbd5e733aa9c974f12de3535ae26f42

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

# Todos
class Todos
  def all_todos(root_directory = "#{Dir.pwd}", config)
    files = load_files(root_directory, config)
    files.map {|f| todos_for_file(f, config) }.flatten
  end

  def load_files(root_directory = "#{Dir.pwd}", config)
    Dir.glob("#{root_directory}/**/*").select do |f|
      next if File.directory?(f) || config.excludes.include?(f)
      split_file_name = f.split('.')
      File.file?(f) &&
        config.file_types.include?(".#{split_file_name.last}")
    end
  end

  def todos_for_file(file, config)
    lines_with_indexes = lines_with_index_for_file(file)
    lines_with_indexes.reduce([]) do |todos, line|
      if todo?(line[:line], config)
        cleaned_todo = clean_todo(line[:line], config)
        unless cleaned_todo.nil?
          todos.push({
                       todo: cleaned_todo,
                       line_number: line[:index],
                       file: file.to_s
                     })
        end
      end
      todos
    end
  end

  def lines_with_index_for_file(file)
    lines = File.readlines file.to_s
    lines.map.with_index do |line, idx|
      { line: line, index: idx }
    end
  end

  def todo?(line, config)
    return false if line.empty?
    todo_types = config.todo_types
    todo_types.any? do |todo_type|
      return false if line.index(todo_type).nil?
      return false unless config.comment_style.any? do |cs|
        line.strip.start_with?(cs)
      end
      true
    end
  end

  def clean_todo(todo, config)
    config.todo_types.each do |style|
      todo.delete!(style)
    end
    config.comment_style.each do |style|
      todo.delete!(style)
    end
    todo.delete!('#')
    todo.delete!(':')
    todo.chomp!
    todo.lstrip!
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
totrello-1.0.0 lib/totrello/todos.rb