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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2d2cb8f2fd7a20e32ebd77079819fd74a32e8d0
4
- data.tar.gz: 9490bfa2e494111d15b3402b88ae0b4181ba8f51
3
+ metadata.gz: 251d7f3cf6dbd183f65412f3819a84c6eb567753
4
+ data.tar.gz: 33fe6680175dcadb13b726680ce7a6bc62a4656c
5
5
  SHA512:
6
- metadata.gz: 565f7bcaa097777c83efbfaefb54e09ba96608f910fe4b823a1a14625419ba1177c87dfbbaf7d2566b847a75653669a65a0bf9730015e776ce69a780dff563e8
7
- data.tar.gz: cde6cbeecffd912f1673b0174dd425ee2e43ab4ff8027d3124d19a0dcf5c6a43c45f358b80ea4387ef74aeb3af0b45d758693db91c365af603cc4633285fbf8c
6
+ metadata.gz: 310cf0ac1b4fa905d42909ad8918e98156ab6f9a5ae08062f0c859cea9c66d390bdc7f450cbe595630fc792b6889ea3cd4b1760f29b4ec7b6fd906627a292354
7
+ data.tar.gz: 569613ba3db4ff52c7f8c6e83acda495c37d75377d0c5d422ec29f4c68f79b2d75d6a1682c1c92fb45ed2ca2d4a3e1155a77ecb8afac2266c2425a2967eae738
@@ -1,3 +1,9 @@
1
+ ## 1.4.0 - December 24, 2014
2
+
3
+ - Add support for custom calendar load paths
4
+ - Remove the 'sepa' calendar
5
+
6
+
1
7
  ## 1.3.0 - December 2, 2014
2
8
 
3
9
  - Add `Calendar#previous_business_day`
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
@@ -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
- path = File.join(File.dirname(__FILE__), 'data', "#{calendar}.yml")
7
- raise "No such calendar '#{calendar}'" unless File.exists?(path)
8
- yaml = YAML.load_file(path)
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
- @lock.synchronize do
15
- @cache ||= { }
16
- unless @cache.include?(calendar)
17
- @cache[calendar] = self.load(calendar)
18
- end
19
- @cache[calendar]
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
 
@@ -1,3 +1,3 @@
1
1
  module Business
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -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 }
@@ -0,0 +1,9 @@
1
+ working_days:
2
+ - monday
3
+ - tuesday
4
+ - wednesday
5
+ - thursday
6
+ - friday
7
+
8
+ holidays:
9
+ - January 1st, 2015
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.3.0
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-02 00:00:00.000000000 Z
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