summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZack Hobson2013-12-26 13:46:21 -0800
committerZack Hobson2013-12-26 13:46:21 -0800
commiteada20ef66ade0b9a98ef5f03256a73ce428244b (patch)
tree2deab4053087e3d8f0907da68e98fc54d1f403a8
parent830ae68c2ee291ecd4e1d83b48d38224f434c98f (diff)
downloadhcl-eada20ef66ade0b9a98ef5f03256a73ce428244b.tar.bz2
timesheet_resource: API improvements
-rw-r--r--lib/hcl/commands.rb8
-rw-r--r--lib/hcl/day_entry.rb4
-rw-r--r--lib/hcl/task.rb8
-rw-r--r--lib/hcl/timesheet_resource.rb17
-rw-r--r--test/app_test.rb2
-rw-r--r--test/command_test.rb6
-rw-r--r--test/task_test.rb4
7 files changed, 33 insertions, 16 deletions
diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb
index 98daff5..e2dccb1 100644
--- a/lib/hcl/commands.rb
+++ b/lib/hcl/commands.rb
@@ -11,10 +11,10 @@ module HCl
end
def tasks project_code=nil
- tasks = Task.all
+ tasks = Task.get_all
if tasks.empty? # cache tasks
- DayEntry.all
- tasks = Task.all
+ DayEntry.get_all
+ tasks = Task.get_all
end
tasks.select! {|t| t.project.code == project_code } if project_code
if tasks.empty?
@@ -127,7 +127,7 @@ module HCl
date = args.empty? ? nil : Chronic.parse(args.join(' '))
total_hours = 0.0
result = ''
- DayEntry.all(date).each do |day|
+ DayEntry.get_all(date).each do |day|
running = day.running? ? '(running) ' : ''
columns = HighLine::SystemExtensions.terminal_size[0] rescue 80
result << "\t#{day.formatted_hours}\t#{running}#{day.project}: #{day.notes.lines.to_a.last}\n"[0..columns-1]
diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb
index 645bb3d..c245b5a 100644
--- a/lib/hcl/day_entry.rb
+++ b/lib/hcl/day_entry.rb
@@ -4,7 +4,7 @@ module HCl
# Get the time sheet entries for a given day. If no date is provided
# defaults to today.
- def self.all date = nil
+ def self.get_all date = nil
url = date.nil? ? 'daily' : "daily/#{date.strftime '%j/%Y'}"
doc = Net.get url
Task.cache_tasks_hash doc
@@ -37,7 +37,7 @@ module HCl
# If I don't include hours it gets reset.
# This doens't appear to be the case for task and project.
(self.notes << "\n#{new_notes}").lstrip!
- Net.post "daily/update/#{id}", notes:notes, hours:hours
+ DayEntry.post "daily/update/#{id}", notes:notes, hours:hours
end
def self.with_timer date=nil
diff --git a/lib/hcl/task.rb b/lib/hcl/task.rb
index e1d5b58..8c36860 100644
--- a/lib/hcl/task.rb
+++ b/lib/hcl/task.rb
@@ -22,7 +22,7 @@ module HCl
File.join(HCl::App::HCL_DIR, 'cache')
end
- def self.all
+ def self.get_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
@@ -34,7 +34,7 @@ module HCl
end
def self.find project_id, id
- all.detect do |t|
+ get_all.detect do |t|
t.project.id.to_i == project_id.to_i && t.id.to_i == id.to_i
end
end
@@ -50,7 +50,7 @@ module HCl
def add opts
notes = opts[:note]
starting_time = opts[:starting_time] || 0
- DayEntry.new Net.post("daily/add", {
+ DayEntry.post("daily/add", {
notes: notes,
hours: starting_time,
project_id: project.id,
@@ -64,7 +64,7 @@ module HCl
if day.running?
day
else
- DayEntry.new Net.get("daily/timer/#{day.id}")
+ DayEntry.get("daily/timer/#{day.id}")
end
end
end
diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb
index 28e5378..dcc6b72 100644
--- a/lib/hcl/timesheet_resource.rb
+++ b/lib/hcl/timesheet_resource.rb
@@ -15,5 +15,22 @@ module HCl
def respond_to? method
(@data && @data.key?(method.to_sym)) || super
end
+
+ class << self
+ def post *args
+ new Net.post *args
+ end
+ def get *args
+ new Net.get *args
+ end
+ def get_all *args
+ Net.get(*args)[collection_key].map {|o| new o }
+ end
+
+ def collection_key
+ @collection_key ||=
+ self.name.split("::").last.split(/(?=[A-Z])/).map(&:downcase).join('_').to_sym
+ end
+ end
end
end
diff --git a/test/app_test.rb b/test/app_test.rb
index 1fc1073..0efc0bc 100644
--- a/test/app_test.rb
+++ b/test/app_test.rb
@@ -13,7 +13,7 @@ class AppTest < HCl::TestCase
end
def test_command_show
- HCl::DayEntry.expects(:all).returns [HCl::DayEntry.new(
+ HCl::DayEntry.expects(:get_all).returns [HCl::DayEntry.new(
hours:'2.06', notes:'hi world', project:'App'
)]
HCl::App.command 'show'
diff --git a/test/command_test.rb b/test/command_test.rb
index dd6f440..59456cf 100644
--- a/test/command_test.rb
+++ b/test/command_test.rb
@@ -24,7 +24,7 @@ class CommandTest < HCl::TestCase
end
def test_tasks
- HCl::Task.expects(:all).returns([HCl::Task.new(
+ HCl::Task.expects(:get_all).returns([HCl::Task.new(
id:123,
name: 'Dev',
project: HCl::Project.new(id:456, name:'App', client:'Bob', code:'b')
@@ -34,7 +34,7 @@ class CommandTest < HCl::TestCase
end
def test_show
- HCl::DayEntry.expects(:all).returns([HCl::DayEntry.new({
+ HCl::DayEntry.expects(:get_all).returns([HCl::DayEntry.new({
hours:'2.06',
notes: 'hi world',
project: 'App'
@@ -46,7 +46,7 @@ class CommandTest < HCl::TestCase
end
def test_aliases
- HCl::Task.expects(:all).returns([HCl::Task.new(
+ HCl::Task.expects(:get_all).returns([HCl::Task.new(
id:123,
name: 'Dev',
project: HCl::Project.new(id:456, name:'App', client:'Bob', code:'b')
diff --git a/test/task_test.rb b/test/task_test.rb
index bbdce89..4eb73bc 100644
--- a/test/task_test.rb
+++ b/test/task_test.rb
@@ -15,7 +15,7 @@ class TaskTest < HCl::TestCase
billable: true
}]
}]})
- assert_equal 1, HCl::Task.all.size
- assert_equal 'Security support', HCl::Task.all.first.name
+ assert_equal 1, HCl::Task.get_all.size
+ assert_equal 'Security support', HCl::Task.get_all.first.name
end
end