diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl/commands.rb | 4 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 18 | ||||
| -rw-r--r-- | lib/hcl/net.rb | 3 | ||||
| -rw-r--r-- | lib/hcl/project.rb | 1 | ||||
| -rw-r--r-- | lib/hcl/timesheet_resource.rb | 49 | 
5 files changed, 61 insertions, 14 deletions
| diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb index 98daff5..08d8a1e 100644 --- a/lib/hcl/commands.rb +++ b/lib/hcl/commands.rb @@ -13,7 +13,7 @@ module HCl      def tasks project_code=nil        tasks = Task.all        if tasks.empty? # cache tasks -        DayEntry.all +        DayEntry.today          tasks = Task.all        end        tasks.select! {|t| t.project.code == project_code } if project_code @@ -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.daily(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..09f62dc 100644 --- a/lib/hcl/day_entry.rb +++ b/lib/hcl/day_entry.rb @@ -2,14 +2,10 @@ module HCl    class DayEntry < TimesheetResource      include Utility -    # Get the time sheet entries for a given day. If no date is provided -    # defaults to today. -    def self.all date = nil -      url = date.nil? ? 'daily' : "daily/#{date.strftime '%j/%Y'}" -      doc = Net.get url -      Task.cache_tasks_hash doc -      doc[:day_entries].map {|e| new e} -    end +    collection_name :day_entries +    resources :today, 'daily', load_cb:->(data) { Task.cache_tasks_hash data } +    resources(:daily) {|date| date ? "daily/#{date.strftime '%j/%Y'}" : 'daily' } +    resource(:project_info, class_name:'Project') { "projects/#{project_id}" }      def to_s        "#{client} - #{project} - #{task} (#{formatted_hours})" @@ -41,16 +37,16 @@ module HCl      end      def self.with_timer date=nil -      all(date).detect {|t| t.running? } +      daily(date).detect {|t| t.running? }      end      def self.last_by_task project_id, task_id -      all.sort {|a,b| b.updated_at<=>a.updated_at}. +      today.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 -      all.sort {|a,b| a.updated_at<=>b.updated_at}[-1] +      today.sort {|a,b| a.updated_at<=>b.updated_at}[-1]      end      def running? diff --git a/lib/hcl/net.rb b/lib/hcl/net.rb index 67e3086..dc75e39 100644 --- a/lib/hcl/net.rb +++ b/lib/hcl/net.rb @@ -1,4 +1,5 @@  require 'hcl/harvest_middleware' +require 'faraday'  module HCl    module Net @@ -22,7 +23,7 @@ module HCl        def http          @http ||= Faraday.new( -          "http#{ssl && 's'}://#{subdomain}.harvestapp.com" +          "http#{ssl ? 's' : '' }://#{subdomain}.harvestapp.com"          ) do |f|            f.use :harvest, login, password            f.adapter Faraday.default_adapter diff --git a/lib/hcl/project.rb b/lib/hcl/project.rb index df9ea0c..170fcd9 100644 --- a/lib/hcl/project.rb +++ b/lib/hcl/project.rb @@ -1,3 +1,4 @@  class HCl::Project < HCl::TimesheetResource +  collection_name :projects  end diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb index 28e5378..8a572ae 100644 --- a/lib/hcl/timesheet_resource.rb +++ b/lib/hcl/timesheet_resource.rb @@ -15,5 +15,54 @@ module HCl      def respond_to? method        (@data && @data.key?(method.to_sym)) || super      end + +    class << self +      def _prepare_resource name, *args, &url_cb +        ((@resources ||= {})[name] = {}).tap do |res| +          opt_or_cb = args.pop +          res[:url_cb] = url_cb +          res[:opts] = {} +          case opt_or_cb +          when String +            res[:url_cb] = ->() { opt_or_cb } +            res[:opts] = args.pop || {} +          when Hash +            res[:opts] = opt_or_cb +            url = args.pop +            res[:url_cb] ||= ->() { url } +          end +        end +      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| +          url = instance_exec *args, &res[:url_cb] +          cb = res[:opts][:load_cb] +          Net.get(url).tap{|e| cb.call(e) if cb }[cls.collection_name].map{|e|new(e)} +        end +      end + +      def resource 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| +          url = instance_exec *args, &res[:url_cb] +          cb = res[:opts][:load_cb] +          cls.new Net.get(url)[cls.underscore_name].tap{|e| cb.call(e) if cb } +        end +      end + +      def underscore_name +        @underscore_name ||= name.split('::').last.split(/(?=[A-Z])/).map(&:downcase).join('_').to_sym +      end + +      def collection_name name=nil +        name ? (@collection_name = name) : @collection_name +      end +    end    end  end | 
