Class: Todo::List

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

Overview

Initializes a Todo List object with a path to the corresponding todo.txt file. For example, if your todo.txt file is located at:

/home/sam/Dropbox/todo/todo.txt

You would initialize the list object like:

list = Todo::List.new("/home/sam/Dropbox/todo/todo.txt")

Alternately, you can initialize the object with an array of strings or tasks. If the array is of strings, the strings will be converted into tasks. You can supply a mixed list of string and tasks if you wish.

Example:

tasks = []
tasks << "A task line"
tasks << Todo::Task.new("A task object")

list = Todo::List.new(tasks)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ List

Returns a new instance of List.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/todo/list.rb', line 23

def initialize(list)
  case list
  when Array
    tasks = list.map do |item|
      case item
      when String then Task.new(item)
      when Task then item
      else
        raise "Cannot add #{item.class} to list."
      end
    end

    concat(tasks)

  when String
    @path = list
    concat(File.read(list))
  end
end

Instance Attribute Details

#pathObject (readonly)

The path to the todo.txt file that you supplied when you created the Todo::List object.



45
46
47
# File 'lib/todo/list.rb', line 45

def path
  @path
end

Instance Method Details

#by_context(context) ⇒ Todo::List

Filters the list by context and returns a new list.

Example:

list = Todo::List.new("/path/to/list")
list.by_context("@admin")
# => <Todo::List> filtered by '@admin'

Parameters:

  • context (String)

Returns:



71
72
73
# File 'lib/todo/list.rb', line 71

def by_context(context)
  List.new(select { |task| task.contexts.include? context })
end

#by_doneTodo::List

Filters the list by completed tasks and returns a new list.

Example:

list = Todo::List.new("/path/to/list")
list.by_done
# => <Todo::List> filtered by tasks that are done

Returns:



98
99
100
# File 'lib/todo/list.rb', line 98

def by_done
  List.new(select(&:done?))
end

#by_not_doneTodo::List

Filters the list by incomplete tasks and returns a new list.

Example:

list = Todo::List.new("/path/to/list")
list.by_not_done
# => <Todo::List> filtered by tasks that are not done

Returns:



111
112
113
# File 'lib/todo/list.rb', line 111

def by_not_done
  List.new(select { |task| task.done? == false })
end

#by_priority(priority) ⇒ Todo::List

Filters the list by priority and returns a new list.

Example:

list = Todo::List.new("/path/to/list")
list.by_priority("A")
# => Will be a new list with only priority 'A' tasks

Parameters:

  • priority (String)

Returns:



57
58
59
# File 'lib/todo/list.rb', line 57

def by_priority(priority)
  List.new(select { |task| task.priority == priority })
end

#by_project(project) ⇒ Todo::List

Filters the list by project and returns a new list.

Example:

list = Todo::List.new("/path/to/list")
list.by_project("+blog")
# => <Todo::List> filtered by '+blog'

Parameters:

  • project (String)

Returns:



85
86
87
# File 'lib/todo/list.rb', line 85

def by_project(project)
  List.new(select { |task| task.projects.include? project })
end

#save!Object

Saves the list to the original file location.

Warning: This is a destructive operation and will overwrite any existing content in the file. It does not attempt to diff and append changes.

If no ‘path` is specified in the constructor then an error is raised.



121
122
123
124
125
# File 'lib/todo/list.rb', line 121

def save!
  raise "No path specified." unless path

  File.write(path, self)
end