Class: Holidays::Definition::Context::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/holidays/definition/context/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(custom_method_parser, custom_method_source_decorator, custom_methods_repository, test_parser, test_source_generator, module_source_generator) ⇒ Generator

Returns a new instance of Generator.



14
15
16
17
18
19
20
21
# File 'lib/holidays/definition/context/generator.rb', line 14

def initialize(custom_method_parser, custom_method_source_decorator, custom_methods_repository, test_parser, test_source_generator, module_source_generator)
  @custom_method_parser = custom_method_parser
  @custom_method_source_decorator = custom_method_source_decorator
  @custom_methods_repository = custom_methods_repository
  @test_parser = test_parser
  @test_source_generator = test_source_generator
  @module_source_generator = module_source_generator
end

Instance Method Details

#generate_definition_source(module_name, files, regions, rules_by_month, custom_methods, tests) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/holidays/definition/context/generator.rb', line 70

def generate_definition_source(module_name, files, regions, rules_by_month, custom_methods, tests)
  month_strings = generate_month_definition_strings(rules_by_month, custom_methods)

  # Build the custom methods string
  custom_method_string = +''
  custom_methods.each do |key, code|
    custom_method_string << @custom_method_source_decorator.call(code) + ",\n\n"
  end

  module_src = @module_source_generator.call(module_name, files, regions, month_strings, custom_method_string)
  test_src = @test_source_generator.call(module_name, files, tests)

  return module_src, test_src || ''
end

#parse_definition_files(files) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/holidays/definition/context/generator.rb', line 23

def parse_definition_files(files)
  raise ArgumentError, "Must have at least one file to parse" if files.nil? || files.empty?

  all_regions = []
  all_rules_by_month = {}
  all_custom_methods = {}
  all_tests = []
  all_region_names = {}

  files.flatten!

  files.each do |file|
    definition_file = YAML.load_file(file)

    custom_methods = @custom_method_parser.call(definition_file['methods'])

    regions, rules_by_month = parse_month_definitions(definition_file['months'], custom_methods)

    all_regions << regions.flatten

    all_rules_by_month.merge!(rules_by_month) { |month, existing, new|
      existing << new
      existing.flatten!
    }

    #FIXME This is a problem. We will have a 'global' list of methods. That's always bad. What effects will this have?
    # This is an existing problem (just so we are clear). An issue would be extremely rare because we are generally parsing
    # single files/custom files. But it IS possible that we would parse a bunch of things at the same time and step
    # on each other so we need a solution.
    all_custom_methods.merge!(custom_methods)

    all_tests += @test_parser.call(definition_file['tests'])

    region_names = definition_file['region_names']
    if region_names
      symbolized_region_names = region_names.each_with_object({}) do |(region, name), hash|
        hash[region.to_sym] = name
      end
      all_region_names.merge!(symbolized_region_names)
    end
  end

  all_regions.flatten!.uniq!

  [all_regions, all_rules_by_month, all_custom_methods, all_tests, all_region_names]
end