Refactor logic to always choose first matched language.
This commit is contained in:
parent
546a91f78e
commit
bc2d676b6f
@ -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
|
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
|
end
|
||||||
|
|
||||||
def url(*args, &block)
|
def url(*args, &block)
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user