Class: Taskish::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/taskish/task.rb

Overview

Taskish::Task - A single task

USAGE

Taskish::Task.new(project, task) do |task|
  ...
end

SEE ALSO

<Taskish>

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, task) {|_self| ... } ⇒ Task

Returns a new instance of Task.

Yields:

  • (_self)

Yield Parameters:

  • _self (Taskish::Task)

    the object that the method was called on

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'lib/taskish/task.rb', line 20

def initialize(project, task)
  raise(ArgumentError, 'invalid project') if ( project.nil? || !project.kind_of?(Taskish::Project) )
  @data   = Task.parse(task)
  @parent = project
  yield self if block_given?
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



18
19
20
# File 'lib/taskish/task.rb', line 18

def parent
  @parent
end

Class Method Details

.parse(task) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/taskish/task.rb', line 54

def self.parse(task)
  raise(ArgumentError, 'invalid task')    if ( task.nil?    || task.empty?    )
  data = { :raw => task }

  # TODO Extract to something that sucks less.
  task.gsub!(/\s*$/, '')
  # @done
  if    task =~ /^(.+?)\s+\@done\s?(.*)$/
    task        = "#{ $1 }#{ $3 }"
    data[:done] = true
  end
  # @due
  if    task =~ /^(.+?)\s+\@due(\S+)(.*)$/
    task        = "#{ $1 }#{ $3 }"
    due         = $2.gsub(/^\(/, '').gsub(/\)$/, '')
    data[:due]  = Date.parse(due)
  elsif task =~ /^(.+?)\s+\@due\s?(.*)$/
    task        = "#{ $1 }#{ $3 }"
    data[:due]  = Date.today
  end

  data[:task] = task
  data
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/taskish/task.rb', line 27

def done?
  @data.key?(:done) && @data[:done]
end

#dueObject



31
32
33
# File 'lib/taskish/task.rb', line 31

def due
  @data[:due] || nil
end

#due?(date) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/taskish/task.rb', line 35

def due?(date)
  return false  unless key? :due
  case  date
  when  :today
    return @data[:due] <= Date.today
  when  :tomorrow
    return @data[:due] > Date.today && @data[:due] <= ( Date.today + 1 )
  when  :week
    return @data[:due] <= ( Date.today + 7 )
  else
    warn "invalid due date specification - '#{date}'"
  end
  false
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/taskish/task.rb', line 50

def key?(key)
  @data.key? key
end

#rawObject



79
80
81
# File 'lib/taskish/task.rb', line 79

def raw
  @data[:raw]
end

#taskObject



83
84
85
# File 'lib/taskish/task.rb', line 83

def task
  @data[:task]
end

#to_sObject

TODO Make more flexible



88
89
90
91
92
93
94
# File 'lib/taskish/task.rb', line 88

def to_s
  s = sprintf( "%-25s\t%s" % [ self.parent.to_s + ':', @data[:task] ] )
  if key? :due
    s = sprintf( "%s @due(%s)" % [ s, @data[:due] ] )
  end 
  s
end