module Ptf::Commands::List

Public Class Methods

generate_list_display(open, closed) click to toggle source
# File lib/ptf/commands/list.rb, line 68
def generate_list_display(open, closed)
  list_display = ""
  first = true, past_due = false, have_due_date = true
  open.each do |t|
    if have_due_date && t.due_date.nil?
      have_due_date = false
      list_display += "\n" unless first
      list_display += "=========== NoDate ===========\n\n"
    end

    if have_due_date && first && t.due_date <= DateTime.now
      first = false
      past_due = true
      list_display += "============ Past ============\n\n"
    elsif first && have_due_date
      first = false
      list_display += "========== Upcoming ==========\n\n"
    end

    if have_due_date && past_due && t.due_date > DateTime.now
      past_due = false
      list_display += "\n========== Upcoming ==========\n\n"
    end

    list_display += "#{t.due_date_list_format} #{t.title} (#{t.group.abbreviation} #{t.id})\n"
  end

  show_closed_banner = true
  closed.each do |t|
    if show_closed_banner
      list_display += "\n" unless list_display == ""
      list_display += "=========== Closed ===========\n\n"
      show_closed_banner = false
    end

    list_display += "#{t.completed_at_list_format} #{t.title} (#{t.group.abbreviation} #{t.id})\n"
  end

  list_display
end
get_metadata_for_group(group, flag) click to toggle source
# File lib/ptf/commands/list.rb, line 6
def get_metadata_for_group(group, flag)
  raise ArgumentError, "Group #{group} does not exist." unless Ptf::Group.group_exist?(group)

  group_name = Ptf::Group.get_group(group).name
  open_group_path = File.join(Ptf::FileSystem.metadata_open_dir, group_name)
  open_group_dir = Dir.new(open_group_path)
  closed_group_path = File.join(Ptf::FileSystem.metadata_closed_dir, group_name)
  closed_group_dir = Dir.new(closed_group_path)

  open_tasks = []
  closed_tasks = []
  if flag == :open || flag == :both
    open_group_dir.to_a.each do |file|
      path = File.join(open_group_path, file)

      if File.file? path
        open_tasks.push Ptf::MetadataFile.create_from_file(path)
      end
    end
  end

  if flag == :closed || flag == :both
    closed_group_dir.to_a.each do |file|
      path = File.join(closed_group_path, file)

      if File.file? path
        closed_tasks.push Ptf::MetadataFile.create_from_file(path)
      end
    end
  end

  [open_tasks, closed_tasks]
end
list(delimiter, open, closed) click to toggle source
# File lib/ptf/commands/list.rb, line 109
def list(delimiter, open, closed)
  if open && closed
    flag = :both
  elsif closed
    flag = :closed
  else
    flag = :open
  end

  if delimiter.downcase == "all"
    all_groups = Ptf::Group.all_groups

    open_tasks = []
    closed_tasks = []
    all_groups.each do |g|
      open, closed = get_metadata_for_group(g.name, flag)
      open_tasks.concat open
      closed_tasks.concat closed
    end

  else
    if !Ptf::Group.group_exist?(delimiter)
      return "Group #{delimiter} does not exist."
    end

    open_tasks, closed_tasks = get_metadata_for_group(delimiter, flag)
  end

  open_tasks = sort_open_task_arr(open_tasks)
  closed_tasks = sort_closed_task_arr(closed_tasks)
  return generate_list_display(open_tasks, closed_tasks)
end
sort_closed_task_arr(task_arr) click to toggle source
# File lib/ptf/commands/list.rb, line 58
def sort_closed_task_arr(task_arr)
  sorted_arr = task_arr

  sorted_arr.sort! do |a,b|
    b.completed_at <=> a.completed_at
  end

  sorted_arr
end
sort_open_task_arr(task_arr) click to toggle source
# File lib/ptf/commands/list.rb, line 40
def sort_open_task_arr(task_arr)
  sorted_arr = task_arr

  sorted_arr.sort! do |a,b|
    if a.due_date.nil? && b.due_date.nil?
      a.created_at <=> b.created_at
    elsif a.due_date.nil?
      1
    elsif b.due_date.nil?
      -1
    else
      a.due_date <=> b.due_date
    end
  end

  sorted_arr
end