Class: Hacer::Todolist

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

Overview

A todo list manages a list of Todo items. It holds all Todo items, even completed ones, until you call clean!

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Todolist

Create a new Todolist stored in the given filename

filename

String containing the name of the file to create. If the file exists, it will be parsed as an Hacer todolist. If this isn’t possible, an ArgumentError will be raised



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hacer.rb', line 11

def initialize(filename)
  @filename = filename
  if File.exists?(@filename)
    @todos = File.open(@filename) { |file| YAML.load(file) }
    raise ArgumentError.new("#{@filename} doesn't appear to be an hacer todo list") unless valid_todolist?
  else
    @todos = []
    save_todos
  end
  todo_with_biggest_id = @todos.max { |a,b| a.todo_id <=> b.todo_id }
  @next_id  = todo_with_biggest_id.nil? ? 0 : todo_with_biggest_id.todo_id + 1
end

Instance Method Details

#clean!Object

Cleans out any completed todos. This cannot be undone an the completed todos will be lost forever



45
46
47
48
# File 'lib/hacer.rb', line 45

def clean!
  @todos = self.list(:incomplete)
  save_todos
end

#complete(todo) ⇒ Object

Completes this todo item

todo

Todo to complete



39
40
41
42
# File 'lib/hacer.rb', line 39

def complete(todo)
  todo.complete
  save_todos
end

#create(todo_text) ⇒ Object

Create a new todo and store it in this list

todo_text

String containing the text of the todo item

Returns the Todo item created



29
30
31
32
33
34
# File 'lib/hacer.rb', line 29

def create(todo_text)
  todo = TodoInternal.new(todo_text,next_id)
  @todos << todo
  save_todos
  todo
end

#list(show = :incomplete) ⇒ Object

Return all todos in this Todolist as an Array of Todo

show

Symbol representing which todos to return:

:incomplete

show only those not completed

:all

show everything



55
56
57
58
59
60
61
62
# File 'lib/hacer.rb', line 55

def list(show=:incomplete)
  case show
  when :incomplete then @todos.select { |todo| !todo.completed? }
  when :all then @todos
  else
    raise ArgumentError.new("Only :incomplete or :all are allowed")
  end
end

#size(show = :incomplete) ⇒ Object

Returns the size of the todolist as an Int.

show

same as for #list



67
68
69
# File 'lib/hacer.rb', line 67

def size(show=:incomplete)
  return list(show).size
end