Class: Gwtf::Item

Inherits:
Object
  • Object
show all
Includes:
ObjHash
Defined in:
lib/gwtf/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ObjHash

#[], #[]=, #default_property_value, #each, #include?, included, #merge, #merge!, #method_missing, #objhash_config, #objhash_values, #to_hash, #to_json, #to_yaml, #update_property, #validate_property

Constructor Details

#initialize(file = nil, project = nil) ⇒ Item

Returns a new instance of Item.



17
18
19
20
21
22
# File 'lib/gwtf/item.rb', line 17

def initialize(file=nil, project=nil)
  @file = file
  @project = project

  load_item if file
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ObjHash

Instance Attribute Details

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/gwtf/item.rb', line 5

def file
  @file
end

#projectObject (readonly)

Returns the value of attribute project.



6
7
8
# File 'lib/gwtf/item.rb', line 6

def project
  @project
end

Instance Method Details

#backup_dirObject



68
69
70
# File 'lib/gwtf/item.rb', line 68

def backup_dir
  File.join(File.dirname(file), "backups")
end

#cancel_reminderObject



224
225
226
227
228
229
230
231
232
233
# File 'lib/gwtf/item.rb', line 224

def cancel_reminder
  raise "No at job id recorded for this item, cannot cancel a reminder" unless has_at_job?

  system("atrm %s" % at_job)

  raise "Failed to remove the at job %s" % at_job unless $? == 0

  update_property(:at_job, nil)
  save
end

#closeObject



191
192
193
194
# File 'lib/gwtf/item.rb', line 191

def close
  update_property(:closed_at, Time.now)
  update_property(:status, "closed")
end

#closed?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/gwtf/item.rb', line 34

def closed?
  !open?
end

#colorize_by_due_date(string) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/gwtf/item.rb', line 119

def colorize_by_due_date(string)
  if overdue?
    return Gwtf.red(string)
  elsif days_till_due <= 1 && open?
    return Gwtf.yellow(string)
  else
    return string
  end
end

#compact_flagsObject



109
110
111
112
113
114
115
116
117
# File 'lib/gwtf/item.rb', line 109

def compact_flags
  flags = []
  flags << "O" if overdue?
  flags << "D" if has_description?
  flags << "C" if closed?
  flags << "L" unless work_log.empty?

  flags
end

#date_to_due_date(timespec) ⇒ Object



24
25
26
27
28
# File 'lib/gwtf/item.rb', line 24

def date_to_due_date(timespec)
  Gwtf.parse_time(timespec).strftime("%Y-%m-%d")
rescue
  raise "Could not parse time specification #{timespec}"
end

#days_till_dueObject



54
55
56
57
58
# File 'lib/gwtf/item.rb', line 54

def days_till_due
  return 1000 unless has_due_date?

  return (Date.parse(due_date) - Date.today).to_i
end

#due?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/gwtf/item.rb', line 46

def due?
  if has_due_date? && open?
    return !!(days_till_due <= 1)
  else
    return false
  end
end

#flagsObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/gwtf/item.rb', line 98

def flags
  flag = []

  flag << "closed" if closed?
  flag << "open" if open?
  flag << "descr" if description?
  flag << "overdue" if overdue?
  flag << work_log.size.to_s unless work_log.empty?
  flag
end

#load_itemObject



60
61
62
63
64
65
66
# File 'lib/gwtf/item.rb', line 60

def load_item
  raise "A file to read from has not been specified" unless @file

  read_item = JSON.parse(File.read(@file))

  merge!(read_item)
end

#openObject



186
187
188
189
# File 'lib/gwtf/item.rb', line 186

def open
  update_property(:closed_at, nil)
  update_property(:status, "open")
end

#open?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/gwtf/item.rb', line 30

def open?
  status == "open"
end

#overdue?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/gwtf/item.rb', line 38

def overdue?
  if has_due_date? && open?
    return !!(days_till_due < 0)
  else
    return false
  end
end

#record_work(text, elapsed = 0) ⇒ Object



180
181
182
183
184
# File 'lib/gwtf/item.rb', line 180

def record_work(text, elapsed=0)
  update_property(:edited_at, Time.now)

  work_log << {"text" => text, "time" => Time.now, "elapsed" => elapsed}
end

#save(backup = true) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gwtf/item.rb', line 72

def save(backup=true)
  raise "No item_id set, cannot save item" unless item_id

  if backup && File.exist?(@file)
    backup_name = File.basename(@file) + "-" + Time.now.to_f.to_s

    FileUtils.mv(@file, File.join(backup_dir, backup_name))
  end

  File.open(@file, "w") do |f|
    f.print to_json
  end

  @file
end

#schedule_reminder(timespec, recipient, done = false, ifopen = false) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/gwtf/item.rb', line 196

def schedule_reminder(timespec, recipient, done=false, ifopen=false)
  command_args = ["--remind=%s" % [item_id]]
  command_args << "--recipient=%s" % [ recipient ]
  command_args << "--done" if done
  command_args << "--ifopen" if ifopen

  # attempt to parse the timespec with Chronic, if it cant then pass it onto at verbatim
  unless timespec.include?("+")
    if time = Gwtf.parse_time(timespec)
      timespec = "-t %s" % [time.strftime("%Y%m%d%H%M")]
    end
  end

  command = "echo gwtf --project='%s' notify %s | at %s 2>&1" % [ @project, command_args.join(" "), timespec]
  out = %x[#{command}]

  raise "Failed to add at(1) job: %s" % [ out ] unless $? == 0

  if out =~ /^job (\d+?) at \d/
    update_property(:at_job, Integer($1))
  else
    raise "Could not parse at(1) output for jobid: #{out}"
  end

  puts out
  out
end

#send_reminder(recipient, mark_as_done) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/gwtf/item.rb', line 235

def send_reminder(recipient, mark_as_done)
  recipient.split(",").each do |r|
    Gwtf.notifier_for_address(r.strip).new(self, r.strip).notify
  end

  if mark_as_done
    record_work("Closing item as part of scheduled reminder")
    close
    save
  end
end

#summaryObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gwtf/item.rb', line 129

def summary
  summary = StringIO.new

  summary.puts "    Subject: %s" % [ subject ]
  summary.puts "     Status: %s" % [ status ]

  if has_due_date? && open?
    due = "%s (%d days)" % [ due_date, days_till_due ]
    summary.puts "   Due Date: %s" % [ colorize_by_due_date(due) ]
  end

  summary.puts "Time Worked: %s" % [ Gwtf.seconds_to_human(time_worked) ] if time_worked > 0
  summary.puts "    Created: %s" % [ Time.parse(created_at.to_s).strftime("%F %R") ]
  summary.puts "     Closed: %s" % [ Time.parse(closed_at.to_s).strftime("%F %R") ] if closed?
  summary.puts "     At Job: %d" % [ at_job ] if at_job?
  summary.puts "         ID: %s" % [ item_id ]

  if has_description?
    summary.puts
    summary.puts "Description:"

    description.split("\n").each do |line|
      summary.puts "%13s%s" % [ "", line]
    end

    summary.puts
  end

  time_spent = 0

  work_log.reverse.each_with_index do |log, idx|
    summary.puts if idx == 0
    summary.puts "Work Log: " if idx == 0

    # we used to automatically embed this into the description which was dumb
    if log["elapsed"] > 0
      elapsed = "(%s)" % [Gwtf.seconds_to_human(log["elapsed"])] unless log["text"] =~ /\(.+?\)$/
    else
      elapsed = ""
    end

    summary.puts "%27s %s %s" % [Time.parse(log["time"]).strftime("%F %R"), log["text"], elapsed]
  end

  summary.string
end

#time_workedObject



88
89
90
91
92
93
94
95
96
# File 'lib/gwtf/item.rb', line 88

def time_worked
  work_log.inject(0) do |result, log|
    begin
      result + log["elapsed"]
    rescue
      result
    end
  end
end

#to_sObject



176
177
178
# File 'lib/gwtf/item.rb', line 176

def to_s
  colorize_by_due_date("%5s %-4s%-10s %s" % [ item_id, compact_flags.join, has_due_date? ? due_date : "", subject ])
end