Module: Cinchize

Defined in:
lib/cinchize.rb

Defined Under Namespace

Classes: Cinchize

Constant Summary collapse

VERSION =
File.read(File.dirname(__FILE__) + "/../VERSION").chomp

Class Method Summary collapse

Class Method Details

.config(options, network) ⇒ Object



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
# File 'lib/cinchize.rb', line 16

def self.config options, network
  config_file = options[:system] ? options[:system_config]: options[:local_config]
  
  raise ArgumentError.new "there's no config file located at: #{config_file}" unless File.exists? config_file
  raise ArgumentError.new "needs a network" if network.nil? or network.empty?

  cfg = YAML.load_file config_file
  
  raise ArgumentError.new "there's no server config in the config file" unless cfg.has_key? "servers"
  raise ArgumentError.new "there's no networks configured, please recheck #{config_file}" unless cfg["servers"]
  raise ArgumentError.new "the config file doesn't contain a config for #{network}" unless cfg["servers"].has_key? network
  
  ntw = cfg["servers"][network]
  
  plugins = []
  plugin_options = {}

  ntw["plugins"] ||= []
  ntw.delete("plugins").each do |plugin|
    begin
      raise NameError.new "the class can't be null" if plugin["class"].nil?
      
      require plugin["class"].downcase.gsub('::', '/')
    
      clazz = nil
      plugin["class"].split("::").inject(Object) { |m,n| clazz = m.const_get(n) }
      plugins << clazz 
    
      plugin_options[clazz] = plugin["options"] || {}
    rescue LoadError => e
      puts "error while loading the module: #{e}"
    rescue NameError => e
      puts "error while loading the class: #{e}"
    end
  end

  cfg["options"] ||= {}
  dir_mode = cfg["options"].key?("dir_mode") ? cfg["options"]["dir_mode"] : "normal"
      
  daemon_options = {
    :dir_mode => dir_mode.to_sym,
    :dir => cfg["options"]["dir"] || Dir.getwd,
    :log_output => cfg["options"]["log_output"] || false,
    :app_name => "cinchize_#{network}",
    :ontop => options[:ontop],
  }

  [daemon_options, ntw, plugins, plugin_options]
end