Sha256: 7ff1aa528a3db5fc28ebf83b834d9fc2246ec0e1337c627fddbe8a123d1a95ec

Contents?: true

Size: 1.72 KB

Versions: 10

Compression:

Stored size: 1.72 KB

Contents

module Pomo
  class List
    
    ##
    # List path.
    
    attr_reader :path
    
    ##
    # Task array.
    
    attr_accessor :tasks
    
    ##
    # Initialize with _path_.
    
    def initialize path
      @path = File.expand_path path
      @tasks = []
      load rescue save
    end
    
    ##
    # Find tasks by _args_, iterating with _block_.
    #
    # Supports the following:
    #
    #  * n
    #  * n n n
    #  * n..n
    #  * n..-n
    #  * first
    #  * last
    #  * incomplete
    #  * complete[d]
    #  * all
    #
    
    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
    
    ##
    # Add _task_ to the list in memory (requires saving).
    
    def add task
      @tasks << task
    end
    alias :<< :add
    
    ##
    # Save the list.
    
    def save
      File.open(path, 'w') do |file|
        file.write YAML.dump(tasks)
      end
      self
    end
    
    ##
    # Load the list.
    
    def load
      @tasks = YAML.load_file path
      self
    end
    
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pomo-1.0.1 lib/pomo/list.rb
pomo-1.0.0 lib/pomo/list.rb
pomo-0.5.0 lib/pomo/list.rb
pomo-0.4.0 lib/pomo/list.rb
pomo-0.3.1 lib/pomo/list.rb
pomo-0.3.0 lib/pomo/list.rb
pomo-0.2.0 lib/pomo/list.rb
pomo-0.1.0 lib/pomo/list.rb
pomo-0.0.5 lib/pomo/list.rb
pomo-0.0.4 lib/pomo/list.rb