Class: Nin::Todo
- Inherits:
-
Object
- Object
- Nin::Todo
- Defined in:
- lib/nin/todo.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #add(desc, date, tags) ⇒ Object
- #analyze ⇒ Object
- #archive(*ids) ⇒ Object
- #complete(*ids) ⇒ Object
- #delete(*ids) ⇒ Object
- #delete_archived ⇒ Object
- #edit(id, desc, date, tags) ⇒ Object
-
#initialize(store = YamlStore.new, options = {}) ⇒ Todo
constructor
A new instance of Todo.
- #list ⇒ Object
- #prioritize(id, step = 1) ⇒ Object
Constructor Details
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
3 4 5 |
# File 'lib/nin/todo.rb', line 3 def items @items end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
4 5 6 |
# File 'lib/nin/todo.rb', line 4 def store @store end |
Instance Method Details
#add(desc, date, tags) ⇒ Object
22 23 24 25 26 |
# File 'lib/nin/todo.rb', line 22 def add(desc, date, ) @items << Item.new(next_id, desc, date, ) @store.write(to_hash) end |
#analyze ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/nin/todo.rb', line 86 def analyze items_to_analyze = @items.where(:completed, true) histogram = items_to_analyze.group_by(&:date).map { |k, v| [k, v.size] } histogram.each do |date, size| puts "#{date} : #{'*' * size}" end end |
#archive(*ids) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/nin/todo.rb', line 62 def archive(*ids) ids.each do |id| item = find_by_id(id.to_i) item.toggle_archived! end @store.write(to_hash) end |
#complete(*ids) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/nin/todo.rb', line 53 def complete(*ids) ids.each do |id| item = find_by_id(id.to_i) item.toggle_completed! end @store.write(to_hash) end |
#delete(*ids) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/nin/todo.rb', line 77 def delete(*ids) ids.each do |id| item = find_by_id(id.to_i) @items.delete(item) end reset_item_indices! end |
#delete_archived ⇒ Object
71 72 73 74 75 |
# File 'lib/nin/todo.rb', line 71 def delete_archived delete(*archived_items.map(&:id)) reset_item_indices! end |
#edit(id, desc, date, tags) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/nin/todo.rb', line 28 def edit(id, desc, date, ) item = find_by_id(id) item.edit(desc, date, ) @store.write(to_hash) end |
#list ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/nin/todo.rb', line 12 def list items_to_list = if @options[:archived] @items else unarchived_items end puts Presenter::TodoPresenter.new(items_to_list).call end |
#prioritize(id, step = 1) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nin/todo.rb', line 36 def prioritize(id, step = 1) item_to_prioritize = find_by_id(id) item_group = @items.group_by(&:date)[item_to_prioritize.date] new_id, actual_step = item_group.map(&:id).round_shift(id, step) step_sign = actual_step > 0 ? +1 : -1 items_to_reprioritize = item_group.where(:id) do |item_id| step_sign * item_id < step_sign * id end.limit(actual_step) item_to_prioritize.id = new_id items_to_reprioritize.each { |item| item.id += step_sign } @store.write(to_hash) end |