Module: ClassX::Pluggable::ClassMethods

Includes:
Util
Defined in:
lib/classx/pluggable.rb

Instance Method Summary collapse

Methods included from Util

module2path, nested_autoload, nested_const_get

Instance Method Details

#component_class_get(type, name, options = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/classx/pluggable.rb', line 178

def component_class_get type, name, options={}
  case name
  when ::Class
    return name
  else
    mod_name = nil
    target_name = nil
    if name =~ /\A\+([\w:]+)\z/
      target_name = $1
      mod_name = [ self, type.capitalize, target_name ].join("::")
    else
      mod_name = name
    end
    begin
      return nested_const_get(mod_name)
    rescue NameError => e
      begin
        if options[:plugin_dir]
          options[:plugin_dir].each do |path|
            begin
              begin
                self.const_get(type.capitalize).autoload(name, File.expand_path(File.join(path, module2path(target_name))))
              rescue LoadError => e
                raise ::ClassX::Pluggable::PluginLoadError, "class: #{mod_name} is not found"
              end
              return nested_const_get(mod_name)
            rescue NameError => e
              next
            end
            raise NameError, "must not happened unless your code is something wrong!!"
          end
        else
          nested_autoload(mod_name, module2path(mod_name))
          nested_const_get mod_name
        end
      rescue LoadError => e
        raise ::ClassX::Pluggable::PluginLoadError, "class: #{mod_name} is not found."
      end
    end
  end
end