Module: Notes::Tasks

Extended by:
Tasks
Included in:
Tasks
Defined in:
lib/notes-cli/tasks.rb

Instance Method Summary collapse

Instance Method Details

#all(options = Notes::Options.defaults) ⇒ Object

Return list of tasks for the provided options If no options are provided, will use default file locations and flags

Returns Array



126
127
128
129
# File 'lib/notes-cli/tasks.rb', line 126

def all(options = Notes::Options.defaults)
  files = Notes.valid_files(options)
  for_files(files, options)
end

#for_file(filename, flags) ⇒ Object

Parse a file and construct Task objects for each line matching one of the patterns specified in ‘flags`

filename - A String filename to read flags - Array of String flags to match against

Returns Array<Notes::Task>



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/notes-cli/tasks.rb', line 68

def for_file(filename, flags)
  counter = 1
  tasks   = []

  begin
    lines = File.readlines(filename).map(&:chomp)

    lines.each_with_index do |line, idx|
      matched_flags = matching_flags(line, flags)

      if matched_flags.any?
        task_options = {
          filename: Notes.shortname(filename),
          line_num: counter,
          line: line,
          flags: matched_flags,
          context: context_lines(lines, idx)
        }
        # Extract line information from git
        info = line_info(filename, idx)
        task_options[:author] = info[:author]
        task_options[:date]   = info[:date]
        task_options[:sha]    = info[:sha]
        tasks << Notes::Task.new(task_options)
      end
      counter += 1
    end
  rescue
    # Error occurred reading the file (ex: invalid byte sequence in UTF-8)
    # Move on quietly
  end

  tasks
end

#for_files(files, options) ⇒ Object

Compute all tasks for a set of files and flags

files - Array of String filenames options - Hash of options

:flags - Array of String flags to match against

Returns Array



110
111
112
113
114
115
116
117
118
119
# File 'lib/notes-cli/tasks.rb', line 110

def for_files(files, options)
  flags  = options[:flags]
  result = []
  files.each do |filename|
    tasks = Notes::Tasks.for_file(filename, flags)
    result += tasks
  end

  result
end

#matching_flags(line, flags) ⇒ Object

Return array of flags matched in a line

line - A String to match against flags - An Array of String flags to search for

Returns Array of string flags found



56
57
58
59
# File 'lib/notes-cli/tasks.rb', line 56

def matching_flags(line, flags)
  words = line.split(/\W/).map(&:upcase)
  words & flags.map(&:upcase)
end