Class: Nin::Todo
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
- #fork_sync(op, store_write = false, params = {}) ⇒ Object
-
#initialize(config, options = {}) ⇒ Todo
constructor
A new instance of Todo.
- #list ⇒ Object
- #prioritize(id, step = 1) ⇒ Object
- #sync(op, store_write = false, params = {}) ⇒ Object
Constructor Details
#initialize(config, options = {}) ⇒ Todo
Returns a new instance of Todo.
8 9 10 11 12 13 |
# File 'lib/nin/todo.rb', line 8 def initialize(config, = {}) @store = config.fetch(:store) @integration_syncrhonizer = config.fetch(:integration_syncrhonizer, nil) = @items = load_items_sorted end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
5 6 7 |
# File 'lib/nin/todo.rb', line 5 def items @items end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
6 7 8 |
# File 'lib/nin/todo.rb', line 6 def store @store end |
Instance Method Details
#add(desc, date, tags) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/nin/todo.rb', line 27 def add(desc, date, ) item = Item.new(next_id, desc, date, ) @items << item fork_sync(:add, true, item: item) @store.write(to_hash) end |
#analyze ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/nin/todo.rb', line 105 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
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/nin/todo.rb', line 75 def archive(*ids) unless [:completed_only] ids.each do |id| item = find_by_id(id.to_i) item.toggle_archived! end else completed_unarchived_items.each(&:toggle_archived!) end @store.write(to_hash) end |
#complete(*ids) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nin/todo.rb', line 62 def complete(*ids) items = ids.map do |id| item = find_by_id(id.to_i) item.toggle_completed! item end fork_sync(:edit_completed, false, items: items) @store.write(to_hash) end |
#delete(*ids) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/nin/todo.rb', line 92 def delete(*ids) uids = ids.map do |id| item = find_by_id(id.to_i) @items.delete(item) item.uid end fork_sync(:delete, false, ids: uids) reset_item_indices! end |
#delete_archived ⇒ Object
88 89 90 |
# File 'lib/nin/todo.rb', line 88 def delete_archived delete(*archived_items.map(&:id)) end |
#edit(id, desc, date, tags) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/nin/todo.rb', line 36 def edit(id, desc, date, ) item = find_by_id(id) item.edit(desc, date, ) fork_sync(:edit, false, items: item) @store.write(to_hash) end |
#fork_sync(op, store_write = false, params = {}) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/nin/todo.rb', line 128 def fork_sync(op, store_write = false, params = {}) return unless @integration_syncrhonizer pid = fork do @integration_syncrhonizer.sync(op, params) @store.write(to_hash) if store_write exit end Process.detach(pid) end |
#list ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/nin/todo.rb', line 15 def list sync(:read, true, items: @items, next_id: next_id) unless [:local] items_to_list = if [:archived] @items else unarchived_items end puts Presenter::TodoPresenter.new(items_to_list).call end |
#prioritize(id, step = 1) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/nin/todo.rb', line 45 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 |
#sync(op, store_write = false, params = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/nin/todo.rb', line 114 def sync(op, store_write = false, params = {}) return unless @integration_syncrhonizer begin Timeout::timeout(@integration_syncrhonizer.timeout_interval) { @integration_syncrhonizer.sync(op, params) reset_item_indices! if store_write } rescue Timeout::Error puts 'Syncing timed out. Showing local items...' puts end end |