holidays 4.6.0 → 4.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/CONTRIBUTING.md +5 -5
  4. data/Makefile +29 -0
  5. data/README.md +13 -1
  6. data/Rakefile +2 -2
  7. data/definitions/au.yaml +12 -9
  8. data/definitions/ca.yaml +17 -2
  9. data/definitions/ch.yaml +1 -1
  10. data/definitions/index.yaml +3 -0
  11. data/definitions/is.yaml +1 -1
  12. data/definitions/jp.yaml +19 -14
  13. data/definitions/kr.yaml +282 -0
  14. data/definitions/lu.yaml +56 -0
  15. data/definitions/my.yaml +51 -0
  16. data/definitions/ups.yaml +1 -1
  17. data/definitions/us.yaml +1 -1
  18. data/lib/generated_definitions/MANIFEST +3 -0
  19. data/lib/generated_definitions/REGIONS.rb +1 -1
  20. data/lib/generated_definitions/au.rb +11 -8
  21. data/lib/generated_definitions/ca.rb +1 -1
  22. data/lib/generated_definitions/ch.rb +1 -1
  23. data/lib/generated_definitions/europe.rb +2 -2
  24. data/lib/generated_definitions/is.rb +1 -1
  25. data/lib/generated_definitions/jp.rb +13 -15
  26. data/lib/generated_definitions/kr.rb +248 -0
  27. data/lib/generated_definitions/lu.rb +39 -0
  28. data/lib/generated_definitions/my.rb +36 -0
  29. data/lib/generated_definitions/north_america.rb +2 -2
  30. data/lib/generated_definitions/scandinavia.rb +1 -1
  31. data/lib/generated_definitions/ups.rb +1 -1
  32. data/lib/generated_definitions/us.rb +1 -1
  33. data/lib/holidays.rb +29 -51
  34. data/lib/holidays/core_extensions/date.rb +1 -1
  35. data/lib/holidays/definition/context/function_processor.rb +86 -0
  36. data/lib/holidays/definition/context/generator.rb +31 -6
  37. data/lib/holidays/definition/parser/custom_method.rb +1 -3
  38. data/lib/holidays/definition/repository/holidays_by_month.rb +1 -1
  39. data/lib/holidays/definition/validator/region.rb +1 -3
  40. data/lib/holidays/errors.rb +1 -0
  41. data/lib/holidays/factory/date_calculator.rb +37 -0
  42. data/lib/holidays/factory/definition.rb +96 -0
  43. data/lib/holidays/factory/finder.rb +70 -0
  44. data/lib/holidays/finder/context/between.rb +43 -0
  45. data/lib/holidays/{use_case → finder}/context/dates_driver_builder.rb +6 -4
  46. data/lib/holidays/finder/context/next_holiday.rb +57 -0
  47. data/lib/holidays/{option → finder}/context/parse_options.rb +6 -8
  48. data/lib/holidays/finder/context/search.rb +86 -0
  49. data/lib/holidays/finder/context/year_holiday.rb +57 -0
  50. data/lib/holidays/finder/rules/in_region.rb +23 -0
  51. data/lib/holidays/finder/rules/year_range.rb +82 -0
  52. data/lib/holidays/load_all_definitions.rb +7 -5
  53. data/lib/holidays/version.rb +1 -1
  54. data/test/coverage_report.rb +7 -0
  55. data/test/defs/test_defs_au.rb +1 -1
  56. data/test/defs/test_defs_ca.rb +16 -1
  57. data/test/defs/test_defs_jp.rb +4 -0
  58. data/test/defs/test_defs_kr.rb +26 -0
  59. data/test/defs/test_defs_lu.rb +24 -0
  60. data/test/defs/test_defs_my.rb +20 -0
  61. data/test/defs/test_defs_north_america.rb +16 -1
  62. data/test/holidays/core_extensions/test_date_time.rb +7 -7
  63. data/test/holidays/definition/context/test_function_processor.rb +175 -0
  64. data/test/holidays/definition/context/test_generator.rb +18 -11
  65. data/test/holidays/definition/parser/test_custom_method.rb +2 -2
  66. data/test/holidays/definition/repository/test_proc_result_cache.rb +1 -1
  67. data/test/holidays/{test_date_calculator_factory.rb → factory/test_date_calculator.rb} +3 -3
  68. data/test/holidays/factory/test_definition.rb +53 -0
  69. data/test/holidays/factory/test_finder.rb +25 -0
  70. data/test/holidays/finder/context/test_between.rb +172 -0
  71. data/test/holidays/{use_case → finder}/context/test_dates_driver_builder.rb +2 -2
  72. data/test/holidays/finder/context/test_next_holiday.rb +156 -0
  73. data/test/holidays/{option → finder}/context/test_parse_options.rb +9 -9
  74. data/test/holidays/finder/context/test_search.rb +203 -0
  75. data/test/holidays/finder/context/test_year_holiday.rb +202 -0
  76. data/test/holidays/finder/rules/test_in_region.rb +38 -0
  77. data/test/holidays/finder/rules/test_year_range.rb +170 -0
  78. data/test/integration/README.md +9 -0
  79. data/test/{test_all_regions.rb → integration/test_all_regions.rb} +16 -2
  80. data/test/{test_custom_holidays.rb → integration/test_custom_holidays.rb} +2 -2
  81. data/test/{test_custom_year_range_holidays.rb → integration/test_custom_year_range_holidays.rb} +1 -1
  82. data/test/{test_holidays.rb → integration/test_holidays.rb} +43 -32
  83. data/test/{test_holidays_between.rb → integration/test_holidays_between.rb} +8 -16
  84. data/test/{test_multiple_regions.rb → integration/test_multiple_regions.rb} +1 -1
  85. data/test/test_helper.rb +3 -4
  86. metadata +67 -39
  87. data/benchmark.rb +0 -8
  88. data/lib/holidays/date_calculator_factory.rb +0 -35
  89. data/lib/holidays/definition_factory.rb +0 -86
  90. data/lib/holidays/option_factory.rb +0 -15
  91. data/lib/holidays/use_case/context/between.rb +0 -45
  92. data/lib/holidays/use_case/context/context_common.rb +0 -123
  93. data/lib/holidays/use_case/context/next_holiday.rb +0 -54
  94. data/lib/holidays/use_case/context/year_holiday.rb +0 -51
  95. data/lib/holidays/use_case_factory.rb +0 -41
  96. data/test/holidays/test_definition_factory.rb +0 -49
  97. data/test/holidays/test_option_factory.rb +0 -9
  98. data/test/holidays/test_use_case_factory.rb +0 -13
  99. data/test/holidays/use_case/context/test_between.rb +0 -77
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
2
2
  require "#{Holidays::DEFINITIONS_PATH}/gb"
3
3
  require "#{Holidays::DEFINITIONS_PATH}/ie"
4
4
 
@@ -1,5 +1,4 @@
1
- require 'coveralls'
2
- Coveralls.wear!
1
+ require 'coverage_report'
3
2
 
4
3
  $:.unshift(File.expand_path(File.dirname(__FILE__) + '../../lib/'))
5
4
 
@@ -20,7 +19,7 @@ end
20
19
 
21
20
  class Time
22
21
  include Holidays::CoreExtensions::Time
23
- end
22
+ end
24
23
 
25
24
  module Holidays
26
25
  # Test region used for generating a holiday on Date.today
@@ -35,4 +34,4 @@ module Holidays
35
34
  end
36
35
  end
37
36
 
38
- Holidays::DefinitionFactory.merger.call(Holidays::Test::DEFINED_REGIONS, Holidays::Test::HOLIDAYS_BY_MONTH, Holidays::Test::CUSTOM_METHODS)
37
+ Holidays::Factory::Definition.merger.call(Holidays::Test::DEFINED_REGIONS, Holidays::Test::HOLIDAYS_BY_MONTH, Holidays::Test::CUSTOM_METHODS)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: holidays
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.6.0
4
+ version: 4.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dunae
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-21 00:00:00.000000000 Z
12
+ date: 2016-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -108,10 +108,10 @@ files:
108
108
  - CONTRIBUTING.md
109
109
  - Gemfile
110
110
  - LICENSE
111
+ - Makefile
111
112
  - README.md
112
113
  - REFERENCES
113
114
  - Rakefile
114
- - benchmark.rb
115
115
  - bin/console
116
116
  - bin/setup
117
117
  - definitions/README.md
@@ -144,10 +144,13 @@ files:
144
144
  - definitions/is.yaml
145
145
  - definitions/it.yaml
146
146
  - definitions/jp.yaml
147
+ - definitions/kr.yaml
147
148
  - definitions/li.yaml
148
149
  - definitions/lt.yaml
150
+ - definitions/lu.yaml
149
151
  - definitions/ma.yaml
150
152
  - definitions/mx.yaml
153
+ - definitions/my.yaml
151
154
  - definitions/nerc.yaml
152
155
  - definitions/nl.yaml
153
156
  - definitions/no.yaml
@@ -202,10 +205,13 @@ files:
202
205
  - lib/generated_definitions/is.rb
203
206
  - lib/generated_definitions/it.rb
204
207
  - lib/generated_definitions/jp.rb
208
+ - lib/generated_definitions/kr.rb
205
209
  - lib/generated_definitions/li.rb
206
210
  - lib/generated_definitions/lt.rb
211
+ - lib/generated_definitions/lu.rb
207
212
  - lib/generated_definitions/ma.rb
208
213
  - lib/generated_definitions/mx.rb
214
+ - lib/generated_definitions/my.rb
209
215
  - lib/generated_definitions/nerc.rb
210
216
  - lib/generated_definitions/nl.rb
211
217
  - lib/generated_definitions/no.rb
@@ -234,7 +240,7 @@ files:
234
240
  - lib/holidays/date_calculator/day_of_month.rb
235
241
  - lib/holidays/date_calculator/easter.rb
236
242
  - lib/holidays/date_calculator/weekend_modifier.rb
237
- - lib/holidays/date_calculator_factory.rb
243
+ - lib/holidays/definition/context/function_processor.rb
238
244
  - lib/holidays/definition/context/generator.rb
239
245
  - lib/holidays/definition/context/merger.rb
240
246
  - lib/holidays/definition/decorator/custom_method_proc.rb
@@ -248,18 +254,21 @@ files:
248
254
  - lib/holidays/definition/repository/regions.rb
249
255
  - lib/holidays/definition/validator/custom_method.rb
250
256
  - lib/holidays/definition/validator/region.rb
251
- - lib/holidays/definition_factory.rb
252
257
  - lib/holidays/errors.rb
258
+ - lib/holidays/factory/date_calculator.rb
259
+ - lib/holidays/factory/definition.rb
260
+ - lib/holidays/factory/finder.rb
261
+ - lib/holidays/finder/context/between.rb
262
+ - lib/holidays/finder/context/dates_driver_builder.rb
263
+ - lib/holidays/finder/context/next_holiday.rb
264
+ - lib/holidays/finder/context/parse_options.rb
265
+ - lib/holidays/finder/context/search.rb
266
+ - lib/holidays/finder/context/year_holiday.rb
267
+ - lib/holidays/finder/rules/in_region.rb
268
+ - lib/holidays/finder/rules/year_range.rb
253
269
  - lib/holidays/load_all_definitions.rb
254
- - lib/holidays/option/context/parse_options.rb
255
- - lib/holidays/option_factory.rb
256
- - lib/holidays/use_case/context/between.rb
257
- - lib/holidays/use_case/context/context_common.rb
258
- - lib/holidays/use_case/context/dates_driver_builder.rb
259
- - lib/holidays/use_case/context/next_holiday.rb
260
- - lib/holidays/use_case/context/year_holiday.rb
261
- - lib/holidays/use_case_factory.rb
262
270
  - lib/holidays/version.rb
271
+ - test/coverage_report.rb
263
272
  - test/data/test_custom_govt_holiday_defs.yaml
264
273
  - test/data/test_custom_year_range_holiday_defs.yaml
265
274
  - test/data/test_multiple_custom_holiday_defs.yaml
@@ -295,10 +304,13 @@ files:
295
304
  - test/defs/test_defs_is.rb
296
305
  - test/defs/test_defs_it.rb
297
306
  - test/defs/test_defs_jp.rb
307
+ - test/defs/test_defs_kr.rb
298
308
  - test/defs/test_defs_li.rb
299
309
  - test/defs/test_defs_lt.rb
310
+ - test/defs/test_defs_lu.rb
300
311
  - test/defs/test_defs_ma.rb
301
312
  - test/defs/test_defs_mx.rb
313
+ - test/defs/test_defs_my.rb
302
314
  - test/defs/test_defs_nerc.rb
303
315
  - test/defs/test_defs_nl.rb
304
316
  - test/defs/test_defs_no.rb
@@ -327,6 +339,7 @@ files:
327
339
  - test/holidays/date_calculator/test_easter_gregorian.rb
328
340
  - test/holidays/date_calculator/test_easter_julian.rb
329
341
  - test/holidays/date_calculator/test_weekend_modifier.rb
342
+ - test/holidays/definition/context/test_function_processor.rb
330
343
  - test/holidays/definition/context/test_generator.rb
331
344
  - test/holidays/definition/context/test_merger.rb
332
345
  - test/holidays/definition/decorator/test_custom_method_proc.rb
@@ -339,20 +352,25 @@ files:
339
352
  - test/holidays/definition/repository/test_regions.rb
340
353
  - test/holidays/definition/validator/test_custom_method.rb
341
354
  - test/holidays/definition/validator/test_region.rb
342
- - test/holidays/option/context/test_parse_options.rb
343
- - test/holidays/test_date_calculator_factory.rb
344
- - test/holidays/test_definition_factory.rb
345
- - test/holidays/test_option_factory.rb
346
- - test/holidays/test_use_case_factory.rb
347
- - test/holidays/use_case/context/test_between.rb
348
- - test/holidays/use_case/context/test_dates_driver_builder.rb
349
- - test/test_all_regions.rb
350
- - test/test_custom_holidays.rb
351
- - test/test_custom_year_range_holidays.rb
355
+ - test/holidays/factory/test_date_calculator.rb
356
+ - test/holidays/factory/test_definition.rb
357
+ - test/holidays/factory/test_finder.rb
358
+ - test/holidays/finder/context/test_between.rb
359
+ - test/holidays/finder/context/test_dates_driver_builder.rb
360
+ - test/holidays/finder/context/test_next_holiday.rb
361
+ - test/holidays/finder/context/test_parse_options.rb
362
+ - test/holidays/finder/context/test_search.rb
363
+ - test/holidays/finder/context/test_year_holiday.rb
364
+ - test/holidays/finder/rules/test_in_region.rb
365
+ - test/holidays/finder/rules/test_year_range.rb
366
+ - test/integration/README.md
367
+ - test/integration/test_all_regions.rb
368
+ - test/integration/test_custom_holidays.rb
369
+ - test/integration/test_custom_year_range_holidays.rb
370
+ - test/integration/test_holidays.rb
371
+ - test/integration/test_holidays_between.rb
372
+ - test/integration/test_multiple_regions.rb
352
373
  - test/test_helper.rb
353
- - test/test_holidays.rb
354
- - test/test_holidays_between.rb
355
- - test/test_multiple_regions.rb
356
374
  homepage: https://github.com/holidays/holidays
357
375
  licenses:
358
376
  - MIT
@@ -378,6 +396,7 @@ signing_key:
378
396
  specification_version: 4
379
397
  summary: A collection of Ruby methods to deal with statutory and other holidays.
380
398
  test_files:
399
+ - test/coverage_report.rb
381
400
  - test/data/test_custom_govt_holiday_defs.yaml
382
401
  - test/data/test_custom_year_range_holiday_defs.yaml
383
402
  - test/data/test_multiple_custom_holiday_defs.yaml
@@ -413,10 +432,13 @@ test_files:
413
432
  - test/defs/test_defs_is.rb
414
433
  - test/defs/test_defs_it.rb
415
434
  - test/defs/test_defs_jp.rb
435
+ - test/defs/test_defs_kr.rb
416
436
  - test/defs/test_defs_li.rb
417
437
  - test/defs/test_defs_lt.rb
438
+ - test/defs/test_defs_lu.rb
418
439
  - test/defs/test_defs_ma.rb
419
440
  - test/defs/test_defs_mx.rb
441
+ - test/defs/test_defs_my.rb
420
442
  - test/defs/test_defs_nerc.rb
421
443
  - test/defs/test_defs_nl.rb
422
444
  - test/defs/test_defs_no.rb
@@ -445,6 +467,7 @@ test_files:
445
467
  - test/holidays/date_calculator/test_easter_gregorian.rb
446
468
  - test/holidays/date_calculator/test_easter_julian.rb
447
469
  - test/holidays/date_calculator/test_weekend_modifier.rb
470
+ - test/holidays/definition/context/test_function_processor.rb
448
471
  - test/holidays/definition/context/test_generator.rb
449
472
  - test/holidays/definition/context/test_merger.rb
450
473
  - test/holidays/definition/decorator/test_custom_method_proc.rb
@@ -457,17 +480,22 @@ test_files:
457
480
  - test/holidays/definition/repository/test_regions.rb
458
481
  - test/holidays/definition/validator/test_custom_method.rb
459
482
  - test/holidays/definition/validator/test_region.rb
460
- - test/holidays/option/context/test_parse_options.rb
461
- - test/holidays/test_date_calculator_factory.rb
462
- - test/holidays/test_definition_factory.rb
463
- - test/holidays/test_option_factory.rb
464
- - test/holidays/test_use_case_factory.rb
465
- - test/holidays/use_case/context/test_between.rb
466
- - test/holidays/use_case/context/test_dates_driver_builder.rb
467
- - test/test_all_regions.rb
468
- - test/test_custom_holidays.rb
469
- - test/test_custom_year_range_holidays.rb
483
+ - test/holidays/factory/test_date_calculator.rb
484
+ - test/holidays/factory/test_definition.rb
485
+ - test/holidays/factory/test_finder.rb
486
+ - test/holidays/finder/context/test_between.rb
487
+ - test/holidays/finder/context/test_dates_driver_builder.rb
488
+ - test/holidays/finder/context/test_next_holiday.rb
489
+ - test/holidays/finder/context/test_parse_options.rb
490
+ - test/holidays/finder/context/test_search.rb
491
+ - test/holidays/finder/context/test_year_holiday.rb
492
+ - test/holidays/finder/rules/test_in_region.rb
493
+ - test/holidays/finder/rules/test_year_range.rb
494
+ - test/integration/README.md
495
+ - test/integration/test_all_regions.rb
496
+ - test/integration/test_custom_holidays.rb
497
+ - test/integration/test_custom_year_range_holidays.rb
498
+ - test/integration/test_holidays.rb
499
+ - test/integration/test_holidays_between.rb
500
+ - test/integration/test_multiple_regions.rb
470
501
  - test/test_helper.rb
471
- - test/test_holidays.rb
472
- - test/test_holidays_between.rb
473
- - test/test_multiple_regions.rb
@@ -1,8 +0,0 @@
1
- require 'benchmark'
2
-
3
- puts Benchmark.measure {
4
- require 'holidays'
5
- Holidays.between(
6
- Date.civil(2016, 1, 1), Date.civil(2016, 12, 31), :us
7
- )
8
- }
@@ -1,35 +0,0 @@
1
- require 'holidays/date_calculator/easter'
2
- require 'holidays/date_calculator/weekend_modifier'
3
- require 'holidays/date_calculator/day_of_month'
4
-
5
- module Holidays
6
- module DateCalculatorFactory
7
- module Easter
8
- module Gregorian
9
- class << self
10
- def easter_calculator
11
- DateCalculator::Easter::Gregorian.new
12
- end
13
- end
14
- end
15
-
16
- module Julian
17
- class << self
18
- def easter_calculator
19
- DateCalculator::Easter::Julian.new
20
- end
21
- end
22
- end
23
- end
24
-
25
- class << self
26
- def weekend_modifier
27
- DateCalculator::WeekendModifier.new
28
- end
29
-
30
- def day_of_month_calculator
31
- DateCalculator::DayOfMonth.new
32
- end
33
- end
34
- end
35
- end
@@ -1,86 +0,0 @@
1
- require 'holidays/definition/context/generator'
2
- require 'holidays/definition/context/merger'
3
- require 'holidays/definition/decorator/custom_method_proc'
4
- require 'holidays/definition/decorator/custom_method_source'
5
- require 'holidays/definition/parser/custom_method'
6
- require 'holidays/definition/repository/holidays_by_month'
7
- require 'holidays/definition/repository/regions'
8
- require 'holidays/definition/repository/cache'
9
- require 'holidays/definition/repository/proc_result_cache'
10
- require 'holidays/definition/repository/custom_methods'
11
- require 'holidays/definition/validator/custom_method'
12
- require 'holidays/definition/validator/region'
13
-
14
- module Holidays
15
- module DefinitionFactory
16
- class << self
17
- def file_parser
18
- Definition::Context::Generator.new(
19
- custom_method_parser,
20
- custom_method_source_decorator,
21
- custom_methods_repository,
22
- )
23
- end
24
-
25
- def source_generator
26
- Definition::Context::Generator.new(
27
- custom_method_parser,
28
- custom_method_source_decorator,
29
- custom_methods_repository,
30
- )
31
- end
32
-
33
- def merger
34
- Definition::Context::Merger.new(
35
- holidays_by_month_repository,
36
- regions_repository,
37
- custom_methods_repository,
38
- )
39
- end
40
-
41
- def custom_method_parser
42
- Definition::Parser::CustomMethod.new(
43
- custom_method_validator,
44
- )
45
- end
46
-
47
- def custom_method_proc_decorator
48
- Definition::Decorator::CustomMethodProc.new
49
- end
50
-
51
- def custom_method_source_decorator
52
- Definition::Decorator::CustomMethodSource.new
53
- end
54
-
55
- def region_validator
56
- Definition::Validator::Region.new(
57
- regions_repository
58
- )
59
- end
60
-
61
- def custom_method_validator
62
- Definition::Validator::CustomMethod.new
63
- end
64
-
65
- def holidays_by_month_repository
66
- @holidays_repo ||= Definition::Repository::HolidaysByMonth.new
67
- end
68
-
69
- def regions_repository
70
- @regions_repo ||= Definition::Repository::Regions.new
71
- end
72
-
73
- def cache_repository
74
- @cache_repo ||= Definition::Repository::Cache.new
75
- end
76
-
77
- def proc_result_cache_repository
78
- @proc_result_cache_repo ||= Definition::Repository::ProcResultCache.new
79
- end
80
-
81
- def custom_methods_repository
82
- @custom_methods_repository ||= Definition::Repository::CustomMethods.new
83
- end
84
- end
85
- end
86
- end
@@ -1,15 +0,0 @@
1
- require 'holidays/option/context/parse_options'
2
-
3
- module Holidays
4
- module OptionFactory
5
- class << self
6
- def parse_options
7
- Option::Context::ParseOptions.new(
8
- DefinitionFactory.regions_repository,
9
- DefinitionFactory.region_validator,
10
- DefinitionFactory.merger,
11
- )
12
- end
13
- end
14
- end
15
- end
@@ -1,45 +0,0 @@
1
- module Holidays
2
- module UseCase
3
- module Context
4
- class Between
5
- include ContextCommon
6
-
7
- def initialize(holidays_by_month_repo, day_of_month_calculator, custom_methods_repo, proc_result_cache_repo)
8
- @holidays_by_month_repo = holidays_by_month_repo
9
- @day_of_month_calculator = day_of_month_calculator
10
- @custom_methods_repo = custom_methods_repo
11
- @proc_result_cache_repo = proc_result_cache_repo
12
- end
13
-
14
- def call(start_date, end_date, dates_driver, regions, observed, informal)
15
- validate!(start_date, end_date, dates_driver, regions)
16
-
17
- holidays = []
18
- holidays = make_date_array(dates_driver, regions, observed, informal)
19
- holidays = holidays.select{|holiday|holiday[:date].between?(start_date, end_date)}
20
- holidays.sort{|a, b| a[:date] <=> b[:date] }
21
- end
22
-
23
- private
24
-
25
- attr_reader :holidays_by_month_repo,
26
- :day_of_month_calculator,
27
- :custom_methods_repo,
28
- :proc_result_cache_repo
29
-
30
- def validate!(start_date, end_date, dates_driver, regions)
31
- raise ArgumentError unless start_date
32
- raise ArgumentError unless end_date
33
-
34
- raise ArgumentError if dates_driver.nil? || dates_driver.empty?
35
-
36
- dates_driver.each do |year, months|
37
- raise ArgumentError if months.nil? || months.empty?
38
- end
39
-
40
- raise ArgumentError if regions.nil? || regions.empty?
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,123 +0,0 @@
1
- module Holidays
2
- module UseCase
3
- module Context
4
- module ContextCommon
5
- def make_date_array(dates_driver, regions, observed, informal)
6
- holidays = []
7
- dates_driver.each do |year, months|
8
- months.each do |month|
9
- next unless hbm = holidays_by_month_repo.find_by_month(month)
10
- hbm.each do |h|
11
- next unless in_region?(regions, h[:regions])
12
- next if h[:type] == :informal && !informal
13
-
14
- # range check feature.
15
- if h[:year_ranges]
16
- valid_range_year = false
17
- h[:year_ranges].each do |year_range|
18
- next unless year_range.is_a?(Hash) && year_range.length == 1
19
- next unless year_range.select{
20
- |operator,year|[:before,"before",:after,"after",:limited,"limited",:between,"between"].include?(operator)}.count > 0
21
- case year_range.keys.first
22
- when :before,"before"
23
- valid_range_year = true if year <= year_range[year_range.keys.first]
24
- when :after,"after"
25
- valid_range_year = true if year >= year_range[year_range.keys.first]
26
- when :limited,"limited"
27
- valid_range_year = true if year_range[year_range.keys.first].include?(year)
28
- when :between,"between"
29
- year_range[year_range.keys.first] = Range.new(*year_range[year_range.keys.first].split("..").map(&:to_i)) if year_range[year_range.keys.first].is_a?(String)
30
- valid_range_year = true if year_range[year_range.keys.first].cover?(year)
31
- end
32
- break if valid_range_year
33
- end
34
- next unless valid_range_year
35
- end
36
-
37
- #FIXME I don't like this entire if/else. If it's a function, do something, else do some
38
- # weird mday logic? Bollocks. I think this should be a refactor target.
39
- if h[:function]
40
- function_arguments = []
41
-
42
- #FIXME This is a refactor target. We should also allow 'date'. Right now these are the only
43
- # three things that we allow in. I think having a more testable, robust approach here is vital.
44
- if h[:function_arguments].include?(:year)
45
- function_arguments << year
46
- end
47
-
48
- if h[:function_arguments].include?(:month)
49
- function_arguments << month
50
- end
51
-
52
- if h[:function_arguments].include?(:day)
53
- function_arguments << h[:mday]
54
- end
55
-
56
- result = call_proc(h[:function], *function_arguments)
57
-
58
- #FIXME This is a dangerous assumption. We should raise an error or something
59
- # if these procs return something unexpected.
60
- #
61
- # Procs may return either Date or an integer representing mday
62
- if result.kind_of?(Date)
63
- if h[:function_modifier]
64
- result = result + h[:function_modifier] # NOTE: This could be a positive OR negative number.
65
- end
66
-
67
- month = result.month
68
- mday = result.mday
69
- else
70
- mday = result
71
- end
72
- else
73
- mday = h[:mday] || day_of_month_calculator.call(year, month, h[:week], h[:wday])
74
- end
75
-
76
- # Silently skip bad mdays
77
- begin
78
- date = Date.civil(year, month, mday)
79
- rescue; next; end
80
-
81
- #FIXME We should be checking the function arguments and passing in what is specified.
82
- # Right now all 'observed' functions require 'date' but that is by convention. Nothing
83
- # is requiring that. We should be more explicit.
84
- if observed && h[:observed]
85
- date = call_proc(h[:observed], date)
86
- end
87
-
88
- holidays << {:date => date, :name => h[:name], :regions => h[:regions]}
89
- end
90
- end
91
- end
92
- holidays
93
- end
94
-
95
- def call_proc(function_id, *arguments)
96
- function = custom_methods_repo.find(function_id)
97
- raise Holidays::FunctionNotFound.new("Unable to find function with id '#{function_id}'") if function.nil?
98
-
99
- proc_result_cache_repo.lookup(function, *arguments)
100
- end
101
-
102
- # Check sub regions.
103
- #
104
- # When request :any, all holidays should be returned.
105
- # When requesting :ca_bc, holidays in :ca or :ca_bc should be returned.
106
- # When requesting :ca, holidays in :ca but not its subregions should be returned.
107
- def in_region?(requested, available) # :nodoc:
108
- return true if requested.include?(:any)
109
-
110
- # When an underscore is encountered, derive the parent regions
111
- # symbol and include both in the requested array.
112
- requested = requested.collect do |r|
113
- r.to_s =~ /_/ ? [r, r.to_s.gsub(/_[\w]*$/, '').to_sym] : r
114
- end
115
-
116
- requested = requested.flatten.uniq
117
-
118
- available.any? { |avail| requested.include?(avail) }
119
- end
120
- end
121
- end
122
- end
123
- end