module Goldie::Commands

Constants

COMMAND_LIST

Public Instance Methods

_display_errors(item) click to toggle source
# File lib/goldie/commands.rb, line 163
def _display_errors(item)
  item.errors.messages.each do |field, messages|
    messages.each do |message|
      puts "- #{field} #{message}"
    end
  end
end
_find_item(id) click to toggle source
# File lib/goldie/commands.rb, line 155
def _find_item(id)
  if item = Item.where(id: id).first
    item
  else
    puts "Couldn't find item with id '#{id}'."
  end
end
_render_progress_bar(percent, width) click to toggle source
# File lib/goldie/commands.rb, line 171
def _render_progress_bar(percent, width)
  progress = (percent * width / 100).to_i
  ("\u2589" * progress).colored.green.bold + ("\u2589" * (width - progress)).colored.white.bold
end
add(title, weight = 1) click to toggle source
# File lib/goldie/commands.rb, line 19
def add(title, weight = 1)
  item = Item.new(title: title, weight: weight)
  if item.save
    puts "Added item ##{item.id}."
    puts
    list_progress
  else
    puts "Item couldn't be saved:"
    _display_errors(item)
  end
end
archive(id) click to toggle source
# File lib/goldie/commands.rb, line 78
def archive(id)
  if item = _find_item(id)
    item.archive!
    puts "Archived item ##{item.id}."
    puts
    list_progress
  end
end
delete(id) click to toggle source
# File lib/goldie/commands.rb, line 96
def delete(id)
  if item = _find_item(id)
    item.destroy
    puts "Deleted item ##{item.id}."
    puts
    list_progress
  end
end
done(id, weight_completed = nil) click to toggle source
# File lib/goldie/commands.rb, line 49
def done(id, weight_completed = nil)
  if item = _find_item(id)
    if weight_completed
      if item.update_attributes(weight_completed: weight_completed)
        puts "Item ##{item.id} updated."
        puts
        list_progress
      else
        puts "Item couldn't be updated:"
        _display_errors(item)
      end
    else
      item.done!
      puts "Item ##{item.id} marked as done."
      puts
      list_progress
    end
  end
end
help() click to toggle source
# File lib/goldie/commands.rb, line 145
def help
  puts "Usage: goldie [command] [arg1] [arg2] ..."
  puts
  puts "Available commands:"
  puts
  COMMAND_LIST.each do |command, summary|
    puts "  % -16s    %s" % [command, summary]
  end
end
list(*flags) click to toggle source
# File lib/goldie/commands.rb, line 105
def list(*flags)
  items = Item.latest
  if flags.include? "archived"
    items = Item.archived
  elsif not flags.include? "all"
    items = Item.active
  end

  table = Terminal::Table.new(title: "Todo List", headings: %w(ID Progress Item)) do |t|
    items.each do |item|
      progress = "%d / %d" % [item.weight_completed, item.weight]
      progress_bar = _render_progress_bar(item.percent_done, 10)
      archived = item.archived? ? "[A] " : ""
      t.add_row [item.id, progress, "#{progress_bar}  #{archived}#{item.title}"]
    end
  end
  puts table
end
list_progress() click to toggle source
# File lib/goldie/commands.rb, line 135
def list_progress
  if Item.active.empty?
    puts "There are no active items to list."
  else
    list
    puts
    progress
  end
end
not_done(id) click to toggle source
# File lib/goldie/commands.rb, line 69
def not_done(id)
  if item = _find_item(id)
    item.not_done!
    puts "Item ##{item.id} marked as not done."
    puts
    list_progress
  end
end
progress() click to toggle source
# File lib/goldie/commands.rb, line 124
def progress
  if Item.active.empty?
    puts "No active items."
  else
    total_weight = Item.active.sum(:weight)
    total_weight_completed = Item.active.sum(:weight_completed)
    percent = total_weight_completed * 100 / total_weight
    puts "%s %s%%" % [_render_progress_bar(percent, 50), percent]
  end
end
unarchive(id) click to toggle source
# File lib/goldie/commands.rb, line 87
def unarchive(id)
  if item = _find_item(id)
    item.unarchive!
    puts "Unarchived item ##{item.id}."
    puts
    list_progress
  end
end
update(id, field, value) click to toggle source
# File lib/goldie/commands.rb, line 31
def update(id, field, value)
  if item = _find_item(id)
    if %w(title weight weight_completed).include? field
      if item.update_attributes(field => value)
        puts "Updated item ##{item.id}."
        puts
        list_progress
      else
        puts "Item couldn't be updated:"
        _display_errors(item)
      end
    else
      puts "Item couldn't be updated:"
      puts "- #{field} isn't a valid field"
    end
  end
end