Sha256: 947a472faeceeaa4f1a2ee1f67147e1107698d80d6f469f17a05c2c3e2232899

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

require 'yaml'
require 'eventmachine'

module Marvin
  class Settings
    
    cattr_accessor :environment, :configuration, :is_setup, :default_client,
                   :handler_folder, :default_parser, :log_level, :verbose, :daemon
                   
    self.verbose   = false
    self.log_level = :info
    self.daemon    = false
    
    class << self
      
      def daemon?
        self.daemon
      end
      
      def root
        File.expand_path(defined?(MARVIN_ROOT) ? MARVIN_ROOT : File.dirname(__FILE__) / "../..")
      end
      
      def setup(options = {})
        return if self.is_setup
        self.setup!(options)
      end
      
      def setup!(options = {})
        self.environment ||= "development"
        self.configuration = {}
        self.default_client ||= Marvin::IRC::Client
        self.default_parser ||= Marvin::Parsers::SimpleParser
        loaded_yaml = YAML.load_file(root / "config/settings.yml")
        loaded_options = loaded_yaml["default"].
                           merge(loaded_yaml[self.environment]).
                           merge(options)
        self.configuration.merge!(loaded_options)
        self.configuration.symbolize_keys!
        mod = Module.new do
          Settings.configuration.keys.each do |k|
            define_method(k) do
              return Settings.configuration[k]
            end
          
            define_method("#{k}=") do |val|
              Settings.configuration[k] = val
            end
          end
        end
        
        # Extend and include.
        
        extend  mod
        include mod
        
        self.is_setup = true
      end
      
      def [](key)
        self.setup
        return self.configuration[key.to_sym]
      end
      
      def []=(key, value)
        self.setup
        self.configuration[key.to_sym] = value
        return value
      end
      
      def to_hash
        self.configuration
      end
      
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
Sutto-marvin-0.2.0 lib/marvin/settings.rb
Sutto-marvin-0.2.1 lib/marvin/settings.rb