Class: Nin::Todo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = YamlStore.new, options = {}) ⇒ Todo

Returns a new instance of Todo.



6
7
8
9
10
# File 'lib/nin/todo.rb', line 6

def initialize(store = YamlStore.new, options = {})
  @store   = store
  @options = options
  @items   = load_items_sorted
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'lib/nin/todo.rb', line 3

def items
  @items
end

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

  @store.write(to_hash)
end

#analyzeObject



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_archivedObject



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, tags)
  item = find_by_id(id)

  item.edit(desc, date, tags)

  @store.write(to_hash)
end

#listObject



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