class Ptf::MetadataFile

Represents a single Metadata file.

@author Austin Blatt @since 0.1.0

Public Class Methods

create_from_file(filepath) click to toggle source

Create a MetadataFile object from the given filepath.

@param filepath [String] the full file path to the metadata file.

@raise [ArgumentError] if the given filepath does not exist. @raise [ArgumentError] if the given file is not a metadata file.

@return [Ptf::MetadataFile] the object representation of the metadata file.

# 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) click to toggle source

Create a MetadataFile object from the given input.

@param title [String] the task’s title. @param group [Ptf::Group] the Group the task is associated with. @param due_date [DateTime, nil] the due date given for the task. @param estimate [Integer, nil] the estimated time to complete the task. @param id [Integer] the unique ID number for this task.

@return [Ptf::MetadataFile] the metadata file for the task.

# 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.metadata_open_dir
  group_dir = File.join(open_dir, group.name)
  filepath = File.join(group_dir, id.to_s)

  self.new(filepath, data)
end
new(filepath, data) click to toggle source

Initialize a new metadata file.

@param filepath [String] the full path to the file. @param data [Hash] the data in the file. @option data [String] :title the task’s title. @option data [Ptf::Group] :group the Group the task is associated with. @option data [DateTime, nil] :due_date the due date given for the task. @option data [Integer, nil] :estimate the estimated time to complete the task. @option data [Integer] :id the unique ID number for this task. @option data [DateTime] :created_at the time the task was created. @option data [DateTime, nil] :completed_at the time the task was completed (nil if it has not been completed). @option data [String] :hash the file hash (the filepath of the data file).

# File lib/ptf/metadata_file.rb, line 88
def initialize(filepath, data)
  @filepath = filepath
  @data = data
end

Public Instance Methods

add_content(key, val) click to toggle source

Add content to the metadata file.

@param key [Symbol] the key for the new content. @param val [Object] the value for the new content.

@raise [ArgumentError] if the key is not a symbol.

# 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() click to toggle source

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.

# 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.metadata_closed_dir, group.name), id.to_s)

  write_to_file
end
completed_at() click to toggle source

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

@return [DateTime] the date and time that the task was completed at.

# File lib/ptf/metadata_file.rb, line 211
def completed_at
  @data[:completed_at]
end
completed_at_list_format() click to toggle source

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

@return [String] the completed at time in the form ‘Mon Dec 19 - 11AM -’ or ‘Mon Dec 9 - 2PM -’.

# 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() click to toggle source

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

@return [String] the date and time represented as a string.

# File lib/ptf/metadata_file.rb, line 218
def completed_at_str
  Ptf::Date.datetime_to_str completed_at
end
created_at() click to toggle source

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

@return [DateTime] the date and time that the task was created at.

# File lib/ptf/metadata_file.rb, line 197
def created_at
  @data[:created_at]
end
created_at_str() click to toggle source

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

@return [String] the date and time represented as a string.

# File lib/ptf/metadata_file.rb, line 204
def created_at_str
  Ptf::Date.datetime_to_str create_at
end
due_date() click to toggle source

Returns the due date of the task.

@return [DateTime, nil] the due date of the task or nil if no due date exists.

# File lib/ptf/metadata_file.rb, line 134
def due_date
  @data[:due_date]
end
due_date_list_format() click to toggle source

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

@return [String] the due date in the form ‘Mon Dec 19 - 11AM -’ or ‘Mon Dec 9 - 2PM -’.

# 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() click to toggle source

Returns the due date of the task as a String.

@return [String] the due date of the task as a string.

# 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() click to toggle source

Returns the estimated time to complete the task.

@return [Integer] the estimated time to compelte the task.

# File lib/ptf/metadata_file.rb, line 179
def estimate
  @data[:estimate]
end
file_string() click to toggle source

Returns the data in the file as a String.

@return [String] the entire Metadata file as a String.

# 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() click to toggle source

Returns the group the task is associated with.

@return [Ptf::Group] the group the task is associated with.

# File lib/ptf/metadata_file.rb, line 127
def group
  @data[:group]
end
hash() click to toggle source

Return the hash of the task.

@return [String] the hash of the metadata file (the name of the data file).

# File lib/ptf/metadata_file.rb, line 241
def hash
  @data[:hash]
end
id() click to toggle source

Return the id of the task.

@return [Integer] the ID number of the task.

# File lib/ptf/metadata_file.rb, line 234
def id
  @data[:id]
end
key_val_to_str(key, val) click to toggle source
# 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) click to toggle source

Returns the given datetime in list format

@param datetime [DateTime] the datetime to convert to a String.

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

# File lib/ptf/metadata_file.rb, line 163
def list_format(datetime)
  datetime.strftime("%a %b %_d - %l%p -")
end
reopen() click to toggle source
# File lib/ptf/metadata_file.rb, line 258
def reopen
  FileUtils.rm @filepath
  @filepath = File.join(File.join(Ptf::FileSystem.metadata_open_dir, group.name), id.to_s)

  write_to_file
end
set_due_date(new_date) click to toggle source

Set the task due’s date.

@param new_date [DateTime, nil] the new due date for the task

@raise [ArgumentError] if new_date is not a DateTime or nil.

# 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) click to toggle source

Set the task’s estiamted time.

@param new_estimate [Integer, nil] the new estimated time to complete the task.

@raise [ArgumentError] if the new_estimate is not an Integer or nil.

# 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) click to toggle source

Set the task’s title to a new title.

@param new_title [String] the new title.

@raise [ArgumentError] if the new_title is not a String.

# 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() click to toggle source

Returns the title of the task.

@return [String] the title of the task.

# File lib/ptf/metadata_file.rb, line 109
def title
  @data[:title]
end
write_to_file() click to toggle source

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

# 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