time_clock 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ded90791cbcc02d14581f1afdfa801f3e98e75d
4
- data.tar.gz: a2191cfca3a38027625759cddd9de7a2a2755658
3
+ metadata.gz: c3b80eff32b5fadb72dbeff176ee5b2ac58a9f09
4
+ data.tar.gz: 2fa81d1db7ea7835a56b0afeb7ad0768c27c4244
5
5
  SHA512:
6
- metadata.gz: d8c7b904ac72cf3a36b75bac46b4887837788f7e3f0f44584a81412cc19629f1eeb00807c4cfc9db2964de89cec24d546c19c55e8152ee31ff3829a7a90491ab
7
- data.tar.gz: d57345f61c2a7eb57c0a1c5de6c0442efd2a16603a44fb37407e2c8e940bbc30bf0e89474cf90eb9135e6b509f0e031e9f626b1a2bd971d627114cb01abd0f74
6
+ metadata.gz: 96926eed8ac4f075d0aeda39b9f3f0cdf8f0b0f6a90011e616b922285f91eb7d95a898e72c63c6e02e2faa22161867e57a0f697585a8aca60a34bf2f9fc38382
7
+ data.tar.gz: 68b0052866e7a1514c385f24ea1fd4801bbfc8476f00ae27addbb306100f8586cea3e067f9dc0ea8093a2a80e3c830c4d2ee02e891be3e202a59f5ce843aba86
data/README.md CHANGED
@@ -17,10 +17,12 @@ You can also make custom calendars for each calculation (see Calendars section f
17
17
 
18
18
  **Things you may like about this library:**
19
19
 
20
- - No dependencies.
21
- - Extremely simple codebase. Only 80 lines of code.
22
20
  - Works with any kind of `Time`, including mixed types.
21
+ - Extremely simple codebase. Less than 100 lines of code.
23
22
  - Easy to customize with totally custom business calendars.
23
+ - No dependencies.
24
+ - 100% test coverage.
25
+ - [![Code Climate](https://codeclimate.com/repos/5214185789af7e04ce021d7b/badges/ebf8537627c6cc30178c/gpa.png)](https://codeclimate.com/repos/5214185789af7e04ce021d7b/feed)
24
26
 
25
27
  ## Configuration
26
28
 
@@ -1,5 +1,6 @@
1
1
  module TimeClock
2
2
  class Calendar
3
+
3
4
  class OverlappingShiftError < ArgumentError; end
4
5
 
5
6
  attr_reader :shifts
@@ -10,12 +11,12 @@ module TimeClock
10
11
 
11
12
  def add_shift(new_shift)
12
13
  starting_shift_count = @shifts.size
13
- @shifts.each_with_index do |shift, i|
14
+ shifts.each_with_index do |shift, index|
14
15
  raise OverlappingShiftError if new_shift.overlaps?(shift)
15
- break @shifts.insert(i, new_shift) if shift.start_time > new_shift.end_time
16
+ break shifts.insert(index, new_shift) if shift.start_time > new_shift.end_time
16
17
  end
17
- @shifts << new_shift unless @shifts.size > starting_shift_count
18
- @shifts
18
+ shifts << new_shift unless shifts.size > starting_shift_count
19
+ shifts
19
20
  end
20
21
 
21
22
  end
@@ -1,14 +1,17 @@
1
1
  module TimeClock
2
2
  class Comparison
3
3
 
4
+ class NilCalendarError < ArgumentError; end
5
+
4
6
  attr_reader :start_time, :end_time, :calendar
5
7
 
6
8
  def initialize(start_time, end_time, calendar=TimeClock.default_calendar)
9
+ raise NilCalendarError unless calendar
7
10
  @start_time, @end_time, @calendar = start_time, end_time, calendar
8
11
  end
9
12
 
10
13
  def period
11
- @period ||= Shift.new(start_time, end_time)
14
+ @period ||= Shift.new(earlier_time, later_time)
12
15
  end
13
16
 
14
17
  def seconds
@@ -17,5 +20,15 @@ module TimeClock
17
20
  end
18
21
  end
19
22
 
23
+ private
24
+
25
+ def earlier_time
26
+ start_time < end_time ? start_time : end_time
27
+ end
28
+
29
+ def later_time
30
+ end_time > start_time ? end_time : start_time
31
+ end
32
+
20
33
  end
21
34
  end
@@ -1,5 +1,6 @@
1
1
  module TimeClock
2
2
  class Shift
3
+
3
4
  class EndTimeAfterStartTimeError < ArgumentError; end
4
5
 
5
6
  attr_reader :start_time, :end_time
@@ -1,3 +1,3 @@
1
1
  module TimeClock
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/time_clock.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  require "time_clock/version"
2
2
 
3
- require "time_clock/core_ext/time"
4
-
5
3
  require "time_clock/shift"
6
4
  require "time_clock/calendar"
7
5
  require "time_clock/comparison"
8
6
 
7
+ require "time_clock/core_ext/time"
8
+
9
9
  module TimeClock
10
10
 
11
11
  def self.default_calendar=(value)
@@ -43,7 +43,7 @@ module TimeClock
43
43
  expect{subject.add_shift(new_shift)}.to raise_error Calendar::OverlappingShiftError
44
44
  end
45
45
  end
46
-
46
+
47
47
  end
48
48
 
49
49
  end
@@ -27,6 +27,21 @@ module TimeClock
27
27
  expect(subject.period).to eq Shift.new(start_time, end_time)
28
28
  end
29
29
 
30
+ context "when the start time is after the end time" do
31
+ let(:start_time) { Time.now + 3600 }
32
+ let(:end_time) { Time.now }
33
+ it "should create a shift with flipped around times" do
34
+ expect(subject.period).to eq Shift.new(end_time, start_time)
35
+ end
36
+ end
37
+
38
+ context "when there is no default calendar and none is provided" do
39
+ it "should raise an error on initialization" do
40
+ TimeClock.stub(:default_calendar) { nil }
41
+ expect{described_class.new(start_time, end_time)}.to raise_error Comparison::NilCalendarError
42
+ end
43
+ end
44
+
30
45
  context "when calculating seconds between" do
31
46
  let(:calendar) do
32
47
  Calendar.new.tap do |c|
@@ -2,9 +2,22 @@ require 'spec_helper'
2
2
 
3
3
  describe Time do
4
4
  subject { Time.new(2013,8,13,14,15) }
5
+ let(:end_time) { Time.new(2013,8,13,14,18) }
5
6
 
6
- it "should respond to #seconds_until" do
7
- expect(subject).to respond_to :business_seconds_until
7
+ it "should calculate business_seconds_until" do
8
+ comparison = double("comparison", seconds: 3)
9
+ expect(TimeClock::Comparison).to receive(:new).with(subject, end_time).and_return(comparison)
10
+ expect(subject.business_seconds_until(end_time)).to eq comparison.seconds
11
+ end
12
+
13
+ it "should calculate business minutes until" do
14
+ Time.any_instance.stub(:business_seconds_until) { 60 }
15
+ expect(subject.business_minutes_until(end_time)).to eq 1
16
+ end
17
+
18
+ it "should calculate business hours until" do
19
+ Time.any_instance.stub(:business_seconds_until) { 3500 }
20
+ expect(subject.business_hours_until(end_time)).to eq 1
8
21
  end
9
22
 
10
23
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,11 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
 
4
+ require 'simplecov'
5
+ SimpleCov.start do
6
+ add_filter "/spec/"
7
+ end
8
+
4
9
  require 'time_clock'
5
10
 
6
11
  RSpec.configure do |config|
data/time_clock.gemspec CHANGED
@@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "guard-rspec"
25
25
  spec.add_development_dependency "metric_fu"
26
+ spec.add_development_dependency "simplecov"
27
+ spec.add_development_dependency "simplecov-rcov-text"
26
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_clock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - barelyknown
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov-rcov-text
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description: Calculate the business time for a period of time.
84
112
  email:
85
113
  - sean@buytruckload.com