Module: Goldie::Commands

Extended by:
Commands
Included in:
Commands
Defined in:
lib/goldie/commands.rb

Constant Summary collapse

COMMAND_LIST =
{
  add: "create a new item",
  update: "modify an item",
  done: "mark an item as done",
  not_done: "mark an item as not done",
  archive: "archive an item",
  unarchive: "unarchive an item",
  delete: "delete an item",
  list: "list certain items",
  progress: "show progress bar",
  list_progress: "list items and show progress bar",
  help: "list available commands"
}

Instance Method Summary collapse

Instance Method Details

#_display_errors(item) ⇒ Object



163
164
165
166
167
168
169
# 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) ⇒ Object



155
156
157
158
159
160
161
# 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) ⇒ Object



171
172
173
174
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



78
79
80
81
82
83
84
85
# 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) ⇒ Object



96
97
98
99
100
101
102
103
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

#helpObject



145
146
147
148
149
150
151
152
153
# 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) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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_progressObject



135
136
137
138
139
140
141
142
143
# 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) ⇒ Object



69
70
71
72
73
74
75
76
# 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

#progressObject



124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Object



87
88
89
90
91
92
93
94
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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