Class: TodoTxt::List

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/todotxt/list.rb

Overview

Lists can be loaded from a file or instantiated in an empty state. They behave likes arrays and the can be written to a file when you’re finished.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks = []) ⇒ List

Returns a new instance of List.

Parameters:

  • an (Array<Task>)

    array of tasks



25
26
27
# File 'lib/todotxt/list.rb', line 25

def initialize(tasks = [])
  @tasks = tasks
end

Class Method Details

.from_file(file) ⇒ Object

Parses a Todo.txt formatted file

Parameters:

  • file (IO)

    an IO input



16
17
18
19
20
21
22
# File 'lib/todotxt/list.rb', line 16

def self.from_file(file)
  tasks = file.readlines.map { |line|
    chomped = line.chomp
    Task.parse(chomped) unless chomped.empty?
  }.compact
  new tasks
end

Instance Method Details

#eachObject



29
30
31
# File 'lib/todotxt/list.rb', line 29

def each
  tasks.each { |t| yield t }
end

#to_file(file) ⇒ Object

Writes the list to a file

Parameters:

  • file (IO)

    an IO object



39
40
41
# File 'lib/todotxt/list.rb', line 39

def to_file(file)
  file.write(to_s)
end

#to_sObject



33
34
35
# File 'lib/todotxt/list.rb', line 33

def to_s
  map(&:to_s).join("\n")
end