summaryrefslogtreecommitdiff
path: root/lib/oxidized/manager.rb
blob: ee39e406445d669b4540ece2a0631f934be18e4f (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
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 }
      rescue LoadError
        false
      end
    end

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

    def add_input name
      loader @input, Config::InputDir, "input", name
    end

    def add_output name
      loader @output, Config::OutputDir, "output", name
    end

    def add_source name
      loader @source, Config::SourceDir, "source", name
    end

    def add_model name
      loader @model, Config::ModelDir, "model", name
    end

    def add_hook name
      loader @hook, Config::HookDir, "hook", name
    end

    private

    # 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