From a843b5cff1ffa7bba11281402694b992b12a448b Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Thu, 7 Jun 2018 22:08:15 +0300 Subject: Refactor manager, take2 Make all the add_* return falsy valuem and make all the callers raise, this has benefit that the caller has more information, like it can tell what node we were trying to load when model failed to load. We were only failing to do this in two places in the code, source and plugin. --- lib/oxidized/manager.rb | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'lib/oxidized/manager.rb') diff --git a/lib/oxidized/manager.rb b/lib/oxidized/manager.rb index 8b34125..ee39e40 100644 --- a/lib/oxidized/manager.rb +++ b/lib/oxidized/manager.rb @@ -17,49 +17,48 @@ module Oxidized i = klass.new i.setup if i.respond_to? :setup { file => klass } + rescue LoadError + false end end - attr_reader :input, :output, :model, :source, :hook + attr_reader :input, :output, :source, :model, :hook def initialize @input = {} @output = {} - @model = {} @source = {} - @hook = {} + @model = {} + @hook = {} end def add_input name - @input.merge! Manager.load(Config::InputDir, name) + loader @input, Config::InputDir, "input", name end def add_output name - @output.merge! Manager.load(Config::OutputDir, name) + loader @output, Config::OutputDir, "output", name end def add_source name - return nil if @source.has_key? name - @source.merge! Manager.load(Config::SourceDir, name) + loader @source, Config::SourceDir, "source", name end def add_model name - @model.merge! local_load("model", name) || - Manager.load(Config::ModelDir, name) + loader @model, Config::ModelDir, "model", name end def add_hook name - return nil if @hook.has_key? name - @model.merge! local_load("hook", name) || - Manager.load(Config::HookDir, name) + loader @hook, Config::HookDir, "hook", name end private - # try to load locally defined file, instead of upstream provided - def local_load dir, name - dir = File.join(Config::Root, dir) - return false unless File.exist? File.join(dir, name + ".rb") - Manager.load dir, name + # if local version of file exists, load it, else load global - return falsy value if nothing loaded + def loader hash, global_dir, local_dir, name + dir = File.join(Config::Root, local_dir) + map = Manager.load(dir, name) if File.exist? File.join(dir, name + ".rb") + map = Manager.load(global_dir, name) unless map + hash.merge!(map) if map end end end -- cgit v1.2.1