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(opts = {}) ⇒ List

Initialize with path.



19
20
21
22
23
24
25
26
27
28
# File 'lib/pomo/list.rb', line 19

def initialize(opts = {})
  if opts[:init]
    path = '.pomo'
  else
    path = (File.exists?('.pomo') && ENV['POMO_ENV']!='test') ? '.pomo' : File.join(ENV['HOME'],'.pomo')
  end
  @path = File.expand_path path
  @tasks = []
  load rescue save
end

Instance Attribute Details

#pathObject (readonly)

List path.



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

def path
  @path
end

#tasksObject

Task array.



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

def tasks
  @tasks
end

Instance Method Details

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

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



72
73
74
# File 'lib/pomo/list.rb', line 72

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
* n..n n..n n..n
* first
* last
* first last
* incomplete
* complete[d]
* all


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pomo/list.rb', line 48

def find(*args, &block)
  found = find_tasks(args)

  if block.arity == 2
    found.each_with_index do |task, i|
      yield task, i
    end
  else
    found.each do |task|
      yield task
    end
  end
end

#loadObject

Load the list.



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

def load
  @tasks = YAML.load_file path
  self
end

#move(from, to) ⇒ Object

Move task at from index to to index (requres saving).



80
81
82
# File 'lib/pomo/list.rb', line 80

def move(from, to)
  @tasks.insert(index(to), @tasks.delete_at(index(from)))
end

#runningObject

Find currently running task or nil.



65
66
67
# File 'lib/pomo/list.rb', line 65

def running
  tasks.detect {|task| task.running? }
end

#saveObject

Save the list.



87
88
89
90
91
92
# File 'lib/pomo/list.rb', line 87

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