Class: Credentials
- Inherits:
-
Hash
- Object
- Hash
- Credentials
- Defined in:
- lib/buzzcore/config.rb
Overview
credentials files look like : <?xml version=“1.0” encoding=“UTF-8”?> <Credentials> <SimpleItems namespace=“global”> <Item name=“”></Item> <Item name=“”></Item> <Item name=“”></Item> </SimpleItems> <SimpleItems namespace=“yore_test”> <Item name=“”></Item> <Item name=“”></Item> <Item name=“”></Item> </SimpleItems> </Credentials>
global .credentials.xml file
local .credentials.xml file cred = Credentials.new() # optionally specify filename or path or hash. if nil then use Dir.pwd
def initialize(aSource) # load global namespace from ~/.credentials.xml # load global namespace from local .credentials.xml # load given namespace from ~/.credentials.xml # load given namespace from local .credentials.xml # merge all top to bottom
Constant Summary collapse
- CRED_FILENAME =
".credentials.xml"
Instance Method Summary collapse
- #find_file_upwards(aFilename, aStartPath = nil) ⇒ Object
- #get_all_credentials(aXmlRoot) ⇒ Object
- #get_local_credentials(aSource = nil) ⇒ Object
-
#get_user_credentials ⇒ Object
XmlUtils.read_simple_items(@xmlRoot,‘/Yore/SimpleItems’).
-
#initialize(aNamespace = nil, aSource = nil) ⇒ Credentials
constructor
A new instance of Credentials.
Constructor Details
#initialize(aNamespace = nil, aSource = nil) ⇒ Credentials
Returns a new instance of Credentials.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/buzzcore/config.rb', line 194 def initialize(aNamespace=nil,aSource=nil) #HOME_PATH can be preset by tests eg. ::Credentials.const_set('HOME_PATH',@user_dir) Credentials.const_set("HOME_PATH", ENV['HOME']) unless Credentials.const_defined? "HOME_PATH" arrCredentials = [] user_credentials = get_user_credentials() local_credentials = get_local_credentials(aSource) arrCredentials << user_credentials[:global] if user_credentials arrCredentials << local_credentials[:global] if local_credentials arrCredentials << user_credentials[aNamespace.to_sym] if aNamespace && user_credentials arrCredentials << local_credentials[aNamespace.to_sym] if aNamespace && local_credentials arrCredentials.compact! arrCredentials.each do |c| self.merge!(c) end end |
Instance Method Details
#find_file_upwards(aFilename, aStartPath = nil) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/buzzcore/config.rb', line 159 def find_file_upwards(aFilename,aStartPath=nil) aStartPath ||= Dir.pwd return nil if aFilename.nil? || aFilename.empty? arrPath = aStartPath.split(File::SEPARATOR) while arrPath.length > 0 path = File.join(arrPath.join(File::SEPARATOR),aFilename) return path if File.exists?(path) arrPath.pop end return nil end |
#get_all_credentials(aXmlRoot) ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/buzzcore/config.rb', line 171 def get_all_credentials(aXmlRoot) return nil unless aXmlRoot result = {} REXML::XPath.each(aXmlRoot, '/Credentials/SimpleItems') do |si| ns = si.attributes['Namespace'] values = XmlUtils.read_simple_items(si) result[ns.to_sym] = values.symbolize_keys if ns && values end return result end |
#get_local_credentials(aSource = nil) ⇒ Object
187 188 189 190 191 192 |
# File 'lib/buzzcore/config.rb', line 187 def get_local_credentials(aSource=nil) aSource ||= Dir.pwd # assume source is a directory path, but other types could be supported later return nil unless file=find_file_upwards(CRED_FILENAME,aSource) return get_all_credentials(XmlUtils.get_file_root(file)) end |
#get_user_credentials ⇒ Object
XmlUtils.read_simple_items(@xmlRoot,‘/Yore/SimpleItems’)
183 184 185 |
# File 'lib/buzzcore/config.rb', line 183 def get_user_credentials return get_all_credentials(XmlUtils.get_file_root(File.join(HOME_PATH,CRED_FILENAME))) end |