Sha256: cffd9eb4b27ff60c349949d843d19a4b2913fae3dc1896bc04ddd64c5911fc21

Contents?: true

Size: 1.82 KB

Versions: 9

Compression:

Stored size: 1.82 KB

Contents

module UcbRails
  module Configuration
  
  # Manage configuration from file.  Per environment or overall.
  #
  # @example
  #   # config/config.yml
  #   test:
  #     ldap:
  #       username: test_username
  #       password: test_password
  # 
  #   ldap:
  #     username: top_username
  #     password: top_password
  #
  #   # in config/initializers/ucb_rails.rb  (e.g.)
  #   config = UcbRails::Credentials.new
  #   
  #   # in production -- pulls from top level, since no 'production' key
  #   config.for('ldap') #=> { 'username' => 'top_username', 'password' => 'top_password' }
  #
  #   # in test -- pulls from 'test' key
  #   config.for('ldap') #=> { 'username' => 'test_username', 'password' => 'test_password' }
  class Configuration
    FileNotFound = Class.new(StandardError)
    KeyNotFound = Class.new(StandardError)
    
    attr_accessor :config_filename, :config_yaml
    
    def initialize(filename=Rails.root.join('config/config.yml'))
      self.config_filename = filename.to_s
      load_file
    end
    
    # Return configuration value for _key_.
    # @param key [String]
    def for(key)
      from_environment(key) || from_top_level(key)
    end
    
    # Return configuration value for _key_.
    # @raise [UcbRails::Configuration::KeyNotFound] if _key_ not in configuration file.
    def for!(key)
      self.for(key) or raise(KeyNotFound, key.inspect)
    end
    
    private
    
    def from_environment(key)
      environmet_value && environmet_value[key]
    end

    def from_top_level(key)
      config_yaml[key]
    end
    
    def environmet_value
      config_yaml[RailsEnvironment.rails_env]
    end
    
    def load_file
      if File.exists?(config_filename) 
        self.config_yaml = YAML.load_file(config_filename)
      else
        raise(FileNotFound, config_filename)
      end
    end
  end
  
end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ucb_rails-0.0.14 app/models/ucb_rails/configuration/configuration.rb
ucb_rails-0.0.13 app/models/ucb_rails/configuration/configuration.rb
ucb_rails-0.0.12 app/models/ucb_rails/configuration/configuration.rb
ucb_rails-0.0.11 app/models/ucb_rails/configuration/configuration.rb
ucb_rails-0.0.10 app/models/ucb_rails/configuration/configuration.rb
ucb_rails-0.0.9 app/models/ucb_rails/configuration/configuration.rb
ucb_rails-0.0.8 app/models/ucb_rails/configuration/configuration.rb
ucb_rails-0.0.7 app/models/ucb_rails/configuration/configuration.rb
ucb_rails-0.0.6 app/models/ucb_rails/configuration/configuration.rb