lib/global_session/configuration.rb in global_session-3.0.5 vs lib/global_session/configuration.rb in global_session-3.1.0

- old
+ new

@@ -17,10 +17,12 @@ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +require 'yaml' + module GlobalSession # Central point of access for GlobalSession configuration information. This is # mostly a very thin wrapper around the serialized hash written to the YAML config # file. # @@ -33,12 +35,12 @@ # * signed # * insecure # * ephemeral # * timeout # * renew - # * authority - # * trust + # * authority (optional - inferred from presence of private key file) + # * trust (optional - inferred from presence/name of public key files) # * directory # * cookie # * version # * name # * domain @@ -92,11 +94,11 @@ # MissingConfiguration:: if config file is missing or unreadable # TypeError:: if config file does not contain a YAML-serialized Hash def initialize(config, environment) if config.is_a?(Hash) @config = config - elsif File.readable?(config) + elsif File.file?(config) data = YAML.load(File.read(config)) unless data.is_a?(Hash) raise TypeError, "Configuration file #{File.basename(config)} must contain a hash as its top-level element" end @config = data @@ -119,13 +121,36 @@ # value(String):: the value of the configuration element def [](key) get(key, true) end + # Writer for configuration elements. Writes to an environment-specific stanza if one is present, + # else writes to the common stanza. DOES NOT OVERWRITE the key's value if it already has one! + # + # @param [String] key + # @param optional [Object] the value to write, or empty-hash as a default + def []=(key, value={}) + if @config.has_key?(@environment) + @config[@environment][key] ||= value + else + @config['common'][key] ||= value + end + rescue NoMethodError + raise MissingConfiguration, "Configuration key '#{key}' not found" + end + + # Determine whether a given configuration key was specified. + # + # @return [Boolean] true if the key is present in the common or per-environment stanzas + def has_key?(k) + @config[@environment].has_key?(k) || @config['common'].has_key?(k) + end + + alias key? has_key? + def validate # :nodoc - ['attributes/signed', 'cookie/name', - 'timeout'].each {|k| validate_presence_of k} + ['attributes/signed', 'cookie/name', 'timeout'].each {|k| validate_presence_of k} end protected # Helper method to check the presence of a key. Used in #validate. @@ -135,11 +160,16 @@ # # === Return # true always def validate_presence_of(key) elements = key.split '/' - object = get(elements.shift, false) + top_key = elements.shift + object = get(top_key, true) # pretend we're validated in order to get inheritance + if object.nil? + msg = "Configuration does not specify required element '#{top_key}'" + raise MissingConfiguration, msg + end elements.each do |element| object = object[element] if object if object.nil? msg = "Configuration does not specify required element #{elements.map { |x| "['#{x}']"}.join('')}" raise MissingConfiguration, msg @@ -148,17 +178,34 @@ true end private + # Get a configuration key. + # + # @return [Object] the value of the desired key + # @raise [MissingConfiguration] if the key is not found + # @param [String] key + # @param [Boolean] if true, check both the common and per-environment stanzas for the key def get(key, validated) # :nodoc - if @config.has_key?(@environment) && - @config[@environment].has_key?(key) - return @config[@environment][key] + if validated + # Fancy inheritance logic + if ('common' == key) && @config.key?(key) + # The common stanza itself + @config[key] + elsif (@environment == key) && @config.key?(key) + # The environment-specific stanza itself + @config[key] + elsif @config.key?(@environment) && @config[@environment].key?(key) + # Some key in the environment-specific stanza + return @config[@environment][key] + elsif @config.key?('common') && @config['common'].key?(key) + # By process of elimination, some key in the common stanza + @config['common'][key] + end else - @config['common'][key] + # Fail sauce + raise MissingConfiguration, "Configuration key '#{key}' not found" end - rescue NoMethodError - raise MissingConfiguration, "Configuration key '#{key}' not found" end end end