diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl/commands.rb | 14 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 17 | ||||
| -rw-r--r-- | lib/hcl/timesheet_resource.rb | 13 | 
3 files changed, 15 insertions, 29 deletions
| diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb index 17c86b1..0811260 100644 --- a/lib/hcl/commands.rb +++ b/lib/hcl/commands.rb @@ -44,7 +44,7 @@ module HCl      end      def cancel -      entry = DayEntry.with_timer || DayEntry.last +      entry = DayEntry.with_timer(http) || DayEntry.last(http)        if entry          if entry.cancel http            "Deleted entry #{entry}." @@ -99,13 +99,13 @@ module HCl      end      def log *args -      fail "There is already a timer running." if DayEntry.with_timer +      fail "There is already a timer running." if DayEntry.with_timer(http)        start *args        stop      end      def stop *args -      entry = DayEntry.with_timer || DayEntry.with_timer(DateTime.yesterday) +      entry = DayEntry.with_timer(http) || DayEntry.with_timer(http, DateTime.yesterday)        if entry          entry.append_note(http, args.join(' ')) if args.any?          entry.toggle http @@ -116,7 +116,7 @@ module HCl      end      def note *args -      entry = DayEntry.with_timer +      entry = DayEntry.with_timer http        if entry          if args.empty?            return entry.notes @@ -133,7 +133,7 @@ module HCl        date = args.empty? ? nil : Chronic.parse(args.join(' '))        total_hours = 0.0        result = '' -      DayEntry.daily(date).each do |day| +      DayEntry.daily(http, 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] @@ -147,9 +147,9 @@ module HCl        ident = get_ident args        entry = if ident            task_ids = get_task_ids ident, args -          DayEntry.last_by_task *task_ids +          DayEntry.last_by_task http, *task_ids          else -          DayEntry.last +          DayEntry.last(http)          end        if entry          entry.toggle http diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb index 0a54bfd..c1215ed 100644 --- a/lib/hcl/day_entry.rb +++ b/lib/hcl/day_entry.rb @@ -36,28 +36,23 @@ module HCl        http.post "daily/update/#{id}", notes:notes, hours:hours      end -    def self.with_timer date=nil -      daily(date).detect {|t| t.running? } +    def self.with_timer http, date=nil +      daily(http, date).detect {|t| t.running? }      end -    def self.last_by_task project_id, task_id -      today.sort {|a,b| b.updated_at<=>a.updated_at}. +    def self.last_by_task http, project_id, task_id +      today(http).sort {|a,b| b.updated_at<=>a.updated_at}.          detect {|t| t.project_id == project_id && t.task_id == task_id }      end -    def self.last -      today.sort {|a,b| a.updated_at<=>b.updated_at}[-1] +    def self.last http +      today(http).sort {|a,b| a.updated_at<=>b.updated_at}[-1]      end      def running?        !@data[:timer_started_at].nil? && !@data[:timer_started_at].empty?      end -    def initialize *args -      super -      # TODO cache client/project names and ids -    end -      def toggle http        http.get("daily/timer/#{id}")        self diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb index 10f8586..e230f1f 100644 --- a/lib/hcl/timesheet_resource.rb +++ b/lib/hcl/timesheet_resource.rb @@ -16,10 +16,6 @@ module HCl        (@data && @data.key?(method.to_sym)) || super      end -    def http -      self.class.http -    end -      class << self        def _prepare_resource name, *args, &url_cb          ((@resources ||= {})[name] = {}).tap do |res| @@ -38,16 +34,11 @@ module HCl          end        end -      def http -        # XXX how do I get the app instance in here? -        HCl::Net -      end -        def resources name, *args, &url_cb          res = _prepare_resource name, *args, &url_cb          cls = res[:opts][:class_name] ? HCl.const_get(res[:opts][:class_name]) : self          method = cls == self ? :define_singleton_method : :define_method -        send(method, name) do |*args| +        send(method, name) do |http, *args|            url = instance_exec *args, &res[:url_cb]            cb = res[:opts][:load_cb]            http.get(url).tap{|e| cb.call(e) if cb }[cls.collection_name].map{|e|new(e)} @@ -58,7 +49,7 @@ module HCl          res = _prepare_resource name, *args, &url_cb          cls = res[:opts][:class_name] ? HCl.const_get(res[:opts][:class_name]) : self          method = cls == self ? :define_singleton_method : :define_method -        send(method, name) do |*args| +        send(method, name) do |http, *args|            url = instance_exec *args, &res[:url_cb]            cb = res[:opts][:load_cb]            cls.new http.get(url).tap{|e| cb.call(e) if cb }[cls.underscore_name] | 
