From 6434e1a36caf025530363a16f52d77a5780ecc8a Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Wed, 11 Jun 2014 15:34:14 +0300 Subject: Change dynamic discover of file => class name We needed this, because use of 'def inherited' not idempotent, in Oxidized::Script use case we may need to restart Oxidized, even though it's already loaded, and we won't get class names populated via 'def inherited' anymore. There are quite many optiosn to do this a) filename == class name + no discovery/mapping, very clean - user 'source' must give us model name in exactly right capitalization b) add 'self' at end of files, so eval will return class name + deterministic with arbitrary name - DSL cruft, DSL being light is our main value (can we do this via parent class? I couldn't find way) c) load in new module via Module.module_eval X + module will contain only consts we just created - but which one is the one we want? - if we use eval, load errors won't tell line error But at least now we got rid of 'inherited' methods and not adding too much cruft, hope it's better than before. --- lib/oxidized/manager.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'lib/oxidized/manager.rb') diff --git a/lib/oxidized/manager.rb b/lib/oxidized/manager.rb index 9352eaf..f901a5f 100644 --- a/lib/oxidized/manager.rb +++ b/lib/oxidized/manager.rb @@ -8,17 +8,21 @@ module Oxidized def load dir, file begin require File.join dir, file+'.rb' - obj, Oxidized.mgr.loader = Oxidized.mgr.loader, nil - k = obj[:class].new - k.setup if k.respond_to? :setup - { file => obj[:class] } + klass = nil + [Oxidized, Object].each do |mod| + klass = mod.constants.find { |const| const.to_s.downcase.match file.downcase } + klass = mod.const_get klass if klass + break if klass + end + i = klass.new + i.setup if i.respond_to? :setup + { file => klass } rescue LoadError {} end end end attr_reader :input, :output, :model, :source - attr_accessor :loader def initialize @input = {} @output = {} -- cgit v1.2.1