Class: R2do::Task

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

Constant Summary collapse

MAX_LENGTH =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description) ⇒ Task

Creates a new instance of a Task

Parameters:

  • desctiption (String)

    the description for this task



32
33
34
35
36
37
38
39
40
41
# File 'lib/r2do/task.rb', line 32

def initialize(description)
  if description.length > MAX_LENGTH
    raise ArgumentError, "A task description has to be less than 30 characters."
  end

  @description = description
  @done = false
  @date_created = DateTime.now
  @date_done = nil
end

Instance Attribute Details

#date_createdDateTime

Returns the date and time of creation.

Returns:

  • (DateTime)

    the date and time of creation



25
26
27
# File 'lib/r2do/task.rb', line 25

def date_created
  @date_created
end

#date_doneDateTime

Returns the date and time of completion.

Returns:

  • (DateTime)

    the date and time of completion.



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

def date_done
  @date_done
end

#descriptionString

Returns the description for this task.

Returns:

  • (String)

    the description for this task.



21
22
23
# File 'lib/r2do/task.rb', line 21

def description
  @description
end

Instance Method Details

#completedDateTime

Flags the specific task as completed.

Returns:

  • (DateTime)

    the date and time of completion.



61
62
63
64
# File 'lib/r2do/task.rb', line 61

def completed()
  @done = true
  @date_done = DateTime.now
end

#displayString

Returns information regarding the state of this task.

Returns:

  • (String)

    the metadata for this task.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/r2do/task.rb', line 84

def display()
  date = format_date(@date_created)

  result = StringIO.new

  result << "Selected task:\n"
  result << "   %s\n\n" % @description
  result << "Created:\n"
  result << "   %s" % date

  if done?
    result << "\nCompleted:\n"
    result << "   %s" % format_date(@date_done)
  end

  return result.string
end

#done?bool

Gets the completed status of the specific task.

Returns:

  • (bool)

    true if the task is completed.



54
55
56
# File 'lib/r2do/task.rb', line 54

def done?()
  return @done
end

#format_date(date) ⇒ String

Formats the date

Parameters:

  • date (DateTime)

    the date to parse

Returns:

  • (String)

    the formatted date



106
107
108
# File 'lib/r2do/task.rb', line 106

def format_date(date)
  date.strftime('%a %b %e, %Y')
end

#rename(description) ⇒ void

This method returns an undefined value.

Renames this Task

Parameters:

  • description (String)

    the new value for the task



47
48
49
# File 'lib/r2do/task.rb', line 47

def rename(description)
  @description = description
end

#to_sString

Returns a string representation of this Task

Returns:

  • (String)

    the representation of this Task



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/r2do/task.rb', line 69

def to_s()
  completed = ' '
  date = ''

  if done?
    completed = 'x'
    date = format_date(@date_done)
  end

  return "[%s] %-30s %s" % [completed, @description, date]
end