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) ⇒ Generator

Returns a new instance of Generator.



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

def initialize(custom_method_parser, custom_method_source_decorator, custom_methods_repository)
  @custom_method_parser = custom_method_parser
  @custom_method_source_decorator = custom_method_source_decorator
  @custom_methods_repository = custom_methods_repository
end

Instance Method Details

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/holidays/definition/context/generator.rb', line 58

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 = generate_module_src(module_name, files, regions, month_strings, custom_method_string)
  test_src = generate_test_src(module_name, files, tests)

  return module_src, test_src || ''
end

#parse_definition_files(files) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
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
# File 'lib/holidays/definition/context/generator.rb', line 20

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 = []

  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 << parse_test_definitions(definition_file['tests'])
  end

  all_regions.flatten!.uniq!

  [all_regions, all_rules_by_month, all_custom_methods, all_tests]
end