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, connect, delete, get, http_do, #id, #initialize, #method_missing, post, #respond_to?, 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



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

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



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

def self.cache_dir
  File.join(HCl::App::HCL_DIR, 'cache')
end

.cache_fileObject



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

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

.cache_tasks(doc) ⇒ Object



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

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



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

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hcl/task.rb', line 55

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



70
71
72
73
74
75
76
77
# File 'lib/hcl/task.rb', line 70

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



47
48
49
50
51
52
53
# File 'lib/hcl/task.rb', line 47

def to_s
  if project.code.empty?
    "#{project.client} - #{project.name} - #{name}"
  else
    "#{project.client} - [#{project.code}] #{project.name} - #{name}"
  end
end