Module: Ptf::Commands::Task::Edit

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

Class Method Summary collapse

Class Method Details

.edit(id) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ptf/commands/task/edit.rb', line 95

def edit(id)
  unless Ptf::FileSystem.id_exist? (id)
    return "The task #{id} does not exist"
  end

   = Ptf::FileSystem.find_file id

  if .nil?
    return "The task #{id} has been closed. Reopen it with ptf task reopen ###."
  end

  # Get hash from file
   = Ptf::MetadataFile.create_from_file()
  data_file_name = .hash
  data_file = File.join(Ptf::FileSystem.data_dir, data_file_name)

  # Create a copy in tmp foldere
  tmp_file_path = File.join(Ptf::FileSystem.tmp_dir, data_file_name)
  FileUtils.copy data_file, tmp_file_path
  edit_succeeded = system("vim #{data_file}")

  if !edit_succeeded
    # Move tmp file back, then remove it

    FileUtils.copy tmp_file_path, data_file
    FileUtils.remove_file tmp_file_path
    return "Vim edit failed. Aborting changes."
  end

  if verify_file(data_file, )
    FileUtils.remove_file tmp_file_path
    return "Edit successful!"
  else
    FileUtils.copy tmp_file_path, data_file
    FileUtils.remove_file tmp_file_path
    return "File verification failed. Aborting changes."
  end
end

.verify_due(line, metadata) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ptf/commands/task/edit.rb', line 54

def verify_due(line, )
  due_line_regex = /^#Due: ?(\d\d\d\d[01]\d[0-3]\d[0-2]\d[0-5]\d[0-5]\d)?$/

  base, date = due_line_regex.match(line).to_a

  if !base.nil? && date.nil?
    .set_due_date nil
    return [true, ]
  end

  begin
    datetime = Ptf::Date.str_to_datetime date
  rescue
    return [false, ]
  end

  .set_due_date datetime
  [!datetime.nil?, ]
end

.verify_estimate(line, metadata) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ptf/commands/task/edit.rb', line 74

def verify_estimate(line, )
  estimate_line_regex = /^#Estimate: ?(\d+)?$/

  base, estimate = estimate_line_regex.match(line).to_a

  if !base.nil? && estimate.nil?
    .set_estimate nil
    return [true, ]
  elsif base.nil?
    return [false, ]
  end

  estimate = estimate.to_i
  if estimate != 0
    .set_estimate estimate
    return [true, ]
  else
    return [false, ]
  end
end

.verify_file(file, metadata) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ptf/commands/task/edit.rb', line 7

def verify_file(file, )
  title_line, , estimate_line = File.foreach(file).first(3)

  did_succeed,  = verify_title_line title_line, 
  return false unless did_succeed

  did_succeed,  = verify_due , 
  return false unless did_succeed

  did_succeed,  = verify_estimate estimate_line, 
  return false unless did_succeed

  .write_to_file
  did_succeed
end

.verify_group(str, metadata) ⇒ Object



32
33
34
35
36
# File 'lib/ptf/commands/task/edit.rb', line 32

def verify_group(str, )
  return false unless !str.nil? && str.is_a?(String)

  Ptf::Group.group_exist?(str) && .group.abbreviation == str
end

.verify_id(str, metadata) ⇒ Object



38
39
40
41
42
# File 'lib/ptf/commands/task/edit.rb', line 38

def verify_id(str, )
  return false unless !str.nil? && str.is_a?(String)

  str.to_i == .id
end

.verify_title(str, metadata) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ptf/commands/task/edit.rb', line 44

def verify_title(str, )
  return false unless !str.nil? && str.is_a?(String)

  if str != .title
    .set_title str
  end

  [true, ]
end

.verify_title_line(line, metadata) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/ptf/commands/task/edit.rb', line 23

def verify_title_line(line, )
  title_line_regex = /^#([A-Z]+)--(\d+)\s([ a-zA-Z0-9]+)$/

  _, group, id, title = title_line_regex.match(line).to_a

  is_title_valid,  = verify_title title, 
  [(verify_group group, ) && (verify_id id, ) && is_title_valid, ]
end