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

_prepare_resource, collection_name, #id, #initialize, #method_missing, resource, resources, #respond_to?, underscore_name

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



25
26
27
28
29
30
31
32
33
34
# File 'lib/hcl/task.rb', line 25

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



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

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

.cache_fileObject



17
18
19
# File 'lib/hcl/task.rb', line 17

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

.cache_tasks_hash(day_entry_hash) ⇒ Object



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

def self.cache_tasks_hash day_entry_hash
  tasks = day_entry_hash[:projects].
    map { |p| p[:tasks].map {|t| new t.merge(project:Project.new(p)) } }.flatten.uniq
  unless tasks.empty?
    FileUtils.mkdir_p(cache_dir)
    File.open(cache_file, 'w') do |f|
      f.write tasks.to_yaml
    end
  end
end

.find(project_id, id) ⇒ Object



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

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(http, opts) ⇒ Object



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

def add http, opts
  notes = opts[:note]
  starting_time = opts[:starting_time] || 0
  DayEntry.new http.post("daily/add", {
    notes: notes,
    hours: starting_time,
    project_id: project.id,
    task_id: id,
    spent_at: Date.today
  })
end

#start(http, opts) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/hcl/task.rb', line 62

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

#to_sObject



42
43
44
45
46
47
48
# File 'lib/hcl/task.rb', line 42

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