Class: Ptf::MetadataFile
- Inherits:
-
Object
- Object
- Ptf::MetadataFile
- Defined in:
- lib/ptf/metadata_file.rb
Overview
Represents a single Metadata file.
Class Method Summary collapse
-
.create_from_file(filepath) ⇒ Ptf::MetadataFile
Create a MetadataFile object from the given filepath.
-
.create_from_input(title, group, due_date, estimate, id) ⇒ Ptf::MetadataFile
Create a MetadataFile object from the given input.
Instance Method Summary collapse
-
#add_content(key, val) ⇒ Object
Add content to the metadata file.
-
#complete_now ⇒ Object
Completes the task.
-
#completed_at ⇒ DateTime
Returns the date and time that the task was completed at.
-
#completed_at_list_format ⇒ String
Returns the completed at time of the task for the ptf list command.
-
#completed_at_str ⇒ String
Returns the date and time that the task was completed at as a String.
-
#created_at ⇒ DateTime
Returns the date and time that the task was created at.
-
#created_at_str ⇒ String
Returns the date and time that the task was created at as a String.
-
#due_date ⇒ DateTime?
Returns the due date of the task.
-
#due_date_list_format ⇒ String
Returns the due date of the task for the ptf list command.
-
#due_date_str ⇒ String
Returns the due date of the task as a String.
-
#estimate ⇒ Integer
Returns the estimated time to complete the task.
-
#file_string ⇒ String
Returns the data in the file as a String.
-
#group ⇒ Ptf::Group
Returns the group the task is associated with.
-
#hash ⇒ String
Return the hash of the task.
-
#id ⇒ Integer
Return the id of the task.
-
#initialize(filepath, data) ⇒ MetadataFile
constructor
Initialize a new metadata file.
- #key_val_to_str(key, val) ⇒ Object
-
#list_format(datetime) ⇒ String
Returns the given datetime in list format.
- #reopen ⇒ Object
-
#set_due_date(new_date) ⇒ Object
Set the task due’s date.
-
#set_estimate(new_estimate) ⇒ Object
Set the task’s estiamted time.
-
#set_title(new_title) ⇒ Object
Set the task’s title to a new title.
-
#title ⇒ String
Returns the title of the task.
-
#write_to_file ⇒ Object
Writes the metadata file.
Constructor Details
#initialize(filepath, data) ⇒ MetadataFile
Initialize a new metadata file.
88 89 90 91 |
# File 'lib/ptf/metadata_file.rb', line 88 def initialize(filepath, data) @filepath = filepath @data = data end |
Class Method Details
.create_from_file(filepath) ⇒ Ptf::MetadataFile
Create a MetadataFile object from the given filepath.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ptf/metadata_file.rb', line 44 def self.create_from_file(filepath) raise ArgumentError, "File #{filepath} does not exist." unless Ptf::FileSystem.file_exist?(filepath) data = {} # parse file File.open(filepath, "r") do |f| f.each_line do |l| key, val = l.gsub(/\s+/, "").split(":") if key.nil? || !(key.is_a? String) raise ArgumentError, "Error parsing file." end # Convert values to appropriate types case key when "group" val = Ptf::Group.from_name(val) when "due_date", "created_at", "completed_at" val = (val.nil? ? val : Ptf::Date.str_to_datetime(val)) when "id" val = val.to_i when "estimate" val = (val.nil? ? nil : val.to_i) end data[key.to_sym] = val end end return self.new(filepath, data) end |
.create_from_input(title, group, due_date, estimate, id) ⇒ Ptf::MetadataFile
Create a MetadataFile object from the given input.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ptf/metadata_file.rb', line 17 def self.create_from_input(title, group, due_date, estimate, id) data = { :title => title, :group => group, :due_date => due_date, :estimate => estimate, :id => id, :created_at => DateTime.now, :completed_at => nil } # Get the filepath for the infofile open_dir = Ptf::FileSystem. group_dir = File.join(open_dir, group.name) filepath = File.join(group_dir, id.to_s) self.new(filepath, data) end |
Instance Method Details
#add_content(key, val) ⇒ Object
Add content to the metadata file.
99 100 101 102 103 104 |
# File 'lib/ptf/metadata_file.rb', line 99 def add_content(key, val) raise ArgumentError, "The key #{key} is not a symbol." unless key.is_a? Symbol raise ArgumentError, "The key #{key} already exists." unless @data[key].nil? @data[key] = val end |
#complete_now ⇒ Object
Completes the task.
Fills in the completed_at field with the current DateTime. Removes the metadata file from the in progress directory and writes it to the completed directory.
249 250 251 252 253 254 255 256 |
# File 'lib/ptf/metadata_file.rb', line 249 def complete_now @data[:completed_at] = DateTime.now FileUtils.rm @filepath @filepath = File.join(File.join(Ptf::FileSystem., group.name), id.to_s) write_to_file end |
#completed_at ⇒ DateTime
Returns the date and time that the task was completed at.
211 212 213 |
# File 'lib/ptf/metadata_file.rb', line 211 def completed_at @data[:completed_at] end |
#completed_at_list_format ⇒ String
Returns the completed at time of the task for the ptf list command.
225 226 227 228 229 |
# File 'lib/ptf/metadata_file.rb', line 225 def completed_at_list_format return "" if completed_at.nil? list_format completed_at end |
#completed_at_str ⇒ String
Returns the date and time that the task was completed at as a String.
218 219 220 |
# File 'lib/ptf/metadata_file.rb', line 218 def completed_at_str Ptf::Date.datetime_to_str completed_at end |
#created_at ⇒ DateTime
Returns the date and time that the task was created at.
197 198 199 |
# File 'lib/ptf/metadata_file.rb', line 197 def created_at @data[:created_at] end |
#created_at_str ⇒ String
Returns the date and time that the task was created at as a String.
204 205 206 |
# File 'lib/ptf/metadata_file.rb', line 204 def created_at_str Ptf::Date.datetime_to_str create_at end |
#due_date ⇒ DateTime?
Returns the due date of the task.
134 135 136 |
# File 'lib/ptf/metadata_file.rb', line 134 def due_date @data[:due_date] end |
#due_date_list_format ⇒ String
Returns the due date of the task for the ptf list command.
170 171 172 173 174 |
# File 'lib/ptf/metadata_file.rb', line 170 def due_date_list_format return "" if due_date.nil? list_format due_date end |
#due_date_str ⇒ String
Returns the due date of the task as a String.
141 142 143 144 145 |
# File 'lib/ptf/metadata_file.rb', line 141 def due_date_str return "" if due_date.nil? Ptf::Date.datetime_to_str(due_date) end |
#estimate ⇒ Integer
Returns the estimated time to complete the task.
179 180 181 |
# File 'lib/ptf/metadata_file.rb', line 179 def estimate @data[:estimate] end |
#file_string ⇒ String
Returns the data in the file as a String.
280 281 282 283 284 285 286 287 |
# File 'lib/ptf/metadata_file.rb', line 280 def file_string return_str = "" @data.each do |key, val| return_str += "#{key_val_to_str(key, val)}\n" end return_str end |
#group ⇒ Ptf::Group
Returns the group the task is associated with.
127 128 129 |
# File 'lib/ptf/metadata_file.rb', line 127 def group @data[:group] end |
#hash ⇒ String
Return the hash of the task.
241 242 243 |
# File 'lib/ptf/metadata_file.rb', line 241 def hash @data[:hash] end |
#id ⇒ Integer
Return the id of the task.
234 235 236 |
# File 'lib/ptf/metadata_file.rb', line 234 def id @data[:id] end |
#key_val_to_str(key, val) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/ptf/metadata_file.rb', line 265 def key_val_to_str(key, val) str_val = val case key when :group str_val = val.name when :due_date, :created_at, :completed_at str_val = (val.nil? ? "" : Ptf::Date.datetime_to_str(val)) end "#{key}:#{str_val}" end |
#list_format(datetime) ⇒ String
Returns the given datetime in list format
163 164 165 |
# File 'lib/ptf/metadata_file.rb', line 163 def list_format(datetime) datetime.strftime("%a %b %_d - %l%p -") end |
#reopen ⇒ Object
258 259 260 261 262 263 |
# File 'lib/ptf/metadata_file.rb', line 258 def reopen FileUtils.rm @filepath @filepath = File.join(File.join(Ptf::FileSystem., group.name), id.to_s) write_to_file end |
#set_due_date(new_date) ⇒ Object
Set the task due’s date.
152 153 154 155 156 |
# File 'lib/ptf/metadata_file.rb', line 152 def set_due_date(new_date) raise ArgumentError, "Invalid new due date." unless (new_date.nil? || new_date.is_a?(DateTime)) @data[:due_date] = new_date end |
#set_estimate(new_estimate) ⇒ Object
Set the task’s estiamted time.
188 189 190 191 192 |
# File 'lib/ptf/metadata_file.rb', line 188 def set_estimate(new_estimate) raise ArgumentError, "The new estimate, #{new_estimate.to_s}, is not an integer." unless (new_estimate.nil? || new_estimate.is_a?(Integer)) @data[:estimate] = new_estimate end |
#set_title(new_title) ⇒ Object
Set the task’s title to a new title.
118 119 120 121 122 |
# File 'lib/ptf/metadata_file.rb', line 118 def set_title(new_title) raise ArgumentError, "The new title must be a string. Recieved a #{new_title.class.name}" unless new_title.is_a?(String) @data[:title] = new_title end |
#title ⇒ String
Returns the title of the task.
109 110 111 |
# File 'lib/ptf/metadata_file.rb', line 109 def title @data[:title] end |
#write_to_file ⇒ Object
Writes the metadata file. Overwrites any previous metadata file for the same task.
290 291 292 293 294 295 296 297 |
# File 'lib/ptf/metadata_file.rb', line 290 def write_to_file file = File.new(@filepath, "w") @data.each do |key, val| file.puts "#{key_val_to_str(key, val)}" end file.close end |