gb_work_day 0.0.3 → 0.0.4

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: 72786d75440945859cc671242a9413de0905461d
4
- data.tar.gz: 5253d99feed1c3cc620dc5b5c77eff242988a116
3
+ metadata.gz: 310aaaee9f37fe3ccdbe077d1ed7dc7eb9768728
4
+ data.tar.gz: 031751336c41a70bde3ac7313a9050f929e11932
5
5
  SHA512:
6
- metadata.gz: 70ee87050fdd169e48012cafcaf67d737f3368db20e7fa5ca618b442b1f272a431d3bb5d50b2826c32791ca977e626a8baa8e9a2ee2d690d15b233fe867af43f
7
- data.tar.gz: e82fc2e1e223349fac3754c08a5c29d05206a703ecb1b37c75f30b4a9f5b0afd13c201fbbd406a6ee69ce78117608240b61a6151e15fb8a7ec2d6029c777b864
6
+ metadata.gz: adc76599cf592b96a78431284a4f36ba2b329b434daa0fbda77a9e2d3d32a1b65860935ff2aadd47509b08ad8f1139c545a386ba95dd90071e0564bfb8cda098
7
+ data.tar.gz: b74120d209b3f7021aed67e69e817a896c75aa5a7b9c93c59b7250dfcff5747dfd9bcda4bed8239c26c17b7fdc49b0f54c00d77087167b5d18461f20425d05d3
@@ -0,0 +1,119 @@
1
+ # GBWorkDay
2
+
3
+ Library to make calculation on work days.
4
+ Unlike others libraries like [`business_time`](https://github.com/bokmann/business_time),
5
+ [`working_hours`](https://github.com/Intrepidd/working_hours), [`biz`](https://github.com/zendesk/biz)
6
+ it operates on whole days, not hours.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'gb_work_day'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install gb_work_day
23
+
24
+ ## Usage
25
+
26
+ ### Work week
27
+
28
+ Set default work week for current thread
29
+
30
+ ```ruby
31
+ beginning_of_week = 1 #Monday
32
+ work_days = 5 #wrokd days are Monday-Friday
33
+ GBWorkDay::WorkWeek.current = GBWorkWeek.new(work_days, beginning_of_week)
34
+ ```
35
+
36
+ or if you want to setup per instance
37
+
38
+ ```ruby
39
+ beginning_of_week = 1 #Monday
40
+ work_days = 5 #wrokd days are Monday-Friday
41
+ week = GBWorkWeek.new(work_days, beginning_of_week)
42
+ my_date = Date.today.to_work_date(week)
43
+ ```
44
+
45
+ or
46
+
47
+ ```ruby
48
+ beginning_of_week = 1 #Monday
49
+ work_days = 5 #wrokd days are Monday-Friday
50
+ week = GBWorkWeek.new(work_days, beginning_of_week)
51
+ my_date = my_date
52
+ #some code here
53
+ my_date.week = week
54
+ ```
55
+
56
+ ### Date and Time operation
57
+
58
+ Check if today is a work day
59
+
60
+ ```ruby
61
+ Date.today.work?
62
+ ```
63
+
64
+ or
65
+
66
+ ```ruby
67
+ Time.now.work?
68
+ ```
69
+
70
+ Check if today is holiday
71
+
72
+ ```ruby
73
+ Date.today.free?
74
+ ```
75
+
76
+ or
77
+
78
+ ```ruby
79
+ Time.now.free?
80
+ ```
81
+
82
+ Get next working day
83
+
84
+ ```ruby
85
+ Date.today.next_work_day
86
+ ```
87
+
88
+ or
89
+
90
+ ```ruby
91
+ Time.now.next_work_day
92
+ ```
93
+
94
+
95
+ You can also make more complicated calculations
96
+
97
+ ```ruby
98
+ delivery_date = Date.today + 10.work_days
99
+ ```
100
+
101
+ or
102
+
103
+ ```ruby
104
+ amount_of_work = (start_date.to_work_date - end_date).work_days
105
+ ```
106
+
107
+ ## Contributing
108
+
109
+ 1. Fork it ( https://github.com/GenieBelt/gb-works-day/fork )
110
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
111
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
112
+ 4. Push to the branch (`git push origin my-new-feature`)
113
+ 5. Create a new Pull Request
114
+
115
+ ## Alternatives
116
+
117
+ * [`business_time`](https://github.com/bokmann/business_time)
118
+ * [`working_hours`](https://github.com/Intrepidd/working_hours)
119
+ * [`biz`](https://github.com/zendesk/biz)
@@ -38,7 +38,15 @@ class Date
38
38
  # Return next working day
39
39
  # @return [Time]
40
40
  def next_work_day
41
- self + GBWorkDay::Duration.new(1, default_week)
41
+ if default_week.free_day? self
42
+ next_day = self
43
+ while default_week.free_day? next_day
44
+ next_day += 1
45
+ end
46
+ next_day
47
+ else
48
+ self + GBWorkDay::Duration.new(1, default_week)
49
+ end
42
50
  end
43
51
 
44
52
  # Get date object for calculating working days
@@ -39,7 +39,15 @@ class Time
39
39
  # Return next working day
40
40
  # @return [Time]
41
41
  def next_work_day
42
- self + GBWorkDay::Duration.new(1, default_week)
42
+ if default_week.free_day? self
43
+ next_day = self
44
+ while default_week.free_day? next_day
45
+ next_day += GBWorkDay::Duration::SEC_IN_DAY
46
+ end
47
+ next_day
48
+ else
49
+ self + GBWorkDay::Duration.new(1, default_week)
50
+ end
43
51
  end
44
52
 
45
53
  # Get time object for calculating working days
@@ -97,15 +97,23 @@ module GBWorkDay
97
97
  symbol *= -1
98
98
  work_days_left *= -1
99
99
  end
100
+ while @week.free_day? time
101
+ time = sum_normal_days time, symbol
102
+ end
100
103
  while work_days_left > 0
101
- if time.is_a? ::Date
102
- time += symbol * 1
103
- else
104
- time += (symbol * SEC_IN_DAY)
105
- end
104
+ time = sum_normal_days time, symbol
106
105
  work_days_left -= 1 if @week.work_day? time
107
106
  end
108
107
  time
109
108
  end
109
+
110
+ def sum_normal_days(time, days)
111
+ if time.is_a? ::Date
112
+ time += days * 1
113
+ else
114
+ time += (days * SEC_IN_DAY)
115
+ end
116
+ time
117
+ end
110
118
  end
111
119
  end
@@ -1,3 +1,3 @@
1
1
  module GBWorkDay
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -57,8 +57,12 @@ describe 'Date extensions' do
57
57
 
58
58
  it 'should properly add work duration' do
59
59
  monday = Date.today.beginning_of_week
60
+ sunday = monday - 1
60
61
  wednesday = monday + 2.days
61
62
  friday = monday + 4.days
63
+ expect(sunday + 1.work_day).to eq (monday + 1.days)
64
+ expect(sunday + 1.work_day - 1.work_day).to eq monday
65
+ expect(sunday + 6.work_day).to eq (monday + 8.days)
62
66
  expect(monday + 1.work_day).to eq (monday + 1.days)
63
67
  expect(friday + 1.work_day).to eq (friday + 3.days)
64
68
  expect(wednesday + 2.work_day).to eq (wednesday + 2.days)
@@ -57,8 +57,11 @@ describe 'Time extensions' do
57
57
 
58
58
  it 'should properly add work duration' do
59
59
  monday = Time.now.beginning_of_week
60
+ sunday = monday - 1.day
60
61
  wednesday = monday + 2.days
61
62
  friday = monday + 4.days
63
+ expect(sunday + 1.work_day).to eq (monday + 1.days)
64
+ expect(sunday + 1.work_day - 1.work_day).to eq monday
62
65
  expect(monday + 1.work_day).to eq (monday + 1.days)
63
66
  expect(friday + 1.work_day).to eq (friday + 3.days)
64
67
  expect(wednesday + 2.work_day).to eq (wednesday + 2.days)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gb_work_day
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kacper Kawecki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-03 00:00:00.000000000 Z
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,6 +96,7 @@ files:
96
96
  - ".idea/modules.xml"
97
97
  - ".idea/runConfigurations/specs.xml"
98
98
  - Gemfile
99
+ - README.md
99
100
  - Rakefile
100
101
  - gb_work_day.gemspec
101
102
  - lib/gb_work_day.rb