class R2do::Category

Attributes

current_task[RW]

@return [Task] the current task

name[RW]

@return [String] the name of this category.

tasks[RW]

@return [Array] the tasks this category contains.

Public Class Methods

new(name) click to toggle source

Creates a new instance of a Category

@param [String] name the name for this category

# File lib/r2do/category.rb, line 31
def initialize(name)
  @name = name
  @tasks = Array.new
  @current_task = nil
end

Public Instance Methods

add(task) click to toggle source

Adds the object into the specific Category.

@param [Task] task the task to add. @raise [ArgumentError] if task is nil. @raise [Exceptions::TaskAlreadyExistsError] if task with same description is already present. @return [void]

# 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_task() click to toggle source

Clears the current task

@return [void]

# File lib/r2do/category.rb, line 79
def clear_current_task()
  @current_task = nil
end
find_by_description(description) click to toggle source

Finds a task based on the description.

@param [String] description the task description. @return [Task] the task identified by description.

# File lib/r2do/category.rb, line 45
def find_by_description(description)
  @tasks.find { |t| t.description == description }
end
remove(task) click to toggle source

Removes the object from the specific Category.

@param [Task] task the task to remove. @raise [Exceptions::TaskNotFoundError] if task is not found. @return [void]

# File lib/r2do/category.rb, line 88
def remove(task)
  @tasks.delete(task) { raise TaskNotFoundError.new() }
end
rename(name) click to toggle source
# File lib/r2do/category.rb, line 37
def rename(name)
  @name = name
end
set_current(task) click to toggle source

Sets a Task as the current one.

@param [Task] category the category to be set as current. @return [void]

# File lib/r2do/category.rb, line 72
def set_current(task)
  @current_task = task
end
to_s() click to toggle source

Returns a string representation of this Category

@return [String] the representation of this Category

# 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