Class: TodoFind::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/todofind/finder.rb

Overview

Public: The comment finder. It finds all files in the current directory

containing the comments specified in the config data, or the
defaults: TODO and FIXME.

Examples:

finder = Finder.new(config_data, '.')
finder.find
# => [{:filename => "my_file.rb", :comments =>
                                  [{:line => 3,
                                    :label => "TODO",
                                    :comment => "A todo comment"}]}]

Instance Method Summary collapse

Constructor Details

#initialize(config, path) ⇒ Finder

Public: Create a new Finder object with specified config data and a path.

config - A hash of the configuration data. Allowed options are:

:labels - An array of labels to find (Defaults are TODO and
          FIXME)
:exclude_files - An array of files to exclude from the search.
:exclude_dirs - An array of directories to exclude from the
                search.

path - The path to start the search at. The search will recurse down from

here but never above this directory.


26
27
28
29
# File 'lib/todofind/finder.rb', line 26

def initialize(config, path)
  @config = TodoFind::Config.new config
  @path = path
end

Instance Method Details

#findObject

Public: Start recursing through directories under the Finder object’s

path and look for comments with the specified labels.

Examples:

finder.find

# => [{:filename => "my_file.rb",
       :comments => [{:line => 3,
                      :label => "TODO",
                      :comment => "Do something"}]}]

Returns a Hash of the comment data.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/todofind/finder.rb', line 44

def find
file_list = get_file_list
regex = /(#{@config.labels.join "|"}): (.+)/
data = []

file_list.each do |file|
  if File.binary? file
    next
  end

  file_data = { :filename => file, :comments => [] }

  contents = File.read file
  lines = contents.split "\n"
  lines.each_with_index do |line, line_num|
    line.match regex do |match|
      label = match[1]
      comment = match[2]
      file_data[:comments] << { :line => line_num + 1,
        :label => label,
        :comment => comment }
      end
    end
    if !file_data[:comments].empty?
      data << file_data
    end
  end
  return data
end