Class: Ptf::MetadataFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ptf/metadata_file.rb

Overview

Represents a single Metadata file.

Author:

  • Austin Blatt

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, data) ⇒ MetadataFile

Initialize a new metadata file.

Parameters:

  • filepath (String)

    the full path to the file.

  • data (Hash)

    the data in the file.

Options Hash (data):

  • :title (String)

    the task’s title.

  • :group (Ptf::Group)

    the Group the task is associated with.

  • :due_date (DateTime, nil)

    the due date given for the task.

  • :estimate (Integer, nil)

    the estimated time to complete the task.

  • :id (Integer)

    the unique ID number for this task.

  • :created_at (DateTime)

    the time the task was created.

  • :completed_at (DateTime, nil)

    the time the task was completed (nil if it has not been completed).

  • :hash (String)

    the file hash (the filepath of the data file).

Since:

  • 0.1.0



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.

Parameters:

  • filepath (String)

    the full file path to the metadata file.

Returns:

Raises:

  • (ArgumentError)

    if the given filepath does not exist.

  • (ArgumentError)

    if the given file is not a metadata file.

Since:

  • 0.1.0



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.

Parameters:

  • title (String)

    the task’s title.

  • group (Ptf::Group)

    the Group the task is associated with.

  • due_date (DateTime, nil)

    the due date given for the task.

  • estimate (Integer, nil)

    the estimated time to complete the task.

  • id (Integer)

    the unique ID number for this task.

Returns:

Since:

  • 0.1.0



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.

Parameters:

  • key (Symbol)

    the key for the new content.

  • val (Object)

    the value for the new content.

Raises:

  • (ArgumentError)

    if the key is not a symbol.

Since:

  • 0.1.0



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_nowObject

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.

Since:

  • 0.1.0



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_atDateTime

Returns the date and time that the task was completed at.

Returns:

  • (DateTime)

    the date and time that the task was completed at.

Since:

  • 0.1.0



211
212
213
# File 'lib/ptf/metadata_file.rb', line 211

def completed_at
  @data[:completed_at]
end

#completed_at_list_formatString

Returns the completed at time of the task for the ptf list command.

Returns:

  • (String)

    the completed at time in the form ‘Mon Dec 19 - 11AM -’ or ‘Mon Dec 9 - 2PM -’.

Since:

  • 0.1.0



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_strString

Returns the date and time that the task was completed at as a String.

Returns:

  • (String)

    the date and time represented as a string.

Since:

  • 0.1.0



218
219
220
# File 'lib/ptf/metadata_file.rb', line 218

def completed_at_str
  Ptf::Date.datetime_to_str completed_at
end

#created_atDateTime

Returns the date and time that the task was created at.

Returns:

  • (DateTime)

    the date and time that the task was created at.

Since:

  • 0.1.0



197
198
199
# File 'lib/ptf/metadata_file.rb', line 197

def created_at
  @data[:created_at]
end

#created_at_strString

Returns the date and time that the task was created at as a String.

Returns:

  • (String)

    the date and time represented as a string.

Since:

  • 0.1.0



204
205
206
# File 'lib/ptf/metadata_file.rb', line 204

def created_at_str
  Ptf::Date.datetime_to_str create_at
end

#due_dateDateTime?

Returns the due date of the task.

Returns:

  • (DateTime, nil)

    the due date of the task or nil if no due date exists.

Since:

  • 0.1.0



134
135
136
# File 'lib/ptf/metadata_file.rb', line 134

def due_date
  @data[:due_date]
end

#due_date_list_formatString

Returns the due date of the task for the ptf list command.

Returns:

  • (String)

    the due date in the form ‘Mon Dec 19 - 11AM -’ or ‘Mon Dec 9 - 2PM -’.

Since:

  • 0.1.0



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_strString

Returns the due date of the task as a String.

Returns:

  • (String)

    the due date of the task as a string.

Since:

  • 0.1.0



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

#estimateInteger

Returns the estimated time to complete the task.

Returns:

  • (Integer)

    the estimated time to compelte the task.

Since:

  • 0.1.0



179
180
181
# File 'lib/ptf/metadata_file.rb', line 179

def estimate
  @data[:estimate]
end

#file_stringString

Returns the data in the file as a String.

Returns:

  • (String)

    the entire Metadata file as a String.

Since:

  • 0.1.0



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

#groupPtf::Group

Returns the group the task is associated with.

Returns:

  • (Ptf::Group)

    the group the task is associated with.

Since:

  • 0.1.0



127
128
129
# File 'lib/ptf/metadata_file.rb', line 127

def group
  @data[:group]
end

#hashString

Return the hash of the task.

Returns:

  • (String)

    the hash of the metadata file (the name of the data file).

Since:

  • 0.1.0



241
242
243
# File 'lib/ptf/metadata_file.rb', line 241

def hash
  @data[:hash]
end

#idInteger

Return the id of the task.

Returns:

  • (Integer)

    the ID number of the task.

Since:

  • 0.1.0



234
235
236
# File 'lib/ptf/metadata_file.rb', line 234

def id
  @data[:id]
end

#key_val_to_str(key, val) ⇒ Object

Since:

  • 0.1.0



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

Parameters:

  • datetime (DateTime)

    the datetime to convert to a String.

Returns:

  • (String)

    a String representing the datetime (ex. ‘Wed Dec 21 - 11AM -’)

Since:

  • 0.1.0



163
164
165
# File 'lib/ptf/metadata_file.rb', line 163

def list_format(datetime)
  datetime.strftime("%a %b %_d - %l%p -")
end

#reopenObject

Since:

  • 0.1.0



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.

Parameters:

  • new_date (DateTime, nil)

    the new due date for the task

Raises:

  • (ArgumentError)

    if new_date is not a DateTime or nil.

Since:

  • 0.1.0



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.

Parameters:

  • new_estimate (Integer, nil)

    the new estimated time to complete the task.

Raises:

  • (ArgumentError)

    if the new_estimate is not an Integer or nil.

Since:

  • 0.1.0



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.

Parameters:

  • new_title (String)

    the new title.

Raises:

  • (ArgumentError)

    if the new_title is not a String.

Since:

  • 0.1.0



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

#titleString

Returns the title of the task.

Returns:

  • (String)

    the title of the task.

Since:

  • 0.1.0



109
110
111
# File 'lib/ptf/metadata_file.rb', line 109

def title
  @data[:title]
end

#write_to_fileObject

Writes the metadata file. Overwrites any previous metadata file for the same task.

Since:

  • 0.1.0



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