Sha256: ab01b829737ac7ad858621f68832332785524b69d2a90b68333506d477fe9e5b
Contents?: true
Size: 1.95 KB
Versions: 1
Compression:
Stored size: 1.95 KB
Contents
require_relative "list_item" require 'yaml' module Todo class List DIR_PATH = File.join(Dir.home, "todo") FILE_PATH = File.join(DIR_PATH, "todo.yml") attr_reader :tasks, :lists, :name def initialize(list_name=nil, persistent:false) @tasks = [] @name = list_name || 'undefined' @lists = [] @persistent = persistent end def add(name:,**options) if options[:list] add_to_list(name, options) else add_task(name, options) end update end def finish(name) @tasks.each { |t| t.finish! if t.name == name } update end def remove(name) @tasks.delete_if{|t| t.name == name} update all end def all(options={}) @tasks end def lists @lists.map{ |l| l.name } end def access_list(list_name) list_with_name(list_name) end def to_h { name: name, tasks: @tasks.map{ |t| t.to_h }, lists: @lists.map{ |l| l.to_h } } end def type "list" end 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 private def update save if @persistent end def save return unless @name == "undefined" File.open(FILE_PATH, "w") do |f| f.write YAML.dump(self) end end 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 def list_with_name(list_name) index = @lists.find_index{ |l| l.name == list_name } index ? @lists[index] : index end def add_task(name, options) @tasks << Todo::ListItem.new(name: name, priority: options[:priority]) end def add_to_list(name, options) list = create_or_find(options[:list]) options.delete(:list) list.add(name: name, priority: options[:priority]) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
2do-0.0.2 | lib/todo/list.rb |