summaryrefslogtreecommitdiff
path: root/lib/oxidized/manager.rb
blob: 8b34125058fc51d4e96e4a9b5157da39e8b42ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Oxidized
  require 'oxidized/model/model'
  require 'oxidized/input/input'
  require 'oxidized/output/output'
  require 'oxidized/source/source'
  class Manager
    class << self
      def load dir, file
        require File.join dir, file + '.rb'
        klass = nil
        [Oxidized, Object].each do |mod|
          klass = mod.constants.find { |const| const.to_s.downcase == file.downcase }
          klass = mod.constants.find { |const| const.to_s.downcase == 'oxidized' + file.downcase } unless klass
          klass = mod.const_get klass if klass
          break if klass
        end
        i = klass.new
        i.setup if i.respond_to? :setup
        { file => klass }
      end
    end

    attr_reader :input, :output, :model, :source, :hook
    def initialize
      @input  = {}
      @output = {}
      @model  = {}
      @source = {}
      @hook = {}
    end

    def add_input name
      @input.merge! Manager.load(Config::InputDir, name)
    end

    def add_output name
      @output.merge! Manager.load(Config::OutputDir, name)
    end

    def add_source name
      return nil if @source.has_key? name
      @source.merge! Manager.load(Config::SourceDir, name)
    end

    def add_model name
      @model.merge! local_load("model", name) ||
                    Manager.load(Config::ModelDir, name)
    end

    def add_hook name
      return nil if @hook.has_key? name
      @model.merge! local_load("hook", name) ||
                    Manager.load(Config::HookDir, 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
    end
  end
end