Sha256: 9e5a51c765e4f067976324fe0649fa26555a915c6ea4c95ad351e1c3901c0ab7

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

require 'ostruct'
require 'yaml'

class Skein::Config < OpenStruct
  # == Constants ============================================================

  CONFIG_PATH_DEFAULT = 'config/skein.yml'.freeze

  ENV_DEFAULT = 'development'.freeze

  DRIVERS = {
    bunny: 'Bunny',
    march_hare: 'MarchHare'
  }.freeze

  DRIVER_PLATFORM_DEFAULT = Hash.new(:bunny).merge(
    "java" => :march_hare
  ).freeze

  DRIVER_DEFAULT = (
    DRIVERS.find do |name, const|
      const_defined?(const)
    end || [ ]
  )[0] || DRIVER_PLATFORM_DEFAULT[RUBY_PLATFORM]

  DEFAULTS = {
    host: '127.0.0.1',
    port: 5672,
    username: 'guest',
    password: 'guest',
    driver: DRIVER_DEFAULT,
    namespace: nil
  }.freeze

  # == Class Methods ========================================================

  def self.root
    if (defined?(Rails))
      Rails.root
    else
      Dir.pwd
    end
  end

  def self.env
    if (defined?(Rails))
      Rails.env.to_s
    else
      ENV['RAILS_ENV'] || ENV_DEFAULT
    end
  end

  def self.path
    File.expand_path(CONFIG_PATH_DEFAULT, self.root)
  end

  def self.exist?
    File.exist?(self.path)
  end

  # == Instance Methods =====================================================

  def initialize(options = nil)
    config_path = nil

    case (options)
    when String
      if (File.exist?(options))
        config_path = options
      end
    when Hash
      super(options)

      return
    when false, :default
      # Ignore configuration file, use defaults
    else
      config_path = File.expand_path('config/skein.yml', self.class.root)
    end

    if (config_path and File.exist?(config_path))
      super(DEFAULTS.merge(YAML.load_file(config_path)[self.class.env] || { }))
    else
      super(DEFAULTS)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
skein-0.3.1 lib/skein/config.rb
skein-0.3.0 lib/skein/config.rb