Class: R2do::Category

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Category

Creates a new instance of a Category

Parameters:

  • name (String)

    the name for this category



31
32
33
34
35
# File 'lib/r2do/category.rb', line 31

def initialize(name)
  @name = name
  @tasks = Array.new
  @current_task = nil
end

Instance Attribute Details

#current_taskTask

Returns the current task.

Returns:

  • (Task)

    the current task



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

def current_task
  @current_task
end

#nameString

Returns the name of this category.

Returns:

  • (String)

    the name of this category.



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

def name
  @name
end

#tasksArray

Returns the tasks this category contains.

Returns:

  • (Array)

    the tasks this category contains.



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

def tasks
  @tasks
end

Instance Method Details

#add(task) ⇒ void

This method returns an undefined value.

Adds the object into the specific Category.

Parameters:

  • task (Task)

    the task to add.

Raises:

  • (ArgumentError)

    if task is nil.

  • (Exceptions::TaskAlreadyExistsError)

    if task with same description is already present.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/r2do/category.rb', line 55

def add(task)
  if task.nil?
    raise ArgumentError
  end

  duplicate = @tasks.find { |t| t.description == task.description }
  if duplicate
    raise TaskAlreadyExistsError
  end

  @tasks.push(task)
end

#clear_current_taskvoid

This method returns an undefined value.

Clears the current task



79
80
81
# File 'lib/r2do/category.rb', line 79

def clear_current_task()
  @current_task = nil
end

#find_by_description(description) ⇒ Task

Finds a task based on the description.

Parameters:

  • description (String)

    the task description.

Returns:

  • (Task)

    the task identified by description.



45
46
47
# File 'lib/r2do/category.rb', line 45

def find_by_description(description)
  @tasks.find { |t| t.description == description }
end

#remove(task) ⇒ void

This method returns an undefined value.

Removes the object from the specific Category.

Parameters:

  • task (Task)

    the task to remove.

Raises:

  • (Exceptions::TaskNotFoundError)

    if task is not found.



88
89
90
# File 'lib/r2do/category.rb', line 88

def remove(task)
  @tasks.delete(task) { raise TaskNotFoundError.new() }
end

#rename(name) ⇒ Object



37
38
39
# File 'lib/r2do/category.rb', line 37

def rename(name)
  @name = name
end

#set_current(task) ⇒ void

This method returns an undefined value.

Sets a Task as the current one.

Parameters:

  • category (Task)

    the category to be set as current.



72
73
74
# File 'lib/r2do/category.rb', line 72

def set_current(task)
  @current_task = task
end

#to_sString

Returns a string representation of this Category

Returns:

  • (String)

    the representation of this Category



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/r2do/category.rb', line 95

def to_s()
  count = 0
  result = StringIO.new

  result << "%s:\n\n" % [@name]
  result << "    %-30s     %s\n" % ["Task", "Completed"]
  result << "    " << "-"*51
  result << "\n"

  @tasks.each do | task |
    count += 1
    result << "    %s\n" % task
  end

  return result.string
end