Module: Malevich::Loader

Included in:
Kernel
Defined in:
lib/malevich/loader.rb

Instance Method Summary collapse

Instance Method Details

#find_plugin(name) ⇒ Object



13
14
15
# File 'lib/malevich/loader.rb', line 13

def find_plugin(name)
  @all_plugins.find { |p| p.name == name }
end

#load_plugins(plugin_path, config_file) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/malevich/loader.rb', line 11

def load_plugins(plugin_path, config_file)

  def find_plugin(name)
    @all_plugins.find { |p| p.name == name }
  end

  @all_plugins = Malevich::DSL.load(plugin_path)
  @all_names = Array.new
  @plugins_to_run = Array.new

  self.config.deep_merge! YAML.load_file(config_file) #rescue {}
  self.config.each do |name, val|
    @all_names << name
    next if val.nil?
    # dup plugins for val - array
    if val.kind_of?(Array)
      parent = find_plugin(name)
      if parent.nil?
        Kernel::log :error, "Unable to find parent plugin for #{name}"
        next
      end
      val.each_with_index do |p_settings, i|
        child = parent.dup
        child.name = "#{name}_#{i}"
        child.settings.deep_merge!(parent.settings.dup)
        child.settings.deep_merge!(p_settings)
        @all_plugins << child
        @all_names << child.name
      end
      # delete parent plugin
      @all_plugins.delete(parent)
      next
    end

    # dup plugin with parent
    if val.is_a?(Hash) && val.has_key?('parent')
      parent_name = val['parent']
      parent = find_plugin(parent_name)
      if parent.nil?
        Kernel::log :error, "Plugin #{parent_name} not found"
        next
      end
      child = parent.dup
      child.name = name
      child.settings.deep_merge!(parent.settings.dup)
      child.settings.deep_merge!(val)
      @all_plugins << child
      @all_names << child.name
      next
    end
    # over plugins merge
    @all_plugins.each { |p| p.settings.deep_merge!(val) if name == p.name }
  end
  # add plugin if it always_start or get settings and runnable
  @all_plugins.each do |p|
    unless p.always_start || @all_names.include?(p.name)
      Kernel::log(:info, "Plugin '#{p.name}' not started, because it not 'always_start' and not in config")
      next
    end
    @plugins_to_run << p if p.runnable?
  end
  @all_plugins = nil
  @all_names = nil
  # start plugins! 
  @plugins_to_run.each { |p| monitor << p }
  @plugins_to_run = nil
end

#load_respondersObject



4
5
6
7
8
9
# File 'lib/malevich/loader.rb', line 4

def load_responders
  Malevich::Responder.constants.select do |c|
    next unless Class === Malevich::Responder.const_get(c)
    monitor << Malevich::Responder.const_get(c).new
  end
end