Sha256: cf0ff481a3ae362eb4bf42a2872ff4e166f53e53f370e6d82329a61296fa2e63

Contents?: true

Size: 1.8 KB

Versions: 19

Compression:

Stored size: 1.8 KB

Contents

require 'erb'
require 'singleton'

# A wrapper for slingshot  elastic-search adapter for Mongoid
module Mebla
  # Parses the configuration file and holds important configuration attributes  
  class Configuration
    include Singleton
    
    attr_reader :log_dir
    attr_accessor :index, :host, :port, :logger
    
    # @private
    # Initializes a new configuration object
    def initialize
      @log_dir = "#{Dir.pwd}/tmp/log"
      parse_config      
      
      # Setup defaults
      @index ||= "mebla"
      @host ||= "localhost"
      @port ||= 9200
      
      make_tmp_dir
      @logger = ActiveSupport::BufferedLogger.new(
        open("#{@log_dir}/mebla.log", "a")
      )
      @logger.level = ActiveSupport::BufferedLogger::Severity::DEBUG
      
      setup_logger        
      
      # Setup slingshot
      Slingshot::Configuration.url(self.url)
    end
    
    # Sets up the default settings of the logger
    # @return [nil]
    def setup_logger
      @logger.auto_flushing = true      
    end
    
    # Returns the proper url for elasticsearch
    # @return [String] url representation of the configuration options host and port
    def url
      "http://#{@host}:#{@port}"
    end
    
    private    
    # Creates tmp directory if it doesn't exist
    # @return [nil]
    def make_tmp_dir
      FileUtils.mkdir_p @log_dir
      Dir["#{@log_dir}/*"].each do |file|
        FileUtils.rm_rf file
      end
    end
    
    # Loads the configuration file
    # @return [nil]
    def parse_config      
      path = "#{Rails.root}/config/mebla.yml"
      return unless File.exists?(path)
      
      conf = YAML::load(ERB.new(IO.read(path)).result)[Rails.env]
      
      conf.each do |key,value|
        self.send("#{key}=", value) if self.respond_to?("#{key}=")
      end unless conf.nil?
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
rmla-1.0 lib/mebla/configuration.rb
mebla-1.1.14 lib/mebla/configuration.rb
mebla-1.1.13 lib/mebla/configuration.rb
mebla-1.1.12 lib/mebla/configuration.rb
mebla-1.1.11 lib/mebla/configuration.rb
mebla-1.1.10 lib/mebla/configuration.rb
mebla-1.1.9 lib/mebla/configuration.rb
mebla-1.1.8 lib/mebla/configuration.rb
mebla-1.1.7 lib/mebla/configuration.rb
mebla-1.1.6 lib/mebla/configuration.rb
mebla-1.1.5 lib/mebla/configuration.rb
mebla-1.1.4 lib/mebla/configuration.rb
mebla-1.1.2 lib/mebla/configuration.rb
mebla-1.1.1 lib/mebla/configuration.rb
mebla-1.1.0 lib/mebla/configuration.rb
mebla-1.0.3 lib/mebla/configuration.rb
mebla-1.0.2 lib/mebla/configuration.rb
mebla-1.0.1 lib/mebla/configuration.rb
mebla-1.0.0 lib/mebla/configuration.rb