class Todo

Attributes

items[R]

Public Class Methods

new() click to toggle source
# File lib/todo.rb, line 5
def initialize
        @tasks =  File.exist?(Dir.home + '/.todo_list') ? Marshal.load(File.read(Dir.home + '/.todo_list')) : []
end

Public Instance Methods

add(task) click to toggle source
# 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() click to toggle source
# File lib/todo.rb, line 22
def clear
        @tasks.clear
        save_items
end
remove(index) click to toggle source
# File lib/todo.rb, line 17
def remove(index)
        @tasks.delete_at(index - 1)
        save_items
end
to_s() click to toggle source
# 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

Private Instance Methods

save_items() click to toggle source
# File lib/todo.rb, line 41
def save_items
        File.open(Dir.home + '/.todo_list', "w") do |f|
                f.puts Marshal.dump(@tasks)
        end
end