class Nin::Todo
Attributes
items[RW]
store[R]
Public Class Methods
new(config, options = {})
click to toggle source
# File lib/nin/todo.rb, line 8 def initialize(config, options = {}) @store = config.fetch(:store) @integration_syncrhonizer = config.fetch(:integration_syncrhonizer, nil) @options = options @items = load_items_sorted end
Public Instance Methods
add(desc, date, tags)
click to toggle source
# File lib/nin/todo.rb, line 27 def add(desc, date, tags) item = Item.new(next_id, desc, date, tags) @items << item fork_sync(:add, true, item: item) @store.write(to_hash) end
analyze()
click to toggle source
# 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)
click to toggle source
# File lib/nin/todo.rb, line 75 def archive(*ids) unless @options[: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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/nin/todo.rb, line 88 def delete_archived delete(*archived_items.map(&:id)) end
edit(id, desc, date, tags)
click to toggle source
# File lib/nin/todo.rb, line 36 def edit(id, desc, date, tags) item = find_by_id(id) item.edit(desc, date, tags) fork_sync(:edit, false, items: item) @store.write(to_hash) end
fork_sync(op, store_write = false, params = {})
click to toggle source
# 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()
click to toggle source
# File lib/nin/todo.rb, line 15 def list sync(:read, true, items: @items, next_id: next_id) unless @options[:local] items_to_list = if @options[:archived] @items else unarchived_items end puts Presenter::TodoPresenter.new(items_to_list).call end
prioritize(id, step = 1)
click to toggle source
# 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 = {})
click to toggle source
# 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
Private Instance Methods
archived_items()
click to toggle source
# File lib/nin/todo.rb, line 164 def archived_items @items.where(:archived?, true) end
completed_unarchived_items()
click to toggle source
# File lib/nin/todo.rb, line 172 def completed_unarchived_items unarchived_items.where(:completed, true) end
find_by_id(id)
click to toggle source
# File lib/nin/todo.rb, line 176 def find_by_id(id) found_item = @items.find_by(:id, id) raise ItemNotFoundError unless found_item found_item end
groupped_items()
click to toggle source
# File lib/nin/todo.rb, line 190 def groupped_items @items.group_by { |item| item.date.to_s } end
last_id()
click to toggle source
# File lib/nin/todo.rb, line 198 def last_id begin @items.sort_by(&:id).last.id rescue NoMethodError 0 end end
load_items()
click to toggle source
# File lib/nin/todo.rb, line 146 def load_items items = [] @store.read.map do |key, values| date = key.dup values.map do |item| items << Item.new(item.fetch('id').to_i, item.fetch('desc'), date, item.fetch('tags'), item.fetch('uid', nil), item.fetch('completed'), item.fetch('archived')) end end items end
load_items_sorted()
click to toggle source
# File lib/nin/todo.rb, line 142 def load_items_sorted load_items.sort_by { |item| [item.date, item.id] } end
next_id()
click to toggle source
# File lib/nin/todo.rb, line 194 def next_id last_id + 1 end
reset_item_indices!()
click to toggle source
# File lib/nin/todo.rb, line 206 def reset_item_indices! @items.each.with_index(1) do |item, index| item.id = index end @store.write(to_hash) end
to_hash()
click to toggle source
# File lib/nin/todo.rb, line 184 def to_hash groupped_items.reduce({}) do |hash, (date, items)| hash.update(date => items.map(&:to_h)) end end
unarchived_items()
click to toggle source
# File lib/nin/todo.rb, line 168 def unarchived_items @items.where(:archived?, false) end