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.sort_by(&:date)
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
# 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

#archive(*ids) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/nin/todo.rb', line 44

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



35
36
37
38
39
40
41
42
# File 'lib/nin/todo.rb', line 35

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



58
59
60
61
62
63
64
65
# File 'lib/nin/todo.rb', line 58

def delete(*ids)
  ids.each do |id|
    item = find_by_id(id.to_i)
    @items.delete(item)
  end

  reset_item_indices!
end

#delete_archivedObject



53
54
55
56
# File 'lib/nin/todo.rb', line 53

def delete_archived
  delete(*archived_items.map(&:id))
  reset_item_indices!
end

#edit(id, desc, date, tags) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/nin/todo.rb', line 27

def edit(id, desc, date, tags)
  item      = find_by_id(id)
  item.desc = desc
  item.date = date
  item.tags = 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
  if @options[:archived]
    @items
  else
    unarchived_items
  end.each do |item|
    puts item
  end
end