Sha256: 01e9ad4df12c509282f1f4d1bd953e2df1e73195bf162018b8ca93aa60f43de5

Contents?: true

Size: 1.78 KB

Versions: 24

Compression:

Stored size: 1.78 KB

Contents

module UcbRailsUser
  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 = UcbRailsUser::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 [UcbRailsUser::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)
      environment_value && environment_value[key]
    end

    def from_top_level(key)
      config_yaml[key]
    end

    def environment_value
      config_yaml[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

24 entries across 24 versions & 1 rubygems

Version Path
ucb_rails_user-5.0.0 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.1.2 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.1.1 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.1.0 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.7 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.6 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.5 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.4 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.3 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.2 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.1 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.0 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-3.0.2 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-4.0.0.beta1 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-3.0.1 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-3.0.0 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-2.0.1 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-2.0.0 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-1.1.3 app/models/ucb_rails_user/configuration/configuration.rb
ucb_rails_user-1.1.2 app/models/ucb_rails_user/configuration/configuration.rb