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>

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



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

def initialize(project, task)
  @data = Task.parse(project, task)
  yield self if block_given?
end

Class Method Details

.parse(project, task) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/taskish/task.rb', line 50

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

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


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

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

#dueObject



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

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

#due?(date) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/taskish/task.rb', line 31

def due?(date)
  return false  unless key? :due
  case  date
  when  :today
    return @data[:due] <= Date.today
  when  :week
    d     = Date.today + 7
    week  = DateTime.new( d.year, d.mon, d.mday, 23, 59 )
    return @data[:due] <= week
  else
    warn "invalid due date specification - '#{date}'"
  end
  false
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/taskish/task.rb', line 46

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

#taskObject



76
77
78
# File 'lib/taskish/task.rb', line 76

def task
  @data[:task]
end

#to_sObject



80
81
82
83
84
85
86
# File 'lib/taskish/task.rb', line 80

def to_s
  s = sprintf( "%-20s\t%s" % [ @data[:project], @data[:task] ] )
  if key? :due
    s = sprintf( "%s @due(%s)" % [ s, @data[:due] ] )
  end 
  s
end