swedish_holidays 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ require 'yaml'
2
+ require 'date'
3
+ require 'forwardable'
4
+
5
+ module SwedishHolidays
6
+ DATA_DIR = File.expand_path('../../../data', __FILE__).freeze
7
+
8
+ class Holiday
9
+ extend Forwardable
10
+ include Comparable
11
+
12
+ attr_reader :date, :name
13
+
14
+ class << self
15
+ def holiday?(date)
16
+ !find(date).nil?
17
+ end
18
+
19
+ def find(date)
20
+ year = date.year
21
+ load(year)
22
+ loaded[year][date.yday]
23
+ end
24
+
25
+ def each(year = Date.today.year)
26
+ load(year)
27
+ holidays = loaded[year.to_i].values
28
+ return holidays.each unless block_given?
29
+ holidays.each { |holiday| yield holiday }
30
+ end
31
+
32
+ private
33
+
34
+ def file(year)
35
+ File.join(DATA_DIR, year.to_s)
36
+ end
37
+
38
+ def load(year)
39
+ return unless loaded[year.to_i].empty?
40
+ validate_has_year! year
41
+ holidays = YAML.load_file file(year.to_s)
42
+ holidays.each do |hash|
43
+ holiday = new(hash)
44
+ loaded[year.to_i][holiday.yday] = holiday
45
+ end
46
+ end
47
+
48
+ def validate_has_year!(year)
49
+ err_msg = "Sorry I don't have data for year \"#{year}\""
50
+ raise Error, err_msg unless File.exist? file(year)
51
+ end
52
+
53
+ def loaded
54
+ @loaded ||= Hash.new { |hash, key| hash[key] = {} }
55
+ end
56
+ end
57
+
58
+ def initialize(attr)
59
+ @date = Date.parse(attr[:datum])
60
+ @name = attr[:namn]
61
+ end
62
+
63
+ delegate [:wday, :yday] => :date
64
+
65
+ def <=>(other)
66
+ date <=> other.date
67
+ end
68
+
69
+ def to_s
70
+ "#{date.strftime}: #{name}"
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,48 @@
1
+ require 'date'
2
+ require 'swedish_holidays/holiday'
3
+
4
+ module SwedishHolidays
5
+ module Utils
6
+ class << self
7
+ def to_date(arg = nil)
8
+ case arg
9
+ when NilClass
10
+ Date.today
11
+ when /\d{4}-\d{2}-\d{2}/
12
+ Date.parse(arg)
13
+ when Date
14
+ arg
15
+ else
16
+ raise Error, "Don't know how to convert #{arg} (#{arg.class}) to Date"
17
+ end
18
+ end
19
+
20
+ def to_date_range(range)
21
+ if range.exclude_end?
22
+ to_date(range.first)...to_date(range.last)
23
+ else
24
+ to_date(range.first)..to_date(range.last)
25
+ end
26
+ end
27
+
28
+ def enumerator(start = nil)
29
+ start_date = to_date(start)
30
+ Enumerator.new { |yielder| iterate(yielder, start_date) }
31
+ end
32
+
33
+ private
34
+
35
+ def iterate(yielder, start_date)
36
+ year = start_date.year
37
+ loop do
38
+ Holiday.each(year) do |holiday|
39
+ next if holiday.date < start_date
40
+ yielder << holiday
41
+ end
42
+ year += 1
43
+ end
44
+ rescue Error
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,24 @@
1
+ require 'swedish_holidays/holiday'
2
+ require 'swedish_holidays/utils'
3
+
4
+ module SwedishHolidays
5
+ extend Enumerable
6
+
7
+ class Error < StandardError; end
8
+
9
+ def self.[](date)
10
+ return Holiday.find Utils.to_date(date) unless date.is_a? Range
11
+ range = Utils.to_date_range(date)
12
+ each(start: range.first).take_while { |h| range.include? h.date }
13
+ end
14
+
15
+ def self.holiday?(date = nil)
16
+ Holiday.holiday? Utils.to_date(date)
17
+ end
18
+
19
+ def self.each(start: nil)
20
+ enumerator = Utils.enumerator(start)
21
+ return enumerator.lazy unless block_given?
22
+ enumerator.each { |holiday| yield holiday }
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swedish_holidays
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Sammy Henningsson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '12.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ description: Check if a date is a swedish holiday
42
+ email: sammy.henningsson@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - data/2000
48
+ - data/2001
49
+ - data/2002
50
+ - data/2003
51
+ - data/2004
52
+ - data/2005
53
+ - data/2006
54
+ - data/2007
55
+ - data/2008
56
+ - data/2009
57
+ - data/2010
58
+ - data/2011
59
+ - data/2012
60
+ - data/2013
61
+ - data/2014
62
+ - data/2015
63
+ - data/2016
64
+ - data/2017
65
+ - data/2018
66
+ - data/2019
67
+ - data/2020
68
+ - data/2021
69
+ - data/2022
70
+ - data/2023
71
+ - data/2024
72
+ - data/2025
73
+ - data/2026
74
+ - data/2027
75
+ - data/2028
76
+ - data/2029
77
+ - data/2030
78
+ - data/2031
79
+ - data/2032
80
+ - data/2033
81
+ - data/2034
82
+ - data/2035
83
+ - data/2036
84
+ - data/2037
85
+ - data/2038
86
+ - data/2039
87
+ - data/2040
88
+ - data/2041
89
+ - data/2042
90
+ - data/2043
91
+ - data/2044
92
+ - data/2045
93
+ - data/2046
94
+ - data/2047
95
+ - data/2048
96
+ - data/2049
97
+ - lib/swedish_holidays.rb
98
+ - lib/swedish_holidays/holiday.rb
99
+ - lib/swedish_holidays/utils.rb
100
+ homepage: https://github.com/sammyhenningsson/swedish_holidays
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ - data
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '2.3'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.7.6
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Swedish holidays
125
+ test_files: []