Class: R2do::Category
- Inherits:
-
Object
- Object
- R2do::Category
- Defined in:
- lib/r2do/category.rb
Instance Attribute Summary collapse
-
#current_task ⇒ Task
The current task.
-
#name ⇒ String
The name of this category.
-
#tasks ⇒ Array
The tasks this category contains.
Instance Method Summary collapse
-
#add(task) ⇒ void
Adds the object into the specific Category.
-
#clear_current_task ⇒ void
Clears the current task.
-
#find_by_description(description) ⇒ Task
Finds a task based on the description.
-
#initialize(name) ⇒ Category
constructor
Creates a new instance of a Category.
-
#remove(task) ⇒ void
Removes the object from the specific Category.
- #rename(name) ⇒ Object
-
#set_current(task) ⇒ void
Sets a Task as the current one.
-
#to_s ⇒ String
Returns a string representation of this Category.
Constructor Details
#initialize(name) ⇒ Category
Creates a new instance of a 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_task ⇒ Task
Returns the current task.
25 26 27 |
# File 'lib/r2do/category.rb', line 25 def current_task @current_task end |
#name ⇒ String
Returns the name of this category.
21 22 23 |
# File 'lib/r2do/category.rb', line 21 def name @name end |
#tasks ⇒ Array
Returns 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.
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_task ⇒ void
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.
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.
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.
72 73 74 |
# File 'lib/r2do/category.rb', line 72 def set_current(task) @current_task = task end |
#to_s ⇒ String
Returns a string 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 |