Class: Rubdo::List

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ List

Returns a new instance of List.



15
16
17
18
# File 'lib/rubdo/list.rb', line 15

def initialize(options)
  @items = options.fetch(:items) { [] }
  @todo_file = options.fetch(:todo_file) { File.expand_path("~/.tasks/Todo.yml") }
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/rubdo/list.rb', line 7

def items
  @items
end

#todo_fileObject (readonly)

Returns the value of attribute todo_file.



7
8
9
# File 'lib/rubdo/list.rb', line 7

def todo_file
  @todo_file
end

Class Method Details

.read(file = nil) ⇒ Object



9
10
11
12
13
# File 'lib/rubdo/list.rb', line 9

def self.read(file = nil)
  todo_file = File.expand_path(file)
  items = File.exists?(todo_file) ? YAML.load_file(todo_file) : []
  { items: items, todo_file: todo_file }
end

Instance Method Details

#add(description) ⇒ Object Also known as: <<



20
21
22
# File 'lib/rubdo/list.rb', line 20

def add(description)
  items << Item.new(description)
end

#done(id) ⇒ Object



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

def done(id)
  items.delete_at id
end

#edit(id) ⇒ Object



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

def edit(id)
  items[id]
end

#to_aObject



38
39
40
# File 'lib/rubdo/list.rb', line 38

def to_a
  items
end

#writeObject Also known as: save



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

def write
  File.open(todo_file, 'w') { |f| f.write(items.to_yaml) }
end