Class: Task::Task

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

Overview

This class represents a work task. A task gets a unique id from its TaskIDGenerator. Similar to the Person, the task holds a method #to_string for terminal output and #to_file for file output. Corresponding to the mechanism to read stored data (see FileReader) the Task also holds Task.create_from_attribute_list to create a new object from an Array of Strings.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = TaskIDGenerator.generate_new_id, start_time, end_time, description) ⇒ Task

initialization

Parameters:

  • id (Integer) (defaults to: TaskIDGenerator.generate_new_id)

    the unique id of the task

  • start_time (Time)

    the start date

  • end_time (Time)

    the end date

  • description (String)

    the description of the task



28
29
30
31
32
33
34
# File 'lib/entity/task.rb', line 28

def initialize(id=TaskIDGenerator.generate_new_id, start_time, end_time,
               description)
  @id = id
  @start_time = start_time
  @end_time = end_time
  @description = description
end

Instance Attribute Details

#descriptionString (readonly)

Returns the description of the task.

Returns:

  • (String)

    the description of the task



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

def description
  @description
end

#end_timeTime (readonly)

Returns the end date of the task.

Returns:

  • (Time)

    the end date of the task



17
18
19
# File 'lib/entity/task.rb', line 17

def end_time
  @end_time
end

#idInteger (readonly)

Returns the unique id of the task.

Returns:

  • (Integer)

    the unique id of the task



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

def id
  @id
end

#start_timeTime (readonly)

Returns the start date of the task.

Returns:

  • (Time)

    the start date of the task



15
16
17
# File 'lib/entity/task.rb', line 15

def start_time
  @start_time
end

Class Method Details

.check_list_size(list) ⇒ Object

singleton method to check for the correct list size

Parameters:

  • list (Array)

    with attributes

Raises:

  • (ArgumentError)

    if the size of the list does not fit the number of required attributes



71
72
73
74
75
76
# File 'lib/entity/task.rb', line 71

def self.check_list_size(list)
  if (list.size != 4)
    raise ArgumentError,
    " Error: list contains wrong number of arguments to create a task.".red
  end
end

.create_from_attribute_list(list) ⇒ Task

singleton method to create a Task::Task from a list

Parameters:

  • list (Array)

    list of string attributes to create a person

Returns:

  • (Task)

    a task initialized with the content of the given list



58
59
60
61
62
63
64
# File 'lib/entity/task.rb', line 58

def self.create_from_attribute_list(list)
  check_list_size(list)

  start_time = Time.strptime(list[2], "%FT%T%z")
  end_time = Time.strptime(list[3], "%FT%T%z")
  self.new(list[0].to_i, start_time, end_time, list[1])
end

Instance Method Details

#to_fileString

creates an output string for the storage in a file. The format servers the output format of the output file

Returns:

  • (String)

    a string coding all information of the task for storage

See Also:



49
50
51
52
53
# File 'lib/entity/task.rb', line 49

def to_file
  start_str = @start_time.iso8601
  end_str = @end_time.iso8601
  "#{@id};#{@description};#{start_str};#{end_str}"
end

#to_stringString

creates an output string with the attributes timeformat: ruby-doc.org/core-2.2.0/Time.html#method-i-subsec

Returns:

  • (String)

    output string for this task



39
40
41
42
43
# File 'lib/entity/task.rb', line 39

def to_string
  "Task: #{@description} \n with ID: #{@id} started at " \
  "#{@start_time.strftime("%F %R")} and was finished at " \
  "#{@end_time.strftime("%F %R")}."
end