Method: Notes::Tasks#for_file

Defined in:
lib/notes-cli/tasks.rb

#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