business 1.3.0 → 1.4.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +11 -1
- data/lib/business/calendar.rb +22 -9
- data/lib/business/version.rb +1 -1
- data/spec/calendar_spec.rb +28 -0
- data/spec/fixtures/calendars/bacs.yml +9 -0
- data/{lib/business/data/sepa.yml → spec/fixtures/calendars/ecb.yml} +0 -0
- metadata +6 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 251d7f3cf6dbd183f65412f3819a84c6eb567753
|
|
4
|
+
data.tar.gz: 33fe6680175dcadb13b726680ce7a6bc62a4656c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 310cf0ac1b4fa905d42909ad8918e98156ab6f9a5ae08062f0c859cea9c66d390bdc7f450cbe595630fc792b6889ea3cd4b1760f29b4ec7b6fd906627a292354
|
|
7
|
+
data.tar.gz: 569613ba3db4ff52c7f8c6e83acda495c37d75377d0c5d422ec29f4c68f79b2d75d6a1682c1c92fb45ed2ca2d4a3e1155a77ecb8afac2266c2425a2967eae738
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -49,6 +49,17 @@ calendar.business_day?(Date.parse("Sunday, 8 June 2014"))
|
|
|
49
49
|
# => false
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
### Custom calendars
|
|
53
|
+
|
|
54
|
+
To use a calendar you've written yourself, you need to add the directory it's
|
|
55
|
+
stored in as an additional calendar load path:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
Business::Calendar.additional_load_paths = ['path/to/your/calendar/directory']
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
You can then load the calendar as normal.
|
|
62
|
+
|
|
52
63
|
### Business day arithmetic
|
|
53
64
|
|
|
54
65
|
The `add_business_days` and `subtract_business_days` are used to perform
|
|
@@ -86,7 +97,6 @@ calendar.business_days_between(date, date + 7)
|
|
|
86
97
|
# => 5
|
|
87
98
|
```
|
|
88
99
|
|
|
89
|
-
|
|
90
100
|
## But other libraries already do this
|
|
91
101
|
|
|
92
102
|
Another gem, [business_time](https://github.com/bokmann/business_time), also
|
data/lib/business/calendar.rb
CHANGED
|
@@ -2,21 +2,34 @@ require 'yaml'
|
|
|
2
2
|
|
|
3
3
|
module Business
|
|
4
4
|
class Calendar
|
|
5
|
+
class << self
|
|
6
|
+
attr_accessor :additional_load_paths
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def self.calendar_directories
|
|
10
|
+
directories = @additional_load_paths || []
|
|
11
|
+
directories + [File.join(File.dirname(__FILE__), 'data')]
|
|
12
|
+
end
|
|
13
|
+
private_class_method :calendar_directories
|
|
14
|
+
|
|
5
15
|
def self.load(calendar)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
16
|
+
directory = calendar_directories.find do |dir|
|
|
17
|
+
File.exists?(File.join(dir, "#{calendar}.yml"))
|
|
18
|
+
end
|
|
19
|
+
raise "No such calendar '#{calendar}'" unless directory
|
|
20
|
+
|
|
21
|
+
yaml = YAML.load_file(File.join(directory, "#{calendar}.yml"))
|
|
9
22
|
self.new(holidays: yaml['holidays'], working_days: yaml['working_days'])
|
|
10
23
|
end
|
|
11
24
|
|
|
12
25
|
@lock = Mutex.new
|
|
13
26
|
def self.load_cached(calendar)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
@lock.synchronize do
|
|
28
|
+
@cache ||= { }
|
|
29
|
+
unless @cache.include?(calendar)
|
|
30
|
+
@cache[calendar] = self.load(calendar)
|
|
31
|
+
end
|
|
32
|
+
@cache[calendar]
|
|
20
33
|
end
|
|
21
34
|
end
|
|
22
35
|
|
data/lib/business/version.rb
CHANGED
data/spec/calendar_spec.rb
CHANGED
|
@@ -21,6 +21,34 @@ describe Business::Calendar do
|
|
|
21
21
|
it { is_expected.to be_a Business::Calendar }
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
+
context "when given a calendar from a custom directory" do
|
|
25
|
+
before do
|
|
26
|
+
Business::Calendar.additional_load_paths = [
|
|
27
|
+
File.join(File.dirname(__FILE__), 'fixtures', 'calendars')
|
|
28
|
+
]
|
|
29
|
+
end
|
|
30
|
+
after { Business::Calendar.additional_load_paths = nil }
|
|
31
|
+
subject { Business::Calendar.load("ecb") }
|
|
32
|
+
|
|
33
|
+
it "loads the yaml file" do
|
|
34
|
+
expect(YAML).to receive(:load_file) { |path|
|
|
35
|
+
expect(path).to match(/ecb\.yml$/)
|
|
36
|
+
}.and_return({})
|
|
37
|
+
subject
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it { is_expected.to be_a Business::Calendar }
|
|
41
|
+
|
|
42
|
+
context "that also exists as a default calendar" do
|
|
43
|
+
subject { Business::Calendar.load("bacs") }
|
|
44
|
+
|
|
45
|
+
it "uses the custom calendar" do
|
|
46
|
+
expect(subject.business_day?(Date.parse("25th December 2014"))).
|
|
47
|
+
to eq(true)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
24
52
|
context "when given an invalid calendar" do
|
|
25
53
|
subject { Business::Calendar.load("invalid-calendar") }
|
|
26
54
|
specify { expect{ subject }.to raise_error }
|
|
File without changes
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: business
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Harry Marr
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-12-
|
|
11
|
+
date: 2014-12-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -55,11 +55,12 @@ files:
|
|
|
55
55
|
- lib/business.rb
|
|
56
56
|
- lib/business/calendar.rb
|
|
57
57
|
- lib/business/data/bacs.yml
|
|
58
|
-
- lib/business/data/sepa.yml
|
|
59
58
|
- lib/business/data/target.yml
|
|
60
59
|
- lib/business/data/weekdays.yml
|
|
61
60
|
- lib/business/version.rb
|
|
62
61
|
- spec/calendar_spec.rb
|
|
62
|
+
- spec/fixtures/calendars/bacs.yml
|
|
63
|
+
- spec/fixtures/calendars/ecb.yml
|
|
63
64
|
homepage: https://github.com/gocardless/business
|
|
64
65
|
licenses:
|
|
65
66
|
- MIT
|
|
@@ -86,3 +87,5 @@ specification_version: 4
|
|
|
86
87
|
summary: Date calculations based on business calendars
|
|
87
88
|
test_files:
|
|
88
89
|
- spec/calendar_spec.rb
|
|
90
|
+
- spec/fixtures/calendars/bacs.yml
|
|
91
|
+
- spec/fixtures/calendars/ecb.yml
|