Class: Pomo::List

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ List

Initialize with path.



18
19
20
21
22
# File 'lib/pomo/list.rb', line 18

def initialize path
  @path = File.expand_path path
  @tasks = []
  load rescue save
end

Instance Attribute Details

#pathObject (readonly)

List path.



8
9
10
# File 'lib/pomo/list.rb', line 8

def path
  @path
end

#tasksObject

Task array.



13
14
15
# File 'lib/pomo/list.rb', line 13

def tasks
  @tasks
end

Instance Method Details

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

Add task to the list in memory (requires saving).



68
69
70
# File 'lib/pomo/list.rb', line 68

def add task
  @tasks << task
end

#find(*args, &block) ⇒ Object

Find tasks by args, iterating with block.

Supports the following:

* n
* n n n
* n..n
* n..-n
* first
* last
* incomplete
* complete[d]
* all


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pomo/list.rb', line 40

def find *args, &block
  found = []
  found << tasks.first if args.empty?
  if args.include? 'all'
    found = tasks
  elsif args.include? 'first'
    found << tasks.first
  elsif args.include? 'last'
    found << tasks.last
  elsif args.include?('complete') || args.include?('completed')
    found = tasks.select { |task| task.complete? }
  elsif args.include? 'incomplete'
    found = tasks.select { |task| not task.complete? }
  elsif args.any? { |arg| arg =~ /(\d+)\.\.(-?\d+)/ }
    found = tasks[$1.to_i..$2.to_i]
  else
    tasks.each_with_index do |task, i|
      found << task if args.any? { |a| a.to_i == i } 
    end
  end
  found.each_with_index do |task, i|
    yield task, i
  end
end

#loadObject

Load the list.



86
87
88
89
# File 'lib/pomo/list.rb', line 86

def load
  @tasks = YAML.load_file path
  self
end

#saveObject

Save the list.



76
77
78
79
80
81
# File 'lib/pomo/list.rb', line 76

def save
  File.open(path, 'w') do |file|
    file.write YAML.dump(tasks)
  end
  self
end