Class: Taskish

Inherits:
Object
  • Object
show all
Defined in:
lib/taskish.rb,
lib/taskish/app.rb,
lib/taskish/task.rb,
lib/taskish/project.rb,
lib/taskish/version.rb

Overview

:nodoc:

Defined Under Namespace

Classes: App, Project, Task

Constant Summary collapse

VERSION =
'0.4'

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Taskish

Returns a new instance of Taskish.

Yields:

  • (_self)

Yield Parameters:

  • _self (Taskish)

    the object that the method was called on



42
43
44
45
# File 'lib/taskish.rb', line 42

def initialize
  @projects = {}
  yield self if block_given?
end

Instance Method Details

#<<(project) ⇒ Object

Add project.

Raises:

  • (ArgumentError)


53
54
55
56
57
# File 'lib/taskish.rb', line 53

def <<(project)
  raise(ArgumentError, 'invalid project') if ( project.nil? || !project.kind_of?(Taskish::Project) )
  @projects[project.name] = project
  self
end

#[](name) ⇒ Object

Get project.



48
49
50
# File 'lib/taskish.rb', line 48

def [](name)
  @projects[name]
end

#debugObject



59
60
61
# File 'lib/taskish.rb', line 59

def debug
  @projects.values.each { |project| project.debug }
end

#doneObject



63
64
65
66
67
68
69
# File 'lib/taskish.rb', line 63

def done
  task_list = []
  @projects.each_pair do |name, project|
    project.tasks.each { |task| task_list << task if task.done? }
  end
  task_list
end

#due(due = :today) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/taskish.rb', line 71

def due( due = :today )
  task_list = []
  @projects.each_pair do |name, project|
    project.tasks.each { |task| task_list << task if ( task.due?(due) and !task.done? ) }
  end
  task_list.sort_by { |t| [ t.due, t.parent.to_s ] }
end

#parse(lines) ⇒ Object

TODO Move into “Taskish::Project” to properly handle child projects? #7



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/taskish.rb', line 80

def parse(lines)
  projects  = []
  lines.each_with_index do |l, idx|
    idx += 1
    l.chomp!
    if    l =~ /^(\S.+):$/                      # Project
      if project? $1
        projects = [ self[ $1 ] ]
        #warn "RE-USING PROJECT=#{ $1 }"
      else
        p = Taskish::Project.new( $1 ) unless project?( $1 )
        projects = [ p ]
        self.<<(p)
        #warn "PROJECT=#{ $1 }"
      end
    elsif l =~ /^(\s+)(\S+.+):$/                # Child Project
      if projects.empty?
        warn "skipping child project without a parent project - #{ $2 }"
        next
      end
      p = Taskish::Project.new( $2 )
      # TODO This is too ugly to be the best way of handling it.
      if projects[ $1.length ] == nil           # First child project at this depth
        projects[-1] << p
      else                                      # Subsequent child project (at some depth)
        while $1.length < ( projects.size + 1 ) # Go up the tree
          projects.pop
          projects.pop while projects.last.nil?
        end
        projects.last << p
      end
      projects[ $1.length ] = p
    elsif l =~ /^(\s+)\-\s+(.*)$/               # Task
      while $1.length < ( projects.size + 1 ) 
        projects.pop
        projects.pop while projects.last.nil? # TODO Dubious...
      end
      projects.last << Task.new( projects.last, $2 )
      # warn "\tPROJECT=#{ projects.last.name }\tTASK=#{ $2 }"
    elsif l =~ /^\S*$/
      next
    else
      warn "invalid line ##{idx} - (#{l})"
    end
  end  
end

#project?(name) ⇒ Boolean

Is project name defined?

Returns:

  • (Boolean)


128
129
130
# File 'lib/taskish.rb', line 128

def project?(name)
  @projects.key? name
end

#readlines(fn) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
# File 'lib/taskish.rb', line 132

def readlines(fn)
  raise(ArgumentError, 'file does not exist') if ( fn.nil? || !File.exist?(fn) )
  parse( File.open(fn).readlines )
  self
end