Sha256: 542719a002019f98b5cf65e2feef258554da55432e6bd9d7dca623381e57dce9

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'core_ext/hash'

module Fozzie

  # Fozzie configuration allows assignment of global properties
  # that will be used within the Fozzie codebase.
  class Configuration

    attr_accessor :env, :config_path, :host, :port, :appname

    def initialize(args = {})
      merge_and_assign_config(args)

      self
    end

    def data_prefix
      s = env
      s = [appname, s].join('.').strip unless appname.nil? || appname.empty?
      s
    end

    private

    # Handle the merging of the given configuaration, and the default config.
    # @return [Hash]
    def merge_and_assign_config(args = {})
      arg = self.class.default_configuration.merge(args.symbolize_keys)
      arg.delete_if {|key, val| !self.respond_to?(key.to_sym) }
      arg.merge!(config_from_yaml(arg))
      arg.each {|a,v| self.send("#{a}=", v) }

      arg
    end

    # Default configuration settings
    # @return [Hash]
    def self.default_configuration
      {
        :host        => '127.0.0.1',
        :port        => 8125,
        :config_path => '',
        :env         => (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'),
        :appname     => ''
      }.dup
    end

    def full_config_path(path)
      File.expand_path('config/fozzie.yml', path)
    end

    def config_from_yaml(args)
      fp = full_config_path(args[:config_path])
      return {} unless File.exists?(fp)
      cnf = YAML.load(File.open(fp))[args[:env]]
      (cnf.kind_of?(Hash)) ? cnf.symbolize_keys : {}
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fozzie-0.0.9 lib/fozzie/configuration.rb
fozzie-0.0.8 lib/fozzie/configuration.rb
fozzie-0.0.7 lib/fozzie/configuration.rb
fozzie-0.0.6 lib/fozzie/configuration.rb