Class: CheckList::Update

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

Overview

Class to handle updates

Constant Summary collapse

HELP =
CheckList::Helpers

Instance Method Summary collapse

Constructor Details

#initialize(opts, filepath) ⇒ Update

Returns a new instance of Update.



13
14
15
16
# File 'lib/update.rb', line 13

def initialize(opts, filepath)
  @opts = opts
  @json = filepath.fetch_json('CHECKLIST')
end

Instance Method Details

#check_for_q(value) ⇒ Object



36
37
38
39
40
# File 'lib/update.rb', line 36

def check_for_q(value)
  return HELP.good_bye if value.downcase == 'q'

  value
end

#edit(orig_value) ⇒ Object



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

def edit(orig_value)
  value = check_for_q(HELP.ret_value)

  val_arr = nil
  val_arr = value.split(',', -1) if value.match(/^[-,0-9]+$/)
  arr_length = val_arr.length unless val_arr.nil?

  case arr_length
  when 1
    return show_list(orig_value, 'yes') if val_arr[0].to_i.zero?

    return show_sub_tasks(val_arr[0].to_i, orig_value)
  when 2
    return show_list(orig_value, 'yes') if val_arr[0].to_i.zero? || val_arr[1].to_i.zero?

    return edit_sub_task(val_arr[0].to_i, val_arr[1].to_i, orig_value)
  end
  show_list(orig_value, 'yes')
end

#edit_sub_task(task_idx, sub_task_idx, orig_value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/update.rb', line 109

def edit_sub_task(task_idx, sub_task_idx, orig_value)
  HELP.clear
  task = @list['tasks'][task_idx - 1]
  sub_task = task['subTasks'][sub_task_idx - 1]

  HELP.log "#{sub_task['name']} (status: #{HELP.check_status(sub_task['status'])})"
  HELP.log "Change: (#{HELP.green}yes#{HELP.white}/#{HELP.red}no#{HELP.white})"

  value = check_for_q(HELP.ret_value)
  yes = "#{HELP.green}y#{HELP.white}"
  no = "#{HELP.red}n#{HELP.white}"
  na = "#{HELP.yellow}na#{HELP.white}"
  HELP.log "Status: (#{yes}/#{no}/#{na})" if verify_edit(value)
  value = check_for_q(HELP.ret_value)
  update_results(value, orig_value, task_idx, sub_task_idx)
rescue StandardError
  show_list(orig_value, 'yes')
end

#show_list(value, type) ⇒ Object



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

def show_list(value, type)
  HELP.clear
  HELP.log 'Invalid selection' if type.is_a? String

  name = @json['results'][value - 1]['name']
  ref = @json['results'][value - 1]['ref']
  @list = @json['results'][value - 1]
  HELP.log "List: #{name} (#{ref})"
  @json['results'][value - 1]['tasks'].each_with_index do |task, index|
    HELP.log "#{index + 1}. #{task['name']} (status: #{HELP.check_status(task['status'])})"
    task['subTasks'].each_with_index do |sub_task, idx|
      HELP.log "  #{idx + 1}. #{sub_task['name']} (status: #{HELP.check_status(sub_task['status'])})"
    end
  end
  edit(value)
rescue StandardError => e
  HELP.log "Error: #{e}"
  HELP.leave
end

#show_lists(type) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/update.rb', line 18

def show_lists(type)
  HELP.clear if type.is_a? String
  HELP.log 'Edit Cancelled' if type.is_a? String
  HELP.log 'Lists'
  HELP.log ''

  @json['results'].each_with_index do |list, index|
    HELP.log "#{index + 1}. #{list['name']} (#{list['ref']})"
  end

  value = validate_ret_value(HELP.ret_value, @json['results'])

  return show_list(value, nil) unless value.zero?

  HELP.clear
  show_lists(type)
end

#show_sub_tasks(task_idx, orig_value) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/update.rb', line 94

def show_sub_tasks(task_idx, orig_value)
  HELP.clear
  task = @list['tasks'][task_idx - 1]
  HELP.log task['name']
  task['subTasks'].each_with_index do |tsk, idx|
    HELP.log "  #{idx + 1}. #{tsk['name']} (status: #{HELP.check_status(tsk['status'])})"
  end
  sub_task_idx = check_for_q(HELP.ret_value)
  return show_list(orig_value, 'yes') if sub_task_idx.to_i.zero?

  edit_sub_task(task_idx, sub_task_idx.to_i, orig_value)
rescue StandardError
  show_list(orig_value, 'yes')
end

#update_results(value, orig_value, task_idx, sub_task_idx) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/update.rb', line 128

def update_results(value, orig_value, task_idx, sub_task_idx)
  list = @json['results'][orig_value - 1]
  task = list['tasks'][task_idx - 1]
  subtask = task['subTasks'][sub_task_idx - 1]
  subtasks = task['subTasks']
  subtask['status'] = value
  subtask['time'] = CheckList::Config.time_now

  task['status'] = 'y'
  task['time'] = CheckList::Config.time_now
  subtasks.each do |tsk|
    if tsk['status'] == 'n'
      task['status'] = 'n'
      break
    end
  end

  HELP.write_json_file(@json)
  list = Sym.new(list).hash
  CheckList::DisplayResults.new(list)
end

#validate_ret_value(value, arr) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/update.rb', line 42

def validate_ret_value(value, arr)
  val = arr.length
  return value.to_i if !value.to_i.zero? && value.to_i <= val

  return check_for_q(value) if value == 'q'

  0
rescue StandardError
  HELP.log 'Incorrect value'
  0
end

#verify_edit(value) ⇒ Object



150
151
152
153
154
# File 'lib/update.rb', line 150

def verify_edit(value)
  return show_lists('yes') unless value.downcase == 'yes'

  true
end