Class: HCl::Task

Inherits:
TimesheetResource show all
Defined in:
lib/hcl/task.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TimesheetResource

configure, get, http_do, #id, #initialize, #method_missing, post, xml_to_hash

Constructor Details

This class inherits a constructor from HCl::TimesheetResource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HCl::TimesheetResource

Class Method Details

.allObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/hcl/task.rb', line 29

def self.all
  tasks = File.exists?(cache_file) ? YAML.load(File.read(cache_file)) : []
  tasks = tasks.sort do |a,b|
    r = a.project.client <=> b.project.client
    r = a.project.name <=> b.project.name if r == 0
    r = a.name <=> b.name if r == 0
    r
  end
  tasks
end

.cache_dirObject



25
26
27
# File 'lib/hcl/task.rb', line 25

def self.cache_dir
  File.join(ENV['HOME'],'.hcl/cache')
end

.cache_fileObject



21
22
23
# File 'lib/hcl/task.rb', line 21

def self.cache_file
  File.join(cache_dir, 'tasks.yml')
end

.cache_tasks(doc) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hcl/task.rb', line 5

def self.cache_tasks doc
  tasks = []
  doc.root.elements.collect('projects/project') do |project_elem|
    project = Project.new xml_to_hash(project_elem)
    tasks.concat(project_elem.elements.collect('tasks/task') do |task|
      new xml_to_hash(task).merge(:project => project)
    end)
  end
  unless tasks.empty?
    FileUtils.mkdir_p(cache_dir)
    File.open(cache_file, 'w') do |f|
      f.write tasks.uniq.to_yaml
    end
  end
end

.find(project_id, id) ⇒ Object



40
41
42
43
44
# File 'lib/hcl/task.rb', line 40

def self.find project_id, id
  all.detect do |t|
    t.project.id.to_i == project_id.to_i && t.id.to_i == id.to_i
  end
end

Instance Method Details

#add(opts) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hcl/task.rb', line 50

def add opts
  notes = opts[:note]
  starting_time = opts[:starting_time] || 0
  days = DayEntry.from_xml Task.post("daily/add", <<-EOT)
  <request>
    <notes>#{notes}</notes>
    <hours>#{starting_time}</hours>
    <project_id type="integer">#{project.id}</project_id>
    <task_id type="integer">#{id}</task_id>
    <spent_at type="date">#{Date.today}</spent_at>
  </request>
  EOT
  days.first
end

#start(opts) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/hcl/task.rb', line 65

def start opts
  day = add opts
  if day.running?
    day
  else
    DayEntry.from_xml(Task.get("daily/timer/#{day.id}")).first
  end
end

#to_sObject



46
47
48
# File 'lib/hcl/task.rb', line 46

def to_s
  "#{project.client} - #{project.name} - #{name}"
end