Module: Ptf::Commands::Task::Create

Defined in:
lib/ptf/commands/task/create.rb

Class Method Summary collapse

Class Method Details

.create(t, g, d, e) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ptf/commands/task/create.rb', line 24

def create(t, g, d, e)
  title = t
  group = g
  due_date = d
  estimate = e

  # Select a group for the task
  if group.nil?
    group = Ptf::Group.default_group
  else
    if Ptf::Group.group_exist? group
      group = Ptf::Group.get_group group
    else
      return "Group #{p} does not exist. Create it with ptf group add GROUP ABBREVIATION"
    end
  end

  # Verify the due date
  if !due_date.nil?
    due_date = Ptf::Date.create_datetime_from_str(due_date)
    if due_date.nil?
      return "Improperly formatted due date '#{d}' given."
    end
  end

  id = new_id_number

  info_file = Ptf::MetadataFile.create_from_input(title, group, due_date, estimate, id)

  task_info_hash = generate_hash info_file.file_string

  # Append hash to the file
  info_file.add_content(:hash, task_info_hash)

  data_file = Ptf::DataFile.new(info_file)

  info_file.write_to_file
  data_file.write_to_file

  return "Task #{info_file.id} created successfully."
end

.generate_hash(file_contents) ⇒ Object



9
10
11
# File 'lib/ptf/commands/task/create.rb', line 9

def generate_hash(file_contents)
  Digest::SHA1.hexdigest file_contents
end

.new_id_numberObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/ptf/commands/task/create.rb', line 13

def new_id_number
  id_file_path = Ptf::FileSystem.id_counter_file
  next_id = File.read(id_file_path).to_i

  id_file = File.new(id_file_path, "w")
  id_file.write("#{next_id+1}")
  id_file.close

  next_id
end