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 ...@@ -101,27 +101,37 @@ module Hbc
def language(*args, &block) def language(*args, &block)
if !args.empty? && block_given? if !args.empty? && block_given?
args.each do |arg| @language_blocks ||= {}
MacOS.languages.each_with_index do |l, index| @language_blocks[args] = block
next unless arg == :default || l.match(arg)
next unless @language.nil? || @language[:level].nil? || @language[:level] > index
@language = {
block: block,
level: index,
}
end
end
else else
language_eval language_eval
@language @language
end end
end end
def language_eval def language_eval
if @language.is_a?(Hash) && @language.key?(:block) return if instance_variable_defined?(:@language)
@language = @language[:block].call
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 end
# fallback to :default
@language = default_key.nil? ? nil : @language_blocks[default_key].call
end end
def url(*args, &block) def url(*args, &block)
......
...@@ -148,6 +148,12 @@ describe Hbc::DSL do ...@@ -148,6 +148,12 @@ describe Hbc::DSL do
MacOS.stubs(languages: ["THIRD_LANGUAGE"]) MacOS.stubs(languages: ["THIRD_LANGUAGE"])
cask.call.language.must_equal :default 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
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