Class: Syctask::TaskService
- Inherits:
-
Object
- Object
- Syctask::TaskService
- Defined in:
- lib/syctask/task_service.rb
Overview
Provides services to operate tasks as create, read, find, update and save Task objects
Constant Summary collapse
- DEFAULT_DIR =
Default directory where the tasks are saved to if no directory is specified
Syctask::WORK_DIR
Instance Method Summary collapse
-
#create(dir, options, title) ⇒ Object
Creates a new task in the specified directory, with the specified options and the specified title.
-
#delete(dir, filter) ⇒ Object
Deletes tasks in the specified directory that match the provided filter.
-
#find(dir, filter = {}, all = true) ⇒ Object
Finds all tasks that match the given filter.
-
#read(dir, id) ⇒ Object
Reads the task with given ID id located in given directory dir.
-
#read_by_id(id) ⇒ Object
Reads the task identified by ID.
-
#save(dir, task) ⇒ Object
Saves the task to the task directory.
-
#update(dir, id, options) ⇒ Object
Updates the task with the given id in the given directory dir with the provided options.
Instance Method Details
#create(dir, options, title) ⇒ Object
Creates a new task in the specified directory, with the specified options and the specified title. If the directory doesn’t exist it is created. When the task is created it is assigned a unique ID within the directory. Options are
-
description - additional information about the task
-
follow_up - follow-up date of the task
-
due - due date of the task
-
prio - priority of the task
-
note - information about the progress or state of the task
-
tags - can be used to searching tasks that belong to a certain category
25 26 27 28 29 30 31 |
# File 'lib/syctask/task_service.rb', line 25 def create(dir, , title) create_dir(dir) task = Task.new(, title, next_id(dir)) save(dir, task) Syctask::log_task("create", task) task.id end |
#delete(dir, filter) ⇒ Object
Deletes tasks in the specified directory that match the provided filter. If no filter is provide no task is deleted. The count of deleted tasks is returned
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/syctask/task_service.rb', line 116 def delete(dir, filter) deleted = 0 Dir.glob("#{dir}/*.task").each do |file| begin File.file?(file) ? task = YAML.load_file(file) : next rescue Exception => e next # If the file is no task but read by YAML ignore it end next unless not task.nil? and task.class == Syctask::Task if task.matches?(filter) deleted += File.delete(file) ids = File.read(Syctask::IDS) File.write(Syctask::IDS, ids.gsub("#{task.id},#{file}","")) Syctask::log_task("delete", task) end end deleted end |
#find(dir, filter = {}, all = true) ⇒ Object
Finds all tasks that match the given filter. The filter can be provided for :id, :title, :description, :follow_up, :due, :tags and :prio. id can be eather a selection of IDs ID1,ID2,ID3 or a comparison <|=|>ID. title and :description can be a REGEX as /look for d+ examples/ follow-up and :due can be <|=|>DATE tags can be eather a selection TAG1,TAG2,TAG3 or a REGEX /[Ll]ecture/ prio can be <|=|>PRIO dir is the directory where find looks for tasks all specifies whether to consider also completed tasks (default) or only open tasks
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/syctask/task_service.rb', line 70 def find(dir, filter={}, all=true) tasks = [] Dir.glob("#{dir}/*.task").sort.each do |file| begin File.file?(file) ? task = YAML.load_file(file) : next rescue Exception => e next # If the file is no task but read by YAML ignore it end next unless not task.nil? and task.class == Syctask::Task next if not all and task.done? tasks << task if task.matches?(filter) end tasks.sort end |
#read(dir, id) ⇒ Object
Reads the task with given ID id located in given directory dir. If task does not exist nil is returned otherwise the task is returned
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/syctask/task_service.rb', line 35 def read(dir, id) task = read_by_id(id) return task unless task.nil? #task = nil Dir.glob("#{dir}/*.task").each do |file| task = YAML.load_file(file) if File.file? file if not task.nil? and task.class == Syctask::Task and task.id == id.to_i return task end end nil end |
#read_by_id(id) ⇒ Object
Reads the task identified by ID. If no task with ID is found nil is returned otherwise the task. Note: This method might return nil even though the task exists. You should always use #read instead.
52 53 54 55 56 57 58 |
# File 'lib/syctask/task_service.rb', line 52 def read_by_id(id) return nil unless File.exists? Syctask::IDS ids = File.read(Syctask::IDS) entry = ids.scan(/(^#{id}),(.*\n)/)[0] return YAML.load_file(entry[1].chomp) if entry return nil end |
#save(dir, task) ⇒ Object
Saves the task to the task directory. If dir is nil the default dir ~/.tasks will be set.
137 138 139 140 141 142 143 144 |
# File 'lib/syctask/task_service.rb', line 137 def save(dir, task) task.dir = dir.nil? ? DEFAULT_DIR : File.(dir) task_file = "#{task.dir}/#{task.id}.task" unless File.exists? task_file File.open(Syctask::IDS, 'a') {|f| f.puts "#{task.id},#{task_file}"} end File.open(task_file, 'w') {|f| YAML.dump(task, f)} end |
#update(dir, id, options) ⇒ Object
Updates the task with the given id in the given directory dir with the provided options. Options are
-
description - additional information about the task
-
follow_up - follow-up date of the task
-
due - due date of the task
-
prio - priority of the task
-
note - information about the progress or state of the task
-
tags - can be used to searching tasks that belong to a certain category
Except for note and tags the values of the task are overridden with the new value. If note and tags are provided these are added to the existing values.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/syctask/task_service.rb', line 97 def update(dir, id, ) task = read_by_id(id) unless task task_file = Dir.glob("#{dir}/#{id}.task")[0] task = YAML.load_file(task_file) if task_file end updated = false if task task.update() save(task.dir, task) Syctask::log_task("update", task) updated = true end updated end |