lib/cratus/config.rb in cratus-0.2.1 vs lib/cratus/config.rb in cratus-0.2.2

- old
+ new

@@ -1,57 +1,62 @@ -# A generic way of constructing a mergeable configuration -class Cratus::Config < OpenStruct - # Construct a base config using the following order of precedence: - # * environment variables - # * YAML file - # * defaults - def load - # First, apply the defaults - defaults = { - group_dn_attribute: :cn, - group_member_attribute: :member, - group_description_attribute: :description, - group_objectclass: :group, - group_basedn: 'ou=groups,dc=example,dc=com', - group_memberof_attribute: :memberOf, - user_dn_attribute: :samaccountname, - user_objectclass: :user, - user_basedn: 'ou=users,dc=example,dc=com', - user_department_attribute: :department, - user_lockout_attribute: :lockouttime, - user_mail_attribute: :mail, - user_displayname_attribute: :displayName, - user_memberof_attribute: :memberOf, - host: 'ldap.example.com', - port: 389, - basedn: 'dc=example,dc=com', - username: 'username', - password: 'p@assedWard!', - } - merge defaults - - # Then apply the config file, if one exists - begin - apprc_dir = File.expand_path('~') - config_file = File.expand_path(File.join(apprc_dir, '.cratus.yml')) - merge YAML.load_file(config_file) if File.readable?(config_file) - rescue => e - puts "WARNING: Unable to read from #{config_file}" +module Cratus + # A generic way of constructing a mergeable configuration + class Config < OpenStruct + # A Hash of the default configuration options + def defaults + { + group_dn_attribute: :cn, + group_member_attribute: :member, + group_description_attribute: :description, + group_objectclass: :group, + group_basedn: 'ou=groups,dc=example,dc=com', + group_memberof_attribute: :memberOf, + user_dn_attribute: :samaccountname, + user_objectclass: :user, + user_basedn: 'ou=users,dc=example,dc=com', + user_department_attribute: :department, + user_lockout_attribute: :lockouttime, + user_mail_attribute: :mail, + user_displayname_attribute: :displayName, + user_memberof_attribute: :memberOf, + host: 'ldap.example.com', port: 389, + basedn: 'dc=example,dc=com', + username: 'username', + password: 'p@assedWard!' + } end - # Finally, apply any environment variables specified - env_conf = {} - defaults.keys.each do |key| - cratus_key = "CRATUS_#{key}".upcase - env_conf[key] = ENV[cratus_key] if ENV.key?(cratus_key) + # Construct a base config using the following order of precedence: + # * environment variables + # * YAML file + # * defaults + def load + # First, apply the defaults + merge defaults + + # Then apply the config file, if one exists + begin + apprc_dir = File.expand_path('~') + config_file = File.expand_path(File.join(apprc_dir, '.cratus.yml')) + merge YAML.load_file(config_file) if File.readable?(config_file) + rescue => e + puts "WARNING: Unable to read from #{config_file}: #{e.message}" + end + + # Finally, apply any environment variables specified + env_conf = {} + defaults.keys.each do |key| + cratus_key = "CRATUS_#{key}".upcase + env_conf[key] = ENV[cratus_key] if ENV.key?(cratus_key) + end + merge env_conf unless env_conf.empty? end - merge env_conf unless env_conf.empty? - end - def merge(data) - raise 'Invalid Config Data' unless data.is_a?(Hash) - data.each do |k, v| - self[k.to_sym] = v + def merge(data) + raise 'Invalid Config Data' unless data.is_a?(Hash) + data.each do |k, v| + self[k.to_sym] = v + end end end end # Make the config available as a singleton