class Todo::List

Constants

DIR_PATH
FILE_PATH

Attributes

lists[R]
name[R]
tasks[R]

Public Class Methods

load() click to toggle source
# File lib/todo/list.rb, line 60
def self.load
  if File.exists?(FILE_PATH)
    YAML.load(File.open(FILE_PATH)) 
  else
    Dir.mkdir(DIR_PATH) unless Dir.exists?(DIR_PATH)
    self.new(persistent: true)
  end
end
new(list_name=nil, persistent:false) click to toggle source
# File lib/todo/list.rb, line 9
          def initialize(list_name=nil, persistent:false)
                  @tasks = []
                  @name = list_name || 'undefined'
                  @lists = []
@persistent = persistent
          end

Public Instance Methods

access_list(list_name) click to toggle source
# File lib/todo/list.rb, line 44
def access_list(list_name)
        list_with_name(list_name)
end
add(name:,**options) click to toggle source
# File lib/todo/list.rb, line 16
          def add(name:,**options)
                  if options[:list]
                          add_to_list(name, options)
                  else
                          add_task(name, options)
                  end
update
          end
all(options={}) click to toggle source
# File lib/todo/list.rb, line 36
def all(options={})
        @tasks
end
finish(name) click to toggle source
# File lib/todo/list.rb, line 25
def finish(name)
  @tasks.each { |t| t.finish! if t.name == name }
  update
end
remove(name) click to toggle source
# File lib/todo/list.rb, line 30
          def remove(name)
                  @tasks.delete_if{|t| t.name == name}
update
                  all
          end
to_h() click to toggle source
# File lib/todo/list.rb, line 48
        def to_h
                {
name:  name,
                        tasks: @tasks.map{ |t| t.to_h },
                        lists: @lists.map{ |l| l.to_h }
                }
        end
type() click to toggle source
# File lib/todo/list.rb, line 56
def type
        "list"
end

Private Instance Methods

add_task(name, options) click to toggle source
# File lib/todo/list.rb, line 98
def add_task(name, options)
  @tasks << Todo::ListItem.new(name: name, priority: options[:priority])
end
add_to_list(name, options) click to toggle source
# File lib/todo/list.rb, line 102
def add_to_list(name, options)
        list = create_or_find(options[:list])
        options.delete(:list)
        list.add(name: name, priority: options[:priority])   
end
create_or_find(list_name) click to toggle source
# File lib/todo/list.rb, line 82
def create_or_find(list_name)
        list = list_with_name(list_name)
        if list
                list
        else
                list = Todo::List.new(list_name)
                @lists << list
                list
        end
end
list_with_name(list_name) click to toggle source
# File lib/todo/list.rb, line 93
def list_with_name(list_name)
        index = @lists.find_index{ |l| l.name == list_name }
        index ? @lists[index] : index
end
save() click to toggle source
# File lib/todo/list.rb, line 75
def save
  return unless @name == "undefined"
  File.open(FILE_PATH, "w") do |f|
    f.write YAML.dump(self)
  end
end
update() click to toggle source
# File lib/todo/list.rb, line 71
def update
  save if @persistent
end