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).



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

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pomo/list.rb', line 48

def find(*args, &block)
  found = []
  found << tasks.first if args.empty?

  args.each do |arg|
    case arg
    when 'all'
      found = tasks
      break
    when 'first'
      found << tasks.first
    when 'last'
      found << tasks.last
    when 'complete'
      found.concat tasks.select { |task| task.complete? }
    when 'completed'
      found.concat tasks.select { |task| task.complete? }
    when 'incomplete'
      found.concat tasks.select { |task| not task.complete? }
    when /^(\d+)$/
      found << tasks[$1.to_i]
    when /^(\d+)\.\.(-?\d+)$/
      found.concat tasks[$1.to_i..$2.to_i]
    end
  end

  found.compact!

  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.



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

def load
  @tasks = YAML.load_file path
  self
end

#move(from, to) ⇒ Object

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



105
106
107
# File 'lib/pomo/list.rb', line 105

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

#runningObject

Find currently running task or nil.



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

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

#saveObject

Save the list.



112
113
114
115
116
117
# File 'lib/pomo/list.rb', line 112

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