Class: Holidays::Definition::Repository::CustomMethods
- Inherits:
-
Object
- Object
- Holidays::Definition::Repository::CustomMethods
- Defined in:
- lib/holidays/definition/repository/custom_methods.rb
Instance Method Summary collapse
-
#add(new_custom_methods, new_sources = {}, function_regions = {}) ⇒ Object
When a conflict is detected the method is stored as a regional override keyed by its regions.
-
#find(method_id, region = nil) ⇒ Object
Returns the proc for the given method_id.
-
#initialize ⇒ CustomMethods
constructor
A new instance of CustomMethods.
Constructor Details
#initialize ⇒ CustomMethods
Returns a new instance of CustomMethods.
5 6 7 8 9 |
# File 'lib/holidays/definition/repository/custom_methods.rb', line 5 def initialize @custom_methods = {} @custom_method_sources = {} @regional_overrides = {} end |
Instance Method Details
#add(new_custom_methods, new_sources = {}, function_regions = {}) ⇒ Object
When a conflict is detected the method is stored as a regional override keyed by its regions. find() then resolves the right implementation at lookup time using the queried region.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/holidays/definition/repository/custom_methods.rb', line 13 def add(new_custom_methods, new_sources = {}, function_regions = {}) raise ArgumentError if new_custom_methods.nil? new_custom_methods.each do |key, method| new_source = new_sources[key] if @custom_methods.key?(key) existing_source = @custom_method_sources[key] if new_source && existing_source && new_source != existing_source regions = function_regions[key] || [] @regional_overrides[key] ||= [] @regional_overrides[key] << { regions: regions, proc: method } else @custom_methods[key] = method @custom_method_sources[key] = new_source if new_source end else @custom_methods[key] = method @custom_method_sources[key] = new_source if new_source end end end |
#find(method_id, region = nil) ⇒ Object
Returns the proc for the given method_id.
When a region is supplied, regional overrides are checked first so that conflicting methods with the same name but different logic each resolve to their own implementation.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/holidays/definition/repository/custom_methods.rb', line 42 def find(method_id, region = nil) raise ArgumentError if method_id.nil? || method_id.empty? if region && @regional_overrides[method_id] override = @regional_overrides[method_id].find { |o| o[:regions].include?(region) } return override[:proc] if override end @custom_methods[method_id] end |