one_inch_punch 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.1.1 2008-08-28
2
+
3
+ * 3 enhancements:
4
+ * Can format total time as "elapsed time" instead of only getting the number of seconds
5
+ * punch command gives improved output for some commands:
6
+ * in/out/log: either silent or gives a message explaining a failure
7
+ * total: shows "elapsed time" instead of number of seconds
8
+
1
9
  == 0.1.0 2008-08-26
2
10
 
3
11
  * 1 enhancement:
data/bin/punch CHANGED
@@ -18,9 +18,7 @@ MANDATORY_OPTIONS = %w[]
18
18
 
19
19
  parser = OptionParser.new do |opts|
20
20
  opts.banner = <<BANNER
21
- This application is wonderful because...
22
-
23
- Usage: #{File.basename($0)} [options]
21
+ Usage: #{File.basename($0)} [command] <project>
24
22
 
25
23
  Options are:
26
24
  BANNER
@@ -50,30 +48,57 @@ end
50
48
  project = ARGV[1]
51
49
 
52
50
  Punch.load
53
- case command
54
- when 'status'
55
- puts Punch.status(project).inspect
56
- when 'total'
57
- puts Punch.total(project, OPTIONS).inspect
58
- when 'in', 'delete'
59
- if project
60
- Punch.write if result = Punch.send(command, project)
61
- puts result.inspect
62
- else
63
- puts "Project required"
64
- end
65
- when 'out'
66
- Punch.write if result = Punch.out(project)
67
- puts result.inspect
68
- when 'log'
69
- if project
70
- if message = ARGV[2]
71
- Punch.write if result = Punch.log(project, message)
51
+
52
+ commands = {
53
+ 'status' => lambda { |project| puts Punch.status(project).inspect },
54
+ 'total' => lambda { |project| puts Punch.total(project, OPTIONS.merge(:format => true)).inspect },
55
+ 'in' => lambda do |project|
56
+ if project
57
+ if Punch.in(project)
58
+ Punch.write
59
+ else
60
+ puts "Already punched in to '#{project}'"
61
+ end
62
+ else
63
+ puts "Project required"
64
+ end
65
+ end,
66
+ 'delete' => lambda do |project|
67
+ if project
68
+ Punch.write if result = Punch.delete(project)
72
69
  puts result.inspect
73
70
  else
74
- puts "Message required"
71
+ puts "Project required"
72
+ end
73
+ end,
74
+ 'out' => lambda do |project|
75
+ if Punch.out(project)
76
+ Punch.write
77
+ else
78
+ message = 'Already punched out of '
79
+ message += project ? "'#{project}'" : 'all projects'
80
+ puts message
81
+ end
82
+ end,
83
+ 'log' => lambda do |project|
84
+ if project
85
+ if message = ARGV[2]
86
+ if Punch.log(project, message)
87
+ Punch.write
88
+ else
89
+ puts "Not punched in to '#{project}'"
90
+ end
91
+ else
92
+ puts "Message required"
93
+ end
94
+ else
95
+ puts "Project required"
75
96
  end
76
- else
77
- puts "Project required"
78
97
  end
98
+ }
99
+
100
+ if command_code = commands[command]
101
+ command_code.call(project)
102
+ else
103
+ puts "Command '#{command}' unknown"
79
104
  end
data/lib/punch.rb CHANGED
@@ -2,6 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  require 'yaml'
5
+ require 'punch/core_ext'
5
6
 
6
7
  module Punch
7
8
  class << self
@@ -83,12 +84,13 @@ module Punch
83
84
  end
84
85
 
85
86
  def total(*args)
87
+ options = args.last.is_a?(Hash) ? args.last : {}
86
88
  list_data = list(*args)
87
89
  if list_data.is_a?(Hash)
88
- list_data.inject({}) { |hash, (project, project_data)| hash.merge(project => do_total_time(project_data)) }
90
+ list_data.inject({}) { |hash, (project, project_data)| hash.merge(project => do_total_time(project_data, options)) }
89
91
  else
90
92
  return nil unless list_data
91
- do_total_time(list_data)
93
+ do_total_time(list_data, options)
92
94
  end
93
95
  end
94
96
 
@@ -117,8 +119,10 @@ module Punch
117
119
  project_data
118
120
  end
119
121
 
120
- def do_total_time(list_data)
121
- list_data.collect { |t| ((t['out'] || Time.now) - t['in']).to_i }.inject(0) { |sum, t| sum + t }
122
+ def do_total_time(list_data, options)
123
+ total = list_data.collect { |t| ((t['out'] || Time.now) - t['in']).to_i }.inject(0) { |sum, t| sum + t }
124
+ return total unless options[:format]
125
+ total.elapsed_time
122
126
  end
123
127
  end
124
128
  end
data/lib/punch/version.rb CHANGED
@@ -2,7 +2,7 @@ module Punch
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -14,11 +14,15 @@ describe 'punch command' do
14
14
  Object.send(:remove_const, const) if Object.const_defined?(const)
15
15
  end
16
16
 
17
- self.stubs(:puts)
17
+ @states = {}
18
+ %w[puts load write].each do |state|
19
+ @states[state] = states(state).starts_as('setup')
20
+ end
21
+
22
+ self.stubs(:puts).when(@states['puts'].is('setup'))
23
+ Punch.stubs(:load).when(@states['load'].is('setup'))
24
+ Punch.stubs(:write).when(@states['write'].is('setup'))
18
25
 
19
- Punch.stubs(:load)
20
- @test = states('test').starts_as('setup')
21
- Punch.stubs(:write).when(@test.is('setup'))
22
26
  @project = 'myproj'
23
27
  end
24
28
 
@@ -59,8 +63,8 @@ describe 'punch command' do
59
63
  end
60
64
 
61
65
  it 'should not write the data' do
62
- @test.become('test')
63
- Punch.expects(:write).never.when(@test.is('test'))
66
+ @states['write'].become('test')
67
+ Punch.expects(:write).never.when(@states['write'].is('test'))
64
68
  run_command('total')
65
69
  end
66
70
 
@@ -92,10 +96,16 @@ describe 'punch command' do
92
96
  Punch.expects(:total).with(nil, has_entry(:before => time))
93
97
  run_command('total', '--before', time_option)
94
98
  end
99
+
100
+ it 'should also pass the formatting option' do
101
+ time_option = '2008-08-26 09:47'
102
+ Punch.expects(:total).with(@project, has_entry(:format => true))
103
+ run_command('total', @project, '--before', time_option)
104
+ end
95
105
  end
96
106
 
97
- it 'should pass no options if none specified' do
98
- Punch.expects(:total).with(@project, {})
107
+ it 'should pass only the formatting option if no options specified' do
108
+ Punch.expects(:total).with(@project, {:format => true})
99
109
  run_command('total', @project)
100
110
  end
101
111
  end
@@ -128,15 +138,16 @@ describe 'punch command' do
128
138
  end
129
139
 
130
140
  it 'should not write the data' do
131
- @test.become('test')
132
- Punch.expects(:write).never.when(@test.is('test'))
141
+ @states['write'].become('test')
142
+ Punch.expects(:write).never.when(@states['write'].is('test'))
133
143
  run_command('status')
134
144
  end
135
145
  end
136
146
 
137
147
  describe "when the command is 'in'" do
138
148
  before :each do
139
- Punch.stubs(:in).when(@test.is('setup'))
149
+ @states['in'] = states('in').starts_as('setup')
150
+ Punch.stubs(:in).when(@states['write'].is('setup'))
140
151
  end
141
152
 
142
153
  it 'should load punch data' do
@@ -145,33 +156,40 @@ describe 'punch command' do
145
156
  end
146
157
 
147
158
  it 'should punch in to the given project' do
148
- @test.become('test')
149
- Punch.stubs(:write)
150
- Punch.expects(:in).with(@project).when(@test.is('test'))
151
- run_command('in', @project)
152
- end
153
-
154
- it 'should output the result' do
155
- result = 'result'
156
- Punch.stubs(:in).returns(result)
157
- self.expects(:puts).with(result.inspect)
159
+ Punch.expects(:in).with(@project)
158
160
  run_command('in', @project)
159
161
  end
160
162
 
161
163
  describe 'when punched in successfully' do
162
- it 'should write the data' do
163
- @test.become('test')
164
+ before :each do
164
165
  Punch.stubs(:in).returns(true)
165
- Punch.expects(:write).when(@test.is('test'))
166
+ end
167
+
168
+ it 'should write the data' do
169
+ Punch.expects(:write)
170
+ run_command('in', @project)
171
+ end
172
+
173
+ it 'should not print anything' do
174
+ @states['puts'].become('test')
175
+ self.expects(:puts).never.when(@states['puts'].is('test'))
166
176
  run_command('in', @project)
167
177
  end
168
178
  end
169
179
 
170
180
  describe 'when not punched in successfully' do
171
- it 'should not write the data' do
172
- @test.become('test')
181
+ before :each do
173
182
  Punch.stubs(:in).returns(false)
174
- Punch.expects(:write).never.when(@test.is('test'))
183
+ end
184
+
185
+ it 'should not write the data' do
186
+ @states['write'].become('test')
187
+ Punch.expects(:write).never.when(@states['write'].is('test'))
188
+ run_command('in', @project)
189
+ end
190
+
191
+ it 'should print a message' do
192
+ self.expects(:puts).with(regexp_matches(/already.+in/i))
175
193
  run_command('in', @project)
176
194
  end
177
195
  end
@@ -183,15 +201,15 @@ describe 'punch command' do
183
201
  end
184
202
 
185
203
  it 'should not punch in' do
186
- @test.become('test')
204
+ @states['in'].become('test')
187
205
  Punch.stubs(:write)
188
- Punch.expects(:in).never.when(@test.is('test'))
206
+ Punch.expects(:in).never.when(@states['in'].is('test'))
189
207
  run_command('in')
190
208
  end
191
209
 
192
210
  it 'should not write the data' do
193
- @test.become('test')
194
- Punch.expects(:write).never.when(@test.is('test'))
211
+ @states['write'].become('test')
212
+ Punch.expects(:write).never.when(@states['write'].is('test'))
195
213
  run_command('in')
196
214
  end
197
215
  end
@@ -217,35 +235,60 @@ describe 'punch command' do
217
235
  run_command('out')
218
236
  end
219
237
 
220
- it 'should output the result' do
221
- result = 'result'
222
- Punch.stubs(:out).returns(result)
223
- self.expects(:puts).with(result.inspect)
224
- run_command('out', @project)
225
- end
226
-
227
238
  describe 'when punched out successfully' do
228
- it 'should write the data' do
229
- @test.become('test')
239
+ before :each do
230
240
  Punch.stubs(:out).returns(true)
231
- Punch.expects(:write).when(@test.is('test'))
241
+ end
242
+
243
+ it 'should write the data' do
244
+ Punch.expects(:write)
232
245
  run_command('out', @project)
233
246
  end
247
+
248
+ it 'should not print anything' do
249
+ @states['puts'].become('test')
250
+ self.expects(:puts).never.when(@states['puts'].is('test'))
251
+ run_command('out', @project)
252
+ end
253
+
254
+ describe 'and no project given' do
255
+ it 'should not print anything' do
256
+ @states['puts'].become('test')
257
+ self.expects(:puts).never.when(@states['puts'].is('test'))
258
+ run_command('out')
259
+ end
260
+ end
234
261
  end
235
262
 
236
263
  describe 'when not punched out successfully' do
237
- it 'should not write the data' do
238
- @test.become('test')
264
+ before :each do
239
265
  Punch.stubs(:out).returns(false)
240
- Punch.expects(:write).never.when(@test.is('test'))
266
+ end
267
+
268
+ it 'should not write the data' do
269
+ @states['write'].become('test')
270
+ Punch.expects(:write).never.when(@states['write'].is('test'))
241
271
  run_command('out', @project)
242
272
  end
273
+
274
+ it 'should print a message' do
275
+ self.expects(:puts).with(regexp_matches(/already.+out/i))
276
+ run_command('out', @project)
277
+ end
278
+
279
+ describe 'and no project given' do
280
+ it 'should print a message' do
281
+ self.expects(:puts).with(regexp_matches(/already.+out/i))
282
+ run_command('out')
283
+ end
284
+ end
243
285
  end
244
286
  end
245
287
 
246
288
  describe "when the command is 'delete'" do
247
289
  before :each do
248
- Punch.stubs(:delete).when(@test.is('setup'))
290
+ @states['delete'] = states('delete').starts_as('setup')
291
+ Punch.stubs(:delete).when(@states['write'].is('setup'))
249
292
  end
250
293
 
251
294
  it 'should load punch data' do
@@ -254,9 +297,8 @@ describe 'punch command' do
254
297
  end
255
298
 
256
299
  it 'should delete the given project' do
257
- @test.become('test')
258
300
  Punch.stubs(:write)
259
- Punch.expects(:delete).with(@project).when(@test.is('test'))
301
+ Punch.expects(:delete).with(@project)
260
302
  run_command('delete', @project)
261
303
  end
262
304
 
@@ -269,18 +311,17 @@ describe 'punch command' do
269
311
 
270
312
  describe 'when deleted successfully' do
271
313
  it 'should write the data' do
272
- @test.become('test')
273
314
  Punch.stubs(:delete).returns(true)
274
- Punch.expects(:write).when(@test.is('test'))
315
+ Punch.expects(:write)
275
316
  run_command('delete', @project)
276
317
  end
277
318
  end
278
319
 
279
320
  describe 'when not deleted successfully' do
280
321
  it 'should not write the data' do
281
- @test.become('test')
322
+ @states['write'].become('test')
282
323
  Punch.stubs(:delete).returns(nil)
283
- Punch.expects(:write).never.when(@test.is('test'))
324
+ Punch.expects(:write).never.when(@states['write'].is('test'))
284
325
  run_command('delete', @project)
285
326
  end
286
327
  end
@@ -292,15 +333,15 @@ describe 'punch command' do
292
333
  end
293
334
 
294
335
  it 'should not delete' do
295
- @test.become('test')
336
+ @states['delete'].become('test')
296
337
  Punch.stubs(:write)
297
- Punch.expects(:delete).never.when(@test.is('test'))
338
+ Punch.expects(:delete).never.when(@states['delete'].is('test'))
298
339
  run_command('delete')
299
340
  end
300
341
 
301
342
  it 'should not write the data' do
302
- @test.become('test')
303
- Punch.expects(:write).never.when(@test.is('test'))
343
+ @states['write'].become('test')
344
+ Punch.expects(:write).never.when(@states['write'].is('test'))
304
345
  run_command('delete')
305
346
  end
306
347
  end
@@ -308,7 +349,8 @@ describe 'punch command' do
308
349
 
309
350
  describe "when the command is 'log'" do
310
351
  before :each do
311
- Punch.stubs(:log).when(@test.is('setup'))
352
+ @states['log'] = states('log').starts_as('setup')
353
+ Punch.stubs(:log).when(@states['log'].is('setup'))
312
354
  @message = 'log message'
313
355
  end
314
356
 
@@ -318,33 +360,41 @@ describe 'punch command' do
318
360
  end
319
361
 
320
362
  it 'should log a message for the given project' do
321
- @test.become('test')
322
363
  Punch.stubs(:write)
323
- Punch.expects(:log).with(@project, @message).when(@test.is('test'))
324
- run_command('log', @project, @message)
325
- end
326
-
327
- it 'should output the result' do
328
- result = 'result'
329
- Punch.stubs(:log).returns(result)
330
- self.expects(:puts).with(result.inspect)
364
+ Punch.expects(:log).with(@project, @message)
331
365
  run_command('log', @project, @message)
332
366
  end
333
367
 
334
368
  describe 'when logged successfully' do
335
- it 'should write the data' do
336
- @test.become('test')
369
+ before :each do
337
370
  Punch.stubs(:log).returns(true)
338
- Punch.expects(:write).when(@test.is('test'))
371
+ end
372
+
373
+ it 'should write the data' do
374
+ Punch.expects(:write)
375
+ run_command('log', @project, @message)
376
+ end
377
+
378
+ it 'should not print anything' do
379
+ @states['puts'].become('test')
380
+ self.expects(:puts).never.when(@states['write'].is('test'))
339
381
  run_command('log', @project, @message)
340
382
  end
341
383
  end
342
384
 
343
385
  describe 'when not deleted successfully' do
344
- it 'should not write the data' do
345
- @test.become('test')
386
+ before :each do
346
387
  Punch.stubs(:log).returns(false)
347
- Punch.expects(:write).never.when(@test.is('test'))
388
+ end
389
+
390
+ it 'should not write the data' do
391
+ @states['write'].become('test')
392
+ Punch.expects(:write).never.when(@states['write'].is('test'))
393
+ run_command('log', @project, @message)
394
+ end
395
+
396
+ it 'should print a message' do
397
+ self.expects(:puts).with(regexp_matches(/not.+in/i))
348
398
  run_command('log', @project, @message)
349
399
  end
350
400
  end
@@ -356,15 +406,15 @@ describe 'punch command' do
356
406
  end
357
407
 
358
408
  it 'should not log' do
359
- @test.become('test')
409
+ @states['log'].become('test')
360
410
  Punch.stubs(:write)
361
- Punch.expects(:log).never.when(@test.is('test'))
411
+ Punch.expects(:log).never.when(@states['log'].is('test'))
362
412
  run_command('log')
363
413
  end
364
414
 
365
415
  it 'should not write the data' do
366
- @test.become('test')
367
- Punch.expects(:write).never.when(@test.is('test'))
416
+ @states['write'].become('test')
417
+ Punch.expects(:write).never.when(@states['write'].is('test'))
368
418
  run_command('log')
369
419
  end
370
420
  end
@@ -376,17 +426,41 @@ describe 'punch command' do
376
426
  end
377
427
 
378
428
  it 'should not log' do
379
- @test.become('test')
429
+ @states['log'].become('test')
380
430
  Punch.stubs(:write)
381
- Punch.expects(:log).never.when(@test.is('test'))
431
+ Punch.expects(:log).never.when(@states['log'].is('test'))
382
432
  run_command('log', @project)
383
433
  end
384
434
 
385
435
  it 'should not write the data' do
386
- @test.become('test')
387
- Punch.expects(:write).never.when(@test.is('test'))
436
+ @states['write'].become('test')
437
+ Punch.expects(:write).never.when(@states['write'].is('test'))
388
438
  run_command('log', @project)
389
439
  end
390
440
  end
391
441
  end
442
+
443
+ describe 'when the command is unknown' do
444
+ it 'should not error' do
445
+ lambda { run_command('bunk') }.should_not raise_error
446
+ end
447
+
448
+ it 'should print a message' do
449
+ self.expects(:puts).with(regexp_matches(/command.+unknown/i))
450
+ run_command('bunk')
451
+ end
452
+
453
+ it 'should not write the data' do
454
+ @states['write'].become('test')
455
+ Punch.expects(:write).never.when(@states['write'].is('test'))
456
+ run_command('bunk')
457
+ end
458
+
459
+ it 'should not run any punch command' do
460
+ [:in, :out, :delete, :status, :total, :log].each do |command|
461
+ Punch.expects(command).never
462
+ end
463
+ run_command('bunk')
464
+ end
465
+ end
392
466
  end
data/spec/punch_spec.rb CHANGED
@@ -375,9 +375,6 @@ describe Punch do
375
375
  end
376
376
  end
377
377
  Punch.data = @data
378
-
379
- @test = states('test').starts_as('setup')
380
- Punch.stubs(:write).when(@test.is('setup'))
381
378
  end
382
379
 
383
380
  it 'should accept a project name' do
@@ -496,9 +493,6 @@ describe Punch do
496
493
  end
497
494
  end
498
495
  Punch.data = @data
499
-
500
- @test = states('test').starts_as('setup')
501
- Punch.stubs(:write).when(@test.is('setup'))
502
496
  end
503
497
 
504
498
  it 'should accept a project name' do
@@ -668,6 +662,10 @@ describe Punch do
668
662
  Punch.total(@project, :after => @now - 2001, :before => @now - 999).should == 1000
669
663
  end
670
664
 
665
+ it 'should format the time spent if passed a format option' do
666
+ Punch.total(@project, :format => true).should == "1:05:00"
667
+ end
668
+
671
669
  describe 'and is punched in' do
672
670
  before :each do
673
671
  @data[@project].push({ 'in' => @now - 25 })
@@ -708,6 +706,10 @@ describe Punch do
708
706
  it 'should respect options' do
709
707
  Punch.total(:after => @now - 51).should == { @projects[0] => 25, @projects[1] => 20, @projects[2] => 15}
710
708
  end
709
+
710
+ it 'should format the time spent if passed a format option' do
711
+ Punch.total(:format => true).should == { @projects[0] => "00:25", @projects[1] => "01:10", @projects[2] => "00:15"}
712
+ end
711
713
  end
712
714
  end
713
715
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: one_inch_punch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yossef Mendelssohn
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-26 00:00:00 -05:00
12
+ date: 2008-08-28 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency