Sha256: 8276b7f8b3af169192ffd7e0af93f9af82f37ffc471714f4df52fc0123709e0b

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'yaml'

module Waffle
  module Config
    extend self
    attr_accessor :settings, :defaults

    @settings = {}
    @defaults = {}

    def option(name, options = {})
      defaults[name] = settings[name] = options[:default]

      class_eval <<-RUBY
        def #{name}
          settings[#{name.inspect}]
        end

        def #{name}=(value)
          settings[#{name.inspect}] = value
        end

        def #{name}?
          #{name}
        end
      RUBY
    end

    option :url, :default => nil
    option :encoder, :default => 'json'
    option :transport, :default => nil
    option :connection_attempt_timeout, :default => 30

    def load! options=nil
      options[:path] ? load_from_yaml!(options[:path]) : load_from_hash!(options)
      self
    end

    def load_from_yaml! filename
      filename = Rails.root.join(filename) if defined?(Rails)
      filename = File.expand_path(filename)

      if File.exists?(filename)
        settings_hash = YAML.load_file(filename)[environment]
        @settings = defaults.merge(settings_hash.symbolize_keys) if settings_hash
      end
    end

    def load_from_hash! options
      @settings = defaults.merge(options)
    end
    
    def environment
      defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : (ENV['RACK_ENV'] || 'development')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
waffle-0.4.0 lib/waffle/config.rb