Class: Todos

Inherits:
Object
  • Object
show all
Defined in:
lib/totrello/todos.rb

Overview

Todos

Instance Method Summary collapse

Instance Method Details

#all_todos(root_directory = "#{Dir.pwd}", config) ⇒ Object



3
4
5
6
# File 'lib/totrello/todos.rb', line 3

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

#clean_todo(todo, config) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/totrello/todos.rb', line 53

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

#lines_with_index_for_file(file) ⇒ Object



34
35
36
37
38
39
# File 'lib/totrello/todos.rb', line 34

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

#load_files(root_directory = "#{Dir.pwd}", config) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/totrello/todos.rb', line 8

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

#todo?(line, config) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/totrello/todos.rb', line 41

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

#todos_for_file(file, config) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/totrello/todos.rb', line 17

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