Class: Todo
- Inherits:
-
Object
- Object
- Todo
- Defined in:
- lib/todo.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
- #add(task) ⇒ Object
- #clear ⇒ Object
-
#initialize ⇒ Todo
constructor
A new instance of Todo.
- #remove(index) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Todo
Returns a new instance of Todo.
5 6 7 |
# File 'lib/todo.rb', line 5 def initialize @tasks = File.exist?(Dir.home + '/.todo_list') ? Marshal.load(File.read(Dir.home + '/.todo_list')) : [] end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
9 10 11 |
# File 'lib/todo.rb', line 9 def items @items end |
Instance Method Details
#add(task) ⇒ Object
11 12 13 14 15 |
# File 'lib/todo.rb', line 11 def add(task) raise ArugmentError.new "Expecting add on type Task" unless task.kind_of? Task @tasks << task save_items end |
#clear ⇒ Object
22 23 24 25 |
# File 'lib/todo.rb', line 22 def clear @tasks.clear save_items end |
#remove(index) ⇒ Object
17 18 19 20 |
# File 'lib/todo.rb', line 17 def remove(index) @tasks.delete_at(index - 1) save_items end |
#to_s ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/todo.rb', line 27 def to_s if @tasks.empty? "No Current Tasks!" else tasks_string = "" @tasks.each_with_index do |item, index| tasks_string << " #{index + 1} - #{item}\n" end return tasks_string end end |