Sha256: 084c684976b4e564ef1ab3d099d2418bb3ac744893ccf0d2f8ed6c9209fb3a57

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

module HasGlobalSession
  module Configuration
    mattr_accessor :config_file
    mattr_accessor :environment
    
    def self.[](key)
      get(key, true)
    end

    def self.validate
      ['attributes/signed', 'integrated', 'cookie/name', 'cookie/domain'].each do |path|
        elements = path.split '/'
        object = get(elements.shift, false)
        elements.each do |element|
          object = object[element]
          if object.nil?
            msg = "#{File.basename(config_file)} does not specify required element #{elements.map { |x| "['#{x}']"}.join('')}"
            raise MissingConfiguration, msg
          end
        end
      end
    end

    private
    def self.get(key, validated)
      unless @config
        raise MissingConfiguration, "config_file is nil; cannot read configuration" unless config_file
        raise MissingConfiguration, "environment is nil; must be specified" unless environment
        @config = YAML.load(File.read(config_file))
        raise TypeError, "#{config_file} must contain a Hash!" unless Hash === @config
        validate if validated
      end
      if @config.has_key?(environment) &&
         @config[environment].respond_to?(:has_key?) &&
         @config[environment].has_key?(key)
        return @config[environment][key]
      else
        @config['common'][key]
      end
    end
  end  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
has_global_session-0.8.3 lib/has_global_session/configuration.rb
has_global_session-0.8.2 lib/has_global_session/configuration.rb
has_global_session-0.8.1 lib/has_global_session/configuration.rb