Class: Pomo::List
- Inherits:
-
Object
- Object
- Pomo::List
- Defined in:
- lib/pomo/list.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
List path.
-
#tasks ⇒ Object
Task array.
Instance Method Summary collapse
-
#add(task) ⇒ Object
(also: #<<)
Add task to the list in memory (requires saving).
-
#find(*args, &block) ⇒ Object
Find tasks by args, iterating with block.
-
#initialize(opts = {}) ⇒ List
constructor
Initialize with path.
-
#load ⇒ Object
Load the list.
-
#move(from, to) ⇒ Object
Move task at from index to to index (requres saving).
-
#running ⇒ Object
Find currently running task or nil.
-
#save ⇒ Object
Save the list.
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. path @tasks = [] load rescue save end |
Instance Attribute Details
#path ⇒ Object (readonly)
List path.
9 10 11 |
# File 'lib/pomo/list.rb', line 9 def path @path end |
#tasks ⇒ Object
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 |
#load ⇒ Object
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 |
#running ⇒ Object
Find currently running task or nil.
65 66 67 |
# File 'lib/pomo/list.rb', line 65 def running tasks.detect {|task| task.running? } end |
#save ⇒ Object
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 |