Sha256: dfeb10ac2a59856b4189ede19260c07243f4fbf7400c0c9b8b980703de4ec6c6

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

require 'singleton'
require 'active_support/core_ext/hash/indifferent_access'

module Waistband
  class Configuration

    include Singleton

    attr_accessor :config_dir

    def initialize
      @yml_config = {}
      @indexes    = {}
    end

    def setup
      raise "Please define a valid `config_dir` configuration variable!"  unless config_dir
      raise "Couldn't find configuration directory #{config_dir}"         unless File.exist?(config_dir)

      @env        ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
      @yml_config   = YAML.load_file("#{config_dir}/waistband.yml")[@env].with_indifferent_access
    end

    def index(name)
      @indexes[name] ||= YAML.load_file("#{config_dir}/waistband_#{name}.yml")[@env].with_indifferent_access
    end

    def hostname
      "#{host}:#{port}"
    end

    def method_missing(method_name, *args, &block)
      return current_server[method_name]  if current_server[method_name]
      return @yml_config[method_name]     if @yml_config[method_name]
      super
    end

    private

      def current_server
        servers.sample
      end

      def servers
        @servers ||= @yml_config['servers'].map {|server_name, config| config}
      end

    # /private

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
waistband-0.2.1 lib/waistband/configuration.rb
waistband-0.2.0 lib/waistband/configuration.rb
waistband-0.1.1 lib/waistband/configuration.rb
waistband-0.0.15 lib/waistband/configuration.rb