Skip to content
Snippets Groups Projects
Commit bc2d676b authored by Markus Reiter's avatar Markus Reiter
Browse files

Refactor logic to always choose first matched language.

parent 546a91f7
No related branches found
No related tags found
No related merge requests found
......@@ -101,27 +101,37 @@ module Hbc
def language(*args, &block)
if !args.empty? && block_given?
args.each do |arg|
MacOS.languages.each_with_index do |l, index|
next unless arg == :default || l.match(arg)
next unless @language.nil? || @language[:level].nil? || @language[:level] > index
@language = {
block: block,
level: index,
}
end
end
@language_blocks ||= {}
@language_blocks[args] = block
else
language_eval
@language
end
end
def language_eval
if @language.is_a?(Hash) && @language.key?(:block)
@language = @language[:block].call
return if instance_variable_defined?(:@language)
return unless instance_variable_defined?(:@language_blocks)
default_key = @language_blocks.keys.detect { |key| key.include?(:default) }
MacOS.languages.each do |language|
@language_blocks.each do |regexes_or_strings, block|
if regexes_or_strings.include?(:default)
regexes_or_strings = regexes_or_strings - [:default] + [%r{^en}]
end
case language
when *regexes_or_strings
@language = block.call
return
end
end
end
# fallback to :default
@language = default_key.nil? ? nil : @language_blocks[default_key].call
end
def url(*args, &block)
......
......@@ -148,6 +148,12 @@ describe Hbc::DSL do
MacOS.stubs(languages: ["THIRD_LANGUAGE"])
cask.call.language.must_equal :default
MacOS.stubs(languages: ["THIRD_LANGUAGE", "SECOND_LANGUAGE", "FIRST_LANGUAGE"])
cask.call.language.must_equal :second
MacOS.stubs(languages: ["THIRD_LANGUAGE", "FIRST_LANGUAGE", "SECOND_LANGUAGE"])
cask.call.language.must_equal :first
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment