business_calendar 0.0.17 → 0.0.18

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
  SHA256:
3
- metadata.gz: 482e0f0674387288534d373978b3c93cad967259af5f631e1cb5c91957d83be2
4
- data.tar.gz: 3b07e79f60daea35982c9d1ddeb14ea85bad885527aabe7cd5be23f5d92918ff
3
+ metadata.gz: 82cbe11f46934df2bcc820bd3725e10601f7e2942ef46285e191e96af392faee
4
+ data.tar.gz: 1bc29d426eb24de502a7b62c26300e841b914c74f66c6ab99193e2b862031103
5
5
  SHA512:
6
- metadata.gz: 816ac828fd2e6625c3ec587627060872d068e909eeaf8d21468e25057237da40b58461165b6acfbbd131093aa0349291516e8a233bff5e303203081bd2ee0637
7
- data.tar.gz: 507a5bbc258ba6f92bc63a6b454da9a8f6aaa0c7fea2b778e6f71160aa93e0dd45fc3e52971b4f83f2c1b487364e29afc4f816c4ef32e11287168efd3b69b126
6
+ metadata.gz: 8aeaca2443bb3a4cfdf40dd6a016803b3e16bbf714104efb725ca417efcb9aecd76a5dd9cc7b334c6a2c79e66a882db31254b72cf834892a00a00b90be3da5f6
7
+ data.tar.gz: 76cc84dc341780de4f5c90899f101d0cc3215fc3b930602f982be66bf2f74b876503ad067abfa56d28be10fd5acee278db90a49e95740fe66c5e7a1f7e098de2
@@ -4,6 +4,7 @@ class BusinessCalendar::Calendar
4
4
  # @param [Proc[Date -> Boolean]] a proc which returns whether or not a date is a
5
5
  # holiday.
6
6
  def initialize(holiday_determiner)
7
+ @is_holiday = {}
7
8
  @holiday_determiner = holiday_determiner
8
9
  end
9
10
 
@@ -11,7 +12,8 @@ class BusinessCalendar::Calendar
11
12
  # @return [Boolean] Whether or not this calendar's list of holidays includes <date>.
12
13
  def is_holiday?(date)
13
14
  date = date.send(:to_date) if date.respond_to?(:to_date, true)
14
- holiday_determiner.call(date)
15
+ return @is_holiday[date] unless @is_holiday[date].nil?
16
+ @is_holiday[date] = holiday_determiner.call(date)
15
17
  end
16
18
 
17
19
  # @param [Date] date
@@ -36,8 +38,9 @@ class BusinessCalendar::Calendar
36
38
  return subtract_business_days(date_or_dates, -num) if num < 0
37
39
 
38
40
  with_one_or_many(date_or_dates) do |date|
39
- start = nearest_business_day(date, initial_direction)
40
- num.times.reduce(start) { |d, _| following_business_day(d) }
41
+ d = nearest_business_day(date, initial_direction)
42
+ num.times { d = following_business_day(d) }
43
+ d
41
44
  end
42
45
  end
43
46
  alias :add_business_day :add_business_days
@@ -51,7 +54,8 @@ class BusinessCalendar::Calendar
51
54
  return add_business_days(date_or_dates, -num) if num < 0
52
55
 
53
56
  with_one_or_many(date_or_dates) do |date|
54
- num.times.reduce(date) { |d, _| preceding_business_day(d) }
57
+ num.times { date = preceding_business_day(date) }
58
+ date
55
59
  end
56
60
  end
57
61
  alias :subtract_business_day :subtract_business_days
@@ -102,13 +106,12 @@ class BusinessCalendar::Calendar
102
106
 
103
107
  private
104
108
  def with_one_or_many(thing_or_things)
105
- is_array = thing_or_things.is_a? Enumerable
106
- things = is_array ? thing_or_things : [thing_or_things]
107
-
108
- results = things.collect do |thing|
109
- yield thing
109
+ if thing_or_things.is_a? Enumerable
110
+ thing_or_things.collect do |thing|
111
+ yield thing
112
+ end
113
+ else
114
+ yield thing_or_things
110
115
  end
111
-
112
- return is_array ? results : results.first
113
116
  end
114
117
  end
@@ -1,3 +1,3 @@
1
1
  module BusinessCalendar
2
- VERSION = "0.0.17"
2
+ VERSION = "0.0.18"
3
3
  end
@@ -2,8 +2,12 @@ module BusinessCalendar
2
2
  CountryNotSupported = Class.new(StandardError)
3
3
  OrganizationNotSupported = Class.new(StandardError)
4
4
  class << self
5
- def for(country)
6
- Calendar.new(holiday_determiner(country))
5
+ def for(country, options = {})
6
+ if options["use_cached_calendar"]
7
+ calendar_cache[country] ||= Calendar.new(holiday_determiner(country))
8
+ else
9
+ Calendar.new(holiday_determiner(country))
10
+ end
7
11
  end
8
12
 
9
13
  def for_organization(org)
@@ -34,6 +38,10 @@ module BusinessCalendar
34
38
  :additions_only => cfg['additions_only'] )
35
39
  end
36
40
 
41
+ def calendar_cache
42
+ @calendar_cache ||= {}
43
+ end
44
+
37
45
  def config(country)
38
46
  @config ||= load_config
39
47
  @config[country.to_s]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Nubel