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



62
63
64
# File 'lib/gwtf/item.rb', line 62

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

#closeObject



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

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

#closed?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/gwtf/item.rb', line 28

def closed?
  !open?
end

#colorize_by_due_date(string) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/gwtf/item.rb', line 113

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



103
104
105
106
107
108
109
110
111
# File 'lib/gwtf/item.rb', line 103

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

#days_till_dueObject



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

def days_till_due
  return 1000 unless has_due_date?

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

#due?Boolean

Returns:

  • (Boolean)


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

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

#flagsObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/gwtf/item.rb', line 92

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



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

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



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

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

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  status == "open"
end

#overdue?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/gwtf/item.rb', line 32

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

#record_work(text, elapsed = 0) ⇒ Object



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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gwtf/item.rb', line 66

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_reminer(timespec, recipient, done = false, ifopen = false) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/gwtf/item.rb', line 190

def schedule_reminer(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

  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



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/gwtf/item.rb', line 211

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



123
124
125
126
127
128
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
# File 'lib/gwtf/item.rb', line 123

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



82
83
84
85
86
87
88
89
90
# File 'lib/gwtf/item.rb', line 82

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

#to_sObject



170
171
172
# File 'lib/gwtf/item.rb', line 170

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