lib/jekyll/language-plugin/language_data.rb in jekyll-language-plugin-1.1.8 vs lib/jekyll/language-plugin/language_data.rb in jekyll-language-plugin-1.2.0
- old
+ new
@@ -1,58 +1,24 @@
module Jekyll
module LanguagePlugin
class LanguageData
- attr_reader :site, :Language
+ attr_reader :language
def initialize(site, language)
- @site = site
@language = language
+ @loaders = Jekyll::LanguagePlugin.loaders.map{ |l| l.new(site) }
end
- def get(keys)
- obj = retrieve_obj(keys)
- raise Jekyll::LanguagePlugin::PluginError.new("Invalid language data configuration. #{keys.join('.')} not found.") if language_data.nil?
- obj
- end
-
- def has?(keys)
- !retrieve_obj(keys).nil?
- end
-
- def retrieve_obj(keys)
- return nil if @language.to_s.empty?
- keys = resolve_dot_notation([language_data, keys])
-
- obj = site.send(keys.shift)
- traverse_hash(obj, keys)
- end
-
- def language_data()
- @language_data ||= site.config['language_data'].to_s || 'data.lang.%%'
- @language_data.gsub("%%", @language)
- end
-
- def traverse_hash(hash, keys)
- return nil if hash.nil?
-
- for key in keys
- if !hash.is_a?(Hash)
- return hash
- elsif !hash.key?(key)
- return nil
- end
- hash = hash[key]
+ def get(key)
+ @loaders.inject(nil) do |result, loader|
+ loader.load unless loader.loaded?
+ result = loader.get(key, @language)
+ break result unless result.nil?
end
- hash
end
- def resolve_dot_notation(keys)
- return keys.split('.') if keys.is_a?(String)
- return [] if !keys.is_a?(Enumerable)
-
- keys.flat_map do |key|
- resolve_dot_notation(key)
- end
+ def has?(key)
+ !get(key).nil?
end
end
end
end