Class: Nin::Todo

Inherits:
Object show all
Defined in:
lib/nin/todo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @store                    = config.fetch(:store)
  @integration_syncrhonizer = config.fetch(:integration_syncrhonizer, nil)
  @options                  = options
  @items                    = load_items_sorted
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



5
6
7
# File 'lib/nin/todo.rb', line 5

def items
  @items
end

#storeObject (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, tags)
  item = Item.new(next_id, desc, date, tags)
  @items << item

  fork_sync(:add, true, item: item)

  @store.write(to_hash)
end

#analyzeObject



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 @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) ⇒ 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_archivedObject



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, 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 = {}) ⇒ 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

#listObject



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 @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) ⇒ 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