class Markdo::ProcessCommand

Public Class Methods

new(*) click to toggle source
Calls superclass method Markdo::Command::new
# File lib/markdo/commands/process_command.rb, line 6
def initialize(*)
  super
  @lines_by_filename = Hash.new { [] }
  @lines = File.readlines(data_source.inbox_path)
  @line_index = 0
end

Public Instance Methods

run() click to toggle source
# File lib/markdo/commands/process_command.rb, line 13
def run
  catch :abort do
    while has_lines?
      line = current_line
      choice = prompt(line)

      case choice
      when 'h'
        show_help
      when 'i'
        move_line_to(@env['MARKDO_INBOX'], line)
      when 's'
        move_line_to('Sprint.md', line)
      when 'b'
        move_line_to('Backlog.md', line)
      when 'm'
        move_line_to('Maybe.md', line)
      when 'a'
        throw :abort
      end
    end

    write_files
  end
end

Private Instance Methods

current_line() click to toggle source
# File lib/markdo/commands/process_command.rb, line 45
def current_line
  @lines[@line_index]
end
has_lines?() click to toggle source
# File lib/markdo/commands/process_command.rb, line 41
def has_lines?
  @line_index < @lines.length
end
move_line_to(filename, line) click to toggle source
# File lib/markdo/commands/process_command.rb, line 64
def move_line_to(filename, line)
  @lines_by_filename[filename] <<= line
  @line_index += 1
end
prompt(line) click to toggle source
# File lib/markdo/commands/process_command.rb, line 49
def prompt(line)
  @stdout.puts line
  @stdout.print 'File [hisbma]? '

  input_line = @stdin.gets

  @stdout.puts

  if input_line.nil?
    throw :abort
  else
    input_line.chomp.downcase
  end
end
show_help() click to toggle source
# File lib/markdo/commands/process_command.rb, line 83
def show_help
  @stdout.puts 'i - inbox (keep in inbox)'
  @stdout.puts 's - sprint'
  @stdout.puts 'b - backlog'
  @stdout.puts 'm - maybe'
  @stdout.puts 'a - abort; make no changes'
end
write_files() click to toggle source
# File lib/markdo/commands/process_command.rb, line 69
def write_files
  inbox_lines = @lines_by_filename.delete(@env['MARKDO_INBOX'])
  File.write(data_source.inbox_path, inbox_lines ? inbox_lines.join : '')

  @lines_by_filename.each do |filename, lines|
    path = data_source.file_path(filename)
    new_content = ["\n## Processed on #{@today.iso8601}\n\n"] << lines

    File.open(path, 'a') do |file|
      file.puts(new_content.join)
    end
  end
end