business-days 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/business-days.rb +43 -3
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a49bf433a03ca5d8e9058919391bb4707f6df649
4
- data.tar.gz: 2971b1a8c7e3b2fa6df25a47a8fd5b42517f1b94
3
+ metadata.gz: cfbdb115a49dd6890f10db52c93f249963b20ccc
4
+ data.tar.gz: 2a62fd6c2adba50683dfaa484c47ba4c581bb453
5
5
  SHA512:
6
- metadata.gz: b4701bf981f722e78d28c28e9bd5cc6f91f9e5008203fd521769c8475c5a63f917f30ff43c79f7ecf1b93c7ee000fa9ce67dd7eb9dd69cae2308860bf8f3325a
7
- data.tar.gz: c56133be9637796533c307d288373a507982b3475e0d9114ebbfe808011ff3b098b9754a4fb0a52576a004f97eed8953ce6b3859ce37b72fa061f11f5ad2d5c4
6
+ metadata.gz: 8c161760f5f74a8d6261b8700051401f5b9352ce0c3b3735ad65c28b9fd56dddba90df848280653fe98f6778ce3992dcc225848995e2ec0005f6952210283dac
7
+ data.tar.gz: b6c3dd701d722704e68228280f6327a44c2069f565271a0e852df9f16436259a807f261aa51aeb97d80293ddaf04fd3b0d98932d91a9d1ac80b680a3183aa455
@@ -17,10 +17,9 @@ class BusinessDaysSingleton
17
17
  # Include holiday
18
18
  @holidays.push(holiday[:date])
19
19
 
20
- # 2. If Carnaval, include the day before and after it (they are bank holidays)
20
+ # 2. If Carnaval, include the day before it (it's a bank holiday)
21
21
  if holiday[:name] == "Carnaval"
22
22
  @holidays.push(holiday[:date] - 1.day)
23
- @holidays.push(holiday[:date] + 1.day)
24
23
  end
25
24
  end
26
25
 
@@ -51,7 +50,7 @@ class BusinessDaysSingleton
51
50
  date
52
51
  end
53
52
 
54
- # Add the given date as a non-business day (for all commands after this).
53
+ # Check if the given date is a business day
55
54
  #
56
55
  # @param date [Date] the non-business date to be add.
57
56
  # @return [Boolean] true if the given date is a business day, false otherwise.
@@ -112,5 +111,46 @@ class BusinessDaysSingleton
112
111
  end
113
112
  end
114
113
 
114
+ class BusinessDaysInstance
115
+ def initialize(holidays)
116
+ @holidays = []
117
+
118
+ holidays.each do |holiday|
119
+ # Include holiday
120
+ @holidays.push(Date.parse(holiday))
121
+ end
122
+ end
123
+
124
+ # Check if the given date is a business day
125
+ #
126
+ # @param date [Date] the non-business date to be add.
127
+ # @return [Boolean] true if the given date is a business day, false otherwise.
128
+ def business_day?(date)
129
+ raise ArgumentError.new('Not a date') unless date.kind_of?(Date)
130
+
131
+ weekend = [6, 7].include?(date.cwday)
132
+ !(weekend || @holidays.include?(date))
133
+ end
134
+
135
+ # Count business days between two dates.
136
+ #
137
+ # @param from [Date] the starting date (inclusive)
138
+ # @param to [Date] the ending date (inclusive)
139
+
140
+ # @return [Integer] number of business days between the two dates
141
+ def business_days(from, to)
142
+ raise ArgumentError.new('Not a date: from') unless from.kind_of?(Date)
143
+ raise ArgumentError.new('Not a date: to') unless to.kind_of?(Date)
144
+ count = 0
145
+
146
+ while from <= to
147
+ count +=1 if self.business_day?(from)
148
+ from = from + 1.day
149
+ end
150
+
151
+ count
152
+ end
153
+ end
154
+
115
155
  # Avoid calling the instance method on every call
116
156
  BusinessDays = BusinessDaysSingleton.instance
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business-days
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Allan Costa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-04 00:00:00.000000000 Z
11
+ date: 2018-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport