samg-timetrap 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/timetrap.rb +27 -12
 - data/spec/timetrap_spec.rb +46 -0
 - metadata +1 -1
 
    
        data/lib/timetrap.rb
    CHANGED
    
    | 
         @@ -44,7 +44,11 @@ module Timetrap 
     | 
|
| 
       44 
44 
     | 
    
         
             
                end
         
     | 
| 
       45 
45 
     | 
    
         | 
| 
       46 
46 
     | 
    
         
             
                def alter
         
     | 
| 
       47 
     | 
    
         
            -
                   
     | 
| 
      
 47 
     | 
    
         
            +
                  entry = args['--id'] ? Entry[args['--id']] : Timetrap.active_entry
         
     | 
| 
      
 48 
     | 
    
         
            +
                  say "can't find entry" && return unless entry
         
     | 
| 
      
 49 
     | 
    
         
            +
                  entry.update :start => args['--start'] if args['--start'] =~ /.+/
         
     | 
| 
      
 50 
     | 
    
         
            +
                  entry.update :end => args['--end'] if args['--end'] =~ /.+/
         
     | 
| 
      
 51 
     | 
    
         
            +
                  entry.update :note => unused_args if unused_args =~ /.+/
         
     | 
| 
       48 
52 
     | 
    
         
             
                end
         
     | 
| 
       49 
53 
     | 
    
         | 
| 
       50 
54 
     | 
    
         
             
                def backend
         
     | 
| 
         @@ -52,7 +56,7 @@ module Timetrap 
     | 
|
| 
       52 
56 
     | 
    
         
             
                end
         
     | 
| 
       53 
57 
     | 
    
         | 
| 
       54 
58 
     | 
    
         
             
                def in
         
     | 
| 
       55 
     | 
    
         
            -
                  Timetrap.start  
     | 
| 
      
 59 
     | 
    
         
            +
                  Timetrap.start unused_args, args['--at']
         
     | 
| 
       56 
60 
     | 
    
         
             
                end
         
     | 
| 
       57 
61 
     | 
    
         | 
| 
       58 
62 
     | 
    
         
             
                def out
         
     | 
| 
         @@ -60,7 +64,7 @@ module Timetrap 
     | 
|
| 
       60 
64 
     | 
    
         
             
                end
         
     | 
| 
       61 
65 
     | 
    
         | 
| 
       62 
66 
     | 
    
         
             
                def kill
         
     | 
| 
       63 
     | 
    
         
            -
                  sheet =  
     | 
| 
      
 67 
     | 
    
         
            +
                  sheet = unused_args
         
     | 
| 
       64 
68 
     | 
    
         
             
                  unless (sheets = Entry.map{|e| e.sheet }.uniq).include?(sheet)
         
     | 
| 
       65 
69 
     | 
    
         
             
                    say "ain't no sheet #{sheet.inspect}", 'sheets:', *sheets
         
     | 
| 
       66 
70 
     | 
    
         
             
                    return
         
     | 
| 
         @@ -76,10 +80,11 @@ module Timetrap 
     | 
|
| 
       76 
80 
     | 
    
         
             
                end
         
     | 
| 
       77 
81 
     | 
    
         | 
| 
       78 
82 
     | 
    
         
             
                def display
         
     | 
| 
       79 
     | 
    
         
            -
                  sheet = sheet_name_from_string( 
     | 
| 
      
 83 
     | 
    
         
            +
                  sheet = sheet_name_from_string(unused_args)
         
     | 
| 
       80 
84 
     | 
    
         
             
                  sheet = (sheet =~ /.+/ ? sheet : Timetrap.current_sheet)
         
     | 
| 
       81 
85 
     | 
    
         
             
                  say "Timesheet: #{sheet}"
         
     | 
| 
       82 
     | 
    
         
            -
                   
     | 
| 
      
 86 
     | 
    
         
            +
                  id_heading = args['--ids'] ? 'Id' : '  '
         
     | 
| 
      
 87 
     | 
    
         
            +
                  say "       #{id_heading}  Day                Start      End        Duration   Notes"
         
     | 
| 
       83 
88 
     | 
    
         
             
                  last_start = nil
         
     | 
| 
       84 
89 
     | 
    
         
             
                  from_current_day = []
         
     | 
| 
       85 
90 
     | 
    
         
             
                  (ee = Timetrap.entries(sheet)).each_with_index do |e, i|
         
     | 
| 
         @@ -87,7 +92,8 @@ module Timetrap 
     | 
|
| 
       87 
92 
     | 
    
         | 
| 
       88 
93 
     | 
    
         
             
                    from_current_day << e
         
     | 
| 
       89 
94 
     | 
    
         
             
                    e_end = e.end || Time.now
         
     | 
| 
       90 
     | 
    
         
            -
                    say "% 
     | 
| 
      
 95 
     | 
    
         
            +
                    say "%9s%18s%11s -%9s%10s    %s" % [
         
     | 
| 
      
 96 
     | 
    
         
            +
                      (args['--ids'] ? e.id : ''),
         
     | 
| 
       91 
97 
     | 
    
         
             
                      format_date_if_new(e.start, last_start),
         
     | 
| 
       92 
98 
     | 
    
         
             
                      format_time(e.start),
         
     | 
| 
       93 
99 
     | 
    
         
             
                      format_time(e.end),
         
     | 
| 
         @@ -115,7 +121,7 @@ module Timetrap 
     | 
|
| 
       115 
121 
     | 
    
         | 
| 
       116 
122 
     | 
    
         
             
                def switch
         
     | 
| 
       117 
123 
     | 
    
         
             
                  sheet = args.unused.join(' ')
         
     | 
| 
       118 
     | 
    
         
            -
                  if not sheet then say "No sheet specified"; return end
         
     | 
| 
      
 124 
     | 
    
         
            +
                  if not sheet =~ /.+/ then say "No sheet specified"; return end
         
     | 
| 
       119 
125 
     | 
    
         
             
                  say "Switching to sheet " + Timetrap.switch(sheet)
         
     | 
| 
       120 
126 
     | 
    
         
             
                end
         
     | 
| 
       121 
127 
     | 
    
         | 
| 
         @@ -200,6 +206,10 @@ module Timetrap 
     | 
|
| 
       200 
206 
     | 
    
         
             
                  ""
         
     | 
| 
       201 
207 
     | 
    
         
             
                end
         
     | 
| 
       202 
208 
     | 
    
         | 
| 
      
 209 
     | 
    
         
            +
                def unused_args
         
     | 
| 
      
 210 
     | 
    
         
            +
                  args.unused.join(' ')
         
     | 
| 
      
 211 
     | 
    
         
            +
                end
         
     | 
| 
      
 212 
     | 
    
         
            +
             
     | 
| 
       203 
213 
     | 
    
         
             
                public
         
     | 
| 
       204 
214 
     | 
    
         
             
                def say *something
         
     | 
| 
       205 
215 
     | 
    
         
             
                  puts *something
         
     | 
| 
         @@ -219,10 +229,6 @@ module Timetrap 
     | 
|
| 
       219 
229 
     | 
    
         
             
                Meta.find(:key => 'current_sheet').value
         
     | 
| 
       220 
230 
     | 
    
         
             
              end
         
     | 
| 
       221 
231 
     | 
    
         | 
| 
       222 
     | 
    
         
            -
              def invoked_as_executable?
         
     | 
| 
       223 
     | 
    
         
            -
                $0 == __FILE__
         
     | 
| 
       224 
     | 
    
         
            -
              end
         
     | 
| 
       225 
     | 
    
         
            -
             
     | 
| 
       226 
232 
     | 
    
         
             
              def entries sheet = nil
         
     | 
| 
       227 
233 
     | 
    
         
             
                Entry.filter(:sheet => sheet).order_by(:start)
         
     | 
| 
       228 
234 
     | 
    
         
             
              end
         
     | 
| 
         @@ -315,6 +321,15 @@ where COMMAND is one of: 
     | 
|
| 
       315 
321 
     | 
    
         
             
              switch - switch to a new timesheet
         
     | 
| 
       316 
322 
     | 
    
         | 
| 
       317 
323 
     | 
    
         
             
              COMMAND OPTIONS
         
     | 
| 
       318 
     | 
    
         
            -
               
     | 
| 
      
 324 
     | 
    
         
            +
              options for `display'
         
     | 
| 
      
 325 
     | 
    
         
            +
              --ids                 Print database ids (for use with alter)
         
     | 
| 
      
 326 
     | 
    
         
            +
             
     | 
| 
      
 327 
     | 
    
         
            +
              options for `in' and `out'
         
     | 
| 
      
 328 
     | 
    
         
            +
              --at <time:qs>        Use this time instead of now
         
     | 
| 
      
 329 
     | 
    
         
            +
             
     | 
| 
      
 330 
     | 
    
         
            +
              options for `alter'
         
     | 
| 
      
 331 
     | 
    
         
            +
              --id <id:i>           Alter entry with id <id> instead of the running entry
         
     | 
| 
      
 332 
     | 
    
         
            +
              --start <time:qs>     Change the start time to <time>
         
     | 
| 
      
 333 
     | 
    
         
            +
              --end <time:qs>       Change the end time to <time>
         
     | 
| 
       319 
334 
     | 
    
         
             
              EOF
         
     | 
| 
       320 
335 
     | 
    
         
             
            end
         
     | 
    
        data/spec/timetrap_spec.rb
    CHANGED
    
    | 
         @@ -29,11 +29,33 @@ describe Timetrap do 
     | 
|
| 
       29 
29 
     | 
    
         
             
                    before do
         
     | 
| 
       30 
30 
     | 
    
         
             
                      Timetrap.start "running entry", nil
         
     | 
| 
       31 
31 
     | 
    
         
             
                    end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
       32 
33 
     | 
    
         
             
                    it "should alter the description of the active period" do
         
     | 
| 
       33 
34 
     | 
    
         
             
                      Timetrap.active_entry.note.should == 'running entry'
         
     | 
| 
       34 
35 
     | 
    
         
             
                      invoke 'alter new description'
         
     | 
| 
       35 
36 
     | 
    
         
             
                      Timetrap.active_entry.note.should == 'new description'
         
     | 
| 
       36 
37 
     | 
    
         
             
                    end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                    it "should alter the start time of the active period" do
         
     | 
| 
      
 40 
     | 
    
         
            +
                      invoke 'alter --start "yesterday 10am"'
         
     | 
| 
      
 41 
     | 
    
         
            +
                      Timetrap.active_entry.start.should == Chronic.parse("yesterday 10am")
         
     | 
| 
      
 42 
     | 
    
         
            +
                      Timetrap.active_entry.note.should == 'running entry'
         
     | 
| 
      
 43 
     | 
    
         
            +
                    end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                    it "should alter the end time of the active period" do
         
     | 
| 
      
 46 
     | 
    
         
            +
                      entry = Timetrap.active_entry
         
     | 
| 
      
 47 
     | 
    
         
            +
                      invoke 'alter --end "yesterday 10am"'
         
     | 
| 
      
 48 
     | 
    
         
            +
                      entry.refresh.end.should == Chronic.parse("yesterday 10am")
         
     | 
| 
      
 49 
     | 
    
         
            +
                      entry.refresh.note.should == 'running entry'
         
     | 
| 
      
 50 
     | 
    
         
            +
                    end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                    it "should alter a non running entry based on id" do
         
     | 
| 
      
 53 
     | 
    
         
            +
                      not_running = Timetrap.active_entry
         
     | 
| 
      
 54 
     | 
    
         
            +
                      Timetrap.stop
         
     | 
| 
      
 55 
     | 
    
         
            +
                      Timetrap.start "another entry", nil
         
     | 
| 
      
 56 
     | 
    
         
            +
                      invoke "alter --id #{not_running.id} a new description"
         
     | 
| 
      
 57 
     | 
    
         
            +
                      not_running.refresh.note.should == 'a new description'
         
     | 
| 
      
 58 
     | 
    
         
            +
                    end
         
     | 
| 
       37 
59 
     | 
    
         
             
                  end
         
     | 
| 
       38 
60 
     | 
    
         | 
| 
       39 
61 
     | 
    
         
             
                  describe "backend" do
         
     | 
| 
         @@ -74,6 +96,19 @@ Timesheet: SpecSheet 
     | 
|
| 
       74 
96 
     | 
    
         
             
                       ---------------------------------------------------------
         
     | 
| 
       75 
97 
     | 
    
         
             
                       Total                                    8:00:00
         
     | 
| 
       76 
98 
     | 
    
         
             
                      OUTPUT
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
                      @desired_output_with_ids = <<-OUTPUT
         
     | 
| 
      
 101 
     | 
    
         
            +
            Timesheet: SpecSheet
         
     | 
| 
      
 102 
     | 
    
         
            +
                   Id  Day                Start      End        Duration   Notes
         
     | 
| 
      
 103 
     | 
    
         
            +
                    3  Fri Oct 03, 2008   12:00:00 - 14:00:00   2:00:00    entry 1
         
     | 
| 
      
 104 
     | 
    
         
            +
                    2                     16:00:00 - 18:00:00   2:00:00    entry 2
         
     | 
| 
      
 105 
     | 
    
         
            +
                                                                4:00:00
         
     | 
| 
      
 106 
     | 
    
         
            +
                    4  Sun Oct 05, 2008   16:00:00 - 18:00:00   2:00:00    entry 3
         
     | 
| 
      
 107 
     | 
    
         
            +
                    5                     18:00:00 -            2:00:00    entry 4
         
     | 
| 
      
 108 
     | 
    
         
            +
                                                                4:00:00
         
     | 
| 
      
 109 
     | 
    
         
            +
                       ---------------------------------------------------------
         
     | 
| 
      
 110 
     | 
    
         
            +
                       Total                                    8:00:00
         
     | 
| 
      
 111 
     | 
    
         
            +
                      OUTPUT
         
     | 
| 
       77 
112 
     | 
    
         
             
                    end
         
     | 
| 
       78 
113 
     | 
    
         | 
| 
       79 
114 
     | 
    
         
             
                    it "should display the current timesheet" do
         
     | 
| 
         @@ -93,6 +128,11 @@ Timesheet: SpecSheet 
     | 
|
| 
       93 
128 
     | 
    
         
             
                      invoke 'display S'
         
     | 
| 
       94 
129 
     | 
    
         
             
                      $stdout.string.should == @desired_output
         
     | 
| 
       95 
130 
     | 
    
         
             
                    end
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
                    it "should display a timesheet with ids" do
         
     | 
| 
      
 133 
     | 
    
         
            +
                      invoke 'display S --ids'
         
     | 
| 
      
 134 
     | 
    
         
            +
                      $stdout.string.should == @desired_output_with_ids
         
     | 
| 
      
 135 
     | 
    
         
            +
                    end
         
     | 
| 
       96 
136 
     | 
    
         
             
                  end
         
     | 
| 
       97 
137 
     | 
    
         | 
| 
       98 
138 
     | 
    
         
             
                  describe "format" do
         
     | 
| 
         @@ -251,6 +291,12 @@ current sheet: 0:01:00 (a timesheet that is running) 
     | 
|
| 
       251 
291 
     | 
    
         
             
                      invoke 'switch sheet 2'
         
     | 
| 
       252 
292 
     | 
    
         
             
                      Timetrap.current_sheet.should == 'sheet 2'
         
     | 
| 
       253 
293 
     | 
    
         
             
                    end
         
     | 
| 
      
 294 
     | 
    
         
            +
             
     | 
| 
      
 295 
     | 
    
         
            +
                    it "should not switch to an blank timesheet" do
         
     | 
| 
      
 296 
     | 
    
         
            +
                      invoke 'switch sheet 1'
         
     | 
| 
      
 297 
     | 
    
         
            +
                      invoke 'switch'
         
     | 
| 
      
 298 
     | 
    
         
            +
                      Timetrap.current_sheet.should == 'sheet 1'
         
     | 
| 
      
 299 
     | 
    
         
            +
                    end
         
     | 
| 
       254 
300 
     | 
    
         
             
                  end
         
     | 
| 
       255 
301 
     | 
    
         
             
                end
         
     | 
| 
       256 
302 
     | 
    
         
             
              end
         
     |