buzzcore 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2008-2009 Gary McGhee - Buzzware Solutions, Western Australia - gary@buzzware.com.au
2
+
3
+ MIT Licence
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/README.rdoc ADDED
@@ -0,0 +1,46 @@
1
+ = buzzcore
2
+
3
+ * Author : http://www.buzzware.com.au
4
+
5
+ == DESCRIPTION:
6
+
7
+ Core library of Buzzware Solutions
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * something
12
+
13
+ == REQUIREMENTS:
14
+
15
+ * tested on MacOS and Linux
16
+
17
+ == INSTALL:
18
+
19
+ sudo gem sources -a http://gems.github.com
20
+ sudo gem install buzzware-buzzcore
21
+
22
+ == LICENSE:
23
+
24
+ (The MIT License)
25
+
26
+ Copyright (c) 2009 Gary McGhee, Buzzware Solutions
27
+
28
+ Permission is hereby granted, free of charge, to any person obtaining
29
+ a copy of this software and associated documentation files (the
30
+ 'Software'), to deal in the Software without restriction, including
31
+ without limitation the rights to use, copy, modify, merge, publish,
32
+ distribute, sublicense, and/or sell copies of the Software, and to
33
+ permit persons to whom the Software is furnished to do so, subject to
34
+ the following conditions:
35
+
36
+ The above copyright notice and this permission notice shall be
37
+ included in all copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
40
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
43
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46
+
data/Rakefile ADDED
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "buzzcore"
8
+ gem.summary = %Q{buzzcore is the ruby core library developed and used by Buzzware Solutions.}
9
+ gem.description = %Q{buzzcore is the ruby core library developed and used by Buzzware Solutions.}
10
+ gem.email = "contact@buzzware.com.au"
11
+ gem.homepage = "http://github.com/buzzware/buzzcore"
12
+ gem.authors = ["buzzware"]
13
+ #gem.rubyforge_project = "buzzware"
14
+ gem.add_development_dependency "thoughtbot-shoulda"
15
+ gem.files.include %w(
16
+ lib/buzzcore.rb
17
+ )
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::RubyforgeTasks.new do |rubyforge|
21
+ rubyforge.doc_task = "rdoc"
22
+ end
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
25
+ end
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ begin
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/*_test.rb'
39
+ test.verbose = true
40
+ end
41
+ rescue LoadError
42
+ task :rcov do
43
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
44
+ end
45
+ end
46
+
47
+ task :test => :check_dependencies
48
+
49
+ task :default => :test
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ if File.exist?('VERSION')
54
+ version = File.read('VERSION')
55
+ else
56
+ version = ""
57
+ end
58
+
59
+ rdoc.rdoc_dir = 'rdoc'
60
+ rdoc.title = "buzzcore #{version}"
61
+ rdoc.rdoc_files.include('README*')
62
+ rdoc.rdoc_files.include('lib/**/*.rb')
63
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.5
data/buzzcore.gemspec ADDED
@@ -0,0 +1,77 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{buzzcore}
8
+ s.version = "0.2.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["buzzware"]
12
+ s.date = %q{2009-10-06}
13
+ s.description = %q{buzzcore is the ruby core library developed and used by Buzzware Solutions.}
14
+ s.email = %q{contact@buzzware.com.au}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "buzzcore.gemspec",
27
+ "buzzcore.vpj",
28
+ "buzzcore.vpw",
29
+ "lib/buzzcore.rb",
30
+ "lib/buzzcore.rb",
31
+ "lib/buzzcore/config.rb",
32
+ "lib/buzzcore/database_utils.rb",
33
+ "lib/buzzcore/enum.rb",
34
+ "lib/buzzcore/extend_base_classes.rb",
35
+ "lib/buzzcore/html_utils.rb",
36
+ "lib/buzzcore/logging.rb",
37
+ "lib/buzzcore/misc_utils.rb",
38
+ "lib/buzzcore/require_paths.rb",
39
+ "lib/buzzcore/shell_extras.rb",
40
+ "lib/buzzcore/string_utils.rb",
41
+ "lib/buzzcore/text_doc.rb",
42
+ "lib/buzzcore/thread_utils.rb",
43
+ "lib/buzzcore/xml_utils.rb",
44
+ "lib/buzzcore_dev.rb",
45
+ "test/buzzcore_test.rb",
46
+ "test/config_test.rb",
47
+ "test/credentials_test.rb",
48
+ "test/shell_test.rb",
49
+ "test/test_helper.rb"
50
+ ]
51
+ s.has_rdoc = true
52
+ s.homepage = %q{http://github.com/buzzware/buzzcore}
53
+ s.rdoc_options = ["--charset=UTF-8"]
54
+ s.require_paths = ["lib"]
55
+ s.rubygems_version = %q{1.3.1}
56
+ s.summary = %q{buzzcore is the ruby core library developed and used by Buzzware Solutions.}
57
+ s.test_files = [
58
+ "test/buzzcore_test.rb",
59
+ "test/config_test.rb",
60
+ "test/credentials_test.rb",
61
+ "test/shell_test.rb",
62
+ "test/test_helper.rb"
63
+ ]
64
+
65
+ if s.respond_to? :specification_version then
66
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
67
+ s.specification_version = 2
68
+
69
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
70
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
71
+ else
72
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
73
+ end
74
+ else
75
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
76
+ end
77
+ end
data/buzzcore.vpj ADDED
@@ -0,0 +1,93 @@
1
+ <!DOCTYPE Project SYSTEM "http://www.slickedit.com/dtd/vse/10.0/vpj.dtd">
2
+ <Project
3
+ Version="10.0"
4
+ VendorName="SlickEdit"
5
+ WorkingDir=".">
6
+ <Config
7
+ Name="Release"
8
+ OutputFile=""
9
+ CompilerConfigName="Latest Version">
10
+ <Menu>
11
+ <Target
12
+ Name="Compile"
13
+ MenuCaption="&amp;Compile"
14
+ CaptureOutputWith="ProcessBuffer"
15
+ SaveOption="SaveCurrent"
16
+ RunFromDir="%rw">
17
+ <Exec/>
18
+ </Target>
19
+ <Target
20
+ Name="Build"
21
+ MenuCaption="&amp;Build"
22
+ CaptureOutputWith="ProcessBuffer"
23
+ SaveOption="SaveWorkspaceFiles"
24
+ RunFromDir="%rw">
25
+ <Exec/>
26
+ </Target>
27
+ <Target
28
+ Name="Rebuild"
29
+ MenuCaption="&amp;Rebuild"
30
+ CaptureOutputWith="ProcessBuffer"
31
+ SaveOption="SaveWorkspaceFiles"
32
+ RunFromDir="%rw">
33
+ <Exec/>
34
+ </Target>
35
+ <Target
36
+ Name="Debug"
37
+ MenuCaption="&amp;Debug"
38
+ SaveOption="SaveNone"
39
+ RunFromDir="%rw">
40
+ <Exec/>
41
+ </Target>
42
+ <Target
43
+ Name="Execute"
44
+ MenuCaption="E&amp;xecute"
45
+ SaveOption="SaveNone"
46
+ RunFromDir="%rw"
47
+ CaptureOutputWith="ProcessBuffer"
48
+ ClearProcessBuffer="1">
49
+ <Exec CmdLine="source actions.sh execute"/>
50
+ </Target>
51
+ </Menu>
52
+ </Config>
53
+ <CustomFolders>
54
+ <Folder
55
+ Name="Source Files"
56
+ Filters="*.c;*.C;*.cc;*.cpp;*.cp;*.cxx;*.c++;*.prg;*.pas;*.dpr;*.asm;*.s;*.bas;*.java;*.cs;*.sc;*.e;*.cob;*.html;*.rc;*.tcl;*.py;*.pl"/>
57
+ <Folder
58
+ Name="Header Files"
59
+ Filters="*.h;*.H;*.hh;*.hpp;*.hxx;*.inc;*.sh;*.cpy;*.if"/>
60
+ <Folder
61
+ Name="Resource Files"
62
+ Filters="*.ico;*.cur;*.dlg"/>
63
+ <Folder
64
+ Name="Bitmaps"
65
+ Filters="*.bmp"/>
66
+ <Folder
67
+ Name="Other Files"
68
+ Filters="">
69
+ </Folder>
70
+ </CustomFolders>
71
+ <Files AutoFolders="DirectoryView">
72
+ <Folder Name="lib">
73
+ <F
74
+ N="lib/*"
75
+ Recurse="1"
76
+ Refilter="0"
77
+ Excludes=""/>
78
+ </Folder>
79
+ <Folder Name="test">
80
+ <F
81
+ N="test/*"
82
+ Recurse="0"
83
+ Refilter="0"
84
+ Excludes=".svn/"/>
85
+ </Folder>
86
+ <F N="buzzcore.gemspec"/>
87
+ <F N="History.txt"/>
88
+ <F N="Manifest.txt"/>
89
+ <F N="PostInstall.txt"/>
90
+ <F N="Rakefile"/>
91
+ <F N="README.rdoc"/>
92
+ </Files>
93
+ </Project>
data/buzzcore.vpw ADDED
@@ -0,0 +1,93 @@
1
+ <!DOCTYPE Project SYSTEM "http://www.slickedit.com/dtd/vse/10.0/vpj.dtd">
2
+ <Project
3
+ Version="10.0"
4
+ VendorName="SlickEdit"
5
+ WorkingDir=".">
6
+ <Config
7
+ Name="Release"
8
+ OutputFile=""
9
+ CompilerConfigName="Latest Version">
10
+ <Menu>
11
+ <Target
12
+ Name="Compile"
13
+ MenuCaption="&amp;Compile"
14
+ CaptureOutputWith="ProcessBuffer"
15
+ SaveOption="SaveCurrent"
16
+ RunFromDir="%rw">
17
+ <Exec/>
18
+ </Target>
19
+ <Target
20
+ Name="Build"
21
+ MenuCaption="&amp;Build"
22
+ CaptureOutputWith="ProcessBuffer"
23
+ SaveOption="SaveWorkspaceFiles"
24
+ RunFromDir="%rw">
25
+ <Exec/>
26
+ </Target>
27
+ <Target
28
+ Name="Rebuild"
29
+ MenuCaption="&amp;Rebuild"
30
+ CaptureOutputWith="ProcessBuffer"
31
+ SaveOption="SaveWorkspaceFiles"
32
+ RunFromDir="%rw">
33
+ <Exec/>
34
+ </Target>
35
+ <Target
36
+ Name="Debug"
37
+ MenuCaption="&amp;Debug"
38
+ SaveOption="SaveNone"
39
+ RunFromDir="%rw">
40
+ <Exec/>
41
+ </Target>
42
+ <Target
43
+ Name="Execute"
44
+ MenuCaption="E&amp;xecute"
45
+ SaveOption="SaveNone"
46
+ RunFromDir="%rw"
47
+ CaptureOutputWith="ProcessBuffer"
48
+ ClearProcessBuffer="1">
49
+ <Exec CmdLine="source actions.sh execute"/>
50
+ </Target>
51
+ </Menu>
52
+ </Config>
53
+ <CustomFolders>
54
+ <Folder
55
+ Name="Source Files"
56
+ Filters="*.c;*.C;*.cc;*.cpp;*.cp;*.cxx;*.c++;*.prg;*.pas;*.dpr;*.asm;*.s;*.bas;*.java;*.cs;*.sc;*.e;*.cob;*.html;*.rc;*.tcl;*.py;*.pl"/>
57
+ <Folder
58
+ Name="Header Files"
59
+ Filters="*.h;*.H;*.hh;*.hpp;*.hxx;*.inc;*.sh;*.cpy;*.if"/>
60
+ <Folder
61
+ Name="Resource Files"
62
+ Filters="*.ico;*.cur;*.dlg"/>
63
+ <Folder
64
+ Name="Bitmaps"
65
+ Filters="*.bmp"/>
66
+ <Folder
67
+ Name="Other Files"
68
+ Filters="">
69
+ </Folder>
70
+ </CustomFolders>
71
+ <Files AutoFolders="DirectoryView">
72
+ <Folder Name="lib">
73
+ <F
74
+ N="lib/*"
75
+ Recurse="1"
76
+ Refilter="0"
77
+ Excludes=""/>
78
+ </Folder>
79
+ <Folder Name="test">
80
+ <F
81
+ N="test/*"
82
+ Recurse="0"
83
+ Refilter="0"
84
+ Excludes=".svn/"/>
85
+ </Folder>
86
+ <F N="buzzcore.gemspec"/>
87
+ <F N="History.txt"/>
88
+ <F N="Manifest.txt"/>
89
+ <F N="PostInstall.txt"/>
90
+ <F N="Rakefile"/>
91
+ <F N="README.rdoc"/>
92
+ </Files>
93
+ </Project>
@@ -0,0 +1,239 @@
1
+ require 'buzzcore/xml_utils'
2
+ require 'buzzcore/extend_base_classes'
3
+
4
+ class ConfigClass < Hash
5
+
6
+ attr_reader :default_values
7
+
8
+ def initialize(aDefaultValues,aNewValues=nil,&aBlock)
9
+ @default_values = aDefaultValues.clone
10
+ reset()
11
+ if aNewValues
12
+ block_given? ? read(aNewValues,&aBlock) : read(aNewValues)
13
+ end
14
+ end
15
+
16
+ # aBlock allows values to be filtered based on key,default and new values
17
+ def read(aSource,&aBlock)
18
+ default_values.each do |k,v|
19
+ done = false
20
+ if block_given? && ((newv = yield(k,v,aSource && aSource[k])) != nil)
21
+ self[k] = newv
22
+ done = true
23
+ end
24
+ copy_item(aSource,k) if !done && aSource && !aSource[k].nil?
25
+ end
26
+ self
27
+ end
28
+
29
+ # reset values back to defaults
30
+ def reset
31
+ self.clear
32
+ me = self
33
+ @default_values.each {|n,v| me[n] = v.is_a?(Class) ? nil : v}
34
+ end
35
+
36
+ def set_int(aKey,aValue)
37
+ case aValue
38
+ when String then self[aKey] = aValue.to_integer(self[aKey]);
39
+ when Fixnum then self[aKey] = aValue;
40
+ when Float then self[aKey] = aValue.to_i;
41
+ end
42
+ end
43
+
44
+ def set_float(aKey,aValue)
45
+ case aValue
46
+ when String then self[aKey] = aValue.to_float(self[aKey]);
47
+ when Fixnum then self[aKey] = aValue.to_f;
48
+ when Float then self[aKey] = aValue;
49
+ end
50
+ end
51
+
52
+ def set_boolean(aKey,aValue)
53
+ case aValue
54
+ when TrueClass,FalseClass then self[aKey] = aValue;
55
+ when String then self[aKey] = (['1','yes','y','true','on'].include?(aValue.downcase))
56
+ else
57
+ set_boolean(aKey,aValue.to_s)
58
+ end
59
+ end
60
+
61
+ def set_symbol(aKey,aValue)
62
+ case aValue
63
+ when String then self[aKey] = (aValue.to_sym rescue nil);
64
+ when Symbol then self[aKey] = aValue;
65
+ end
66
+ end
67
+
68
+ def copy_item(aHash,aKey)
69
+ d = default_values[aKey]
70
+ d_class = (d.is_a?(Class) ? d : d.class)
71
+ cname = d_class.name.to_sym
72
+ case cname
73
+ when :NilClass then ;
74
+ when :String then self[aKey] = aHash[aKey].to_s unless aHash[aKey].nil?
75
+ when :Float then set_float(aKey,aHash[aKey]);
76
+ when :Fixnum then set_int(aKey,aHash[aKey]);
77
+ when :TrueClass, :FalseClass then set_boolean(aKey,aHash[aKey]);
78
+ when :Symbol then self[aKey] = (aHash[aKey].to_sym rescue nil)
79
+ else
80
+ raise StandardError.new('unsupported type')
81
+ end
82
+ end
83
+
84
+ def copy_strings(aHash,*aKeys)
85
+ aKeys.each do |k|
86
+ self[k] = aHash[k].to_s unless aHash[k].nil?
87
+ end
88
+ end
89
+
90
+ def copy_ints(*aDb)
91
+ aHash = aDb.shift
92
+ aKeys = aDb
93
+ aKeys.each do |k|
94
+ set_int(k,aHash[k])
95
+ end
96
+ end
97
+
98
+ def copy_floats(aHash,*aKeys)
99
+ aKeys.each do |k|
100
+ set_float(k,aHash[k])
101
+ end
102
+ end
103
+
104
+ def copy_booleans(aHash,*aKeys)
105
+ aKeys.each do |k|
106
+ set_boolean(k,aHash[k])
107
+ end
108
+ end
109
+
110
+ def to_hash
111
+ {}.merge(self)
112
+ end
113
+
114
+ end
115
+
116
+ # use like this :
117
+ #
118
+ # in initializer or environment.rb :
119
+ #APP_CONFIG = ConfigXmlClass.from_file(
120
+ # SITE_CONFIG_DEFAULTS = {
121
+ # :something => String, # giving a class means the type is the given class, and the default value is nil
122
+ # :session_key => '_session',
123
+ # :upload_path=> 'cms/uploads',
124
+ # :thumbs_cache => File.expand_path('public/thumbs_cache',RAILS_ROOT), # make sure this exists with correct permissions
125
+ # :thumbs_url => '/thumbs_cache'
126
+ # },
127
+ # File.expand_path('config/config.xml',RAILS_ROOT)
128
+ #)
129
+ #
130
+ # config/logikal.config.xml :
131
+ #
132
+ #<?xml version="1.0" encoding="UTF-8"?>
133
+ #<Config>
134
+ # <SimpleItems>
135
+ # <Item Name="upload_path">../shared/uploads</Item>
136
+ # <Item Name="something">a value</Item>
137
+ # </SimpleItems>
138
+ #</Config>
139
+ #
140
+ # then throughout app eg. :
141
+ #
142
+ # ... APP_CONFIG[:upload_path]
143
+ #
144
+
145
+ class ConfigXmlClass < ConfigClass
146
+ attr_accessor :xmlRoot
147
+ def initialize(aDefaultValues,aConfig)
148
+ return super(aDefaultValues,aConfig) unless aConfig.is_a?(REXML::Element)
149
+ @xmlRoot = aConfig.deep_clone
150
+ super(aDefaultValues,XmlUtils.read_simple_items(@xmlRoot,'SimpleItems').symbolize_keys)
151
+ end
152
+
153
+ def self.from_file(aDefaultValues,aFile)
154
+ xml = XmlUtils.get_file_root(aFile)
155
+ return ConfigXmlClass.new(aDefaultValues,xml)
156
+ end
157
+ end
158
+
159
+ # credentials files look like :
160
+ #<?xml version="1.0" encoding="UTF-8"?>
161
+ #<Credentials>
162
+ # <SimpleItems namespace="global">
163
+ # <Item name=""></Item>
164
+ # <Item name=""></Item>
165
+ # <Item name=""></Item>
166
+ # </SimpleItems>
167
+ # <SimpleItems namespace="yore_test">
168
+ # <Item name=""></Item>
169
+ # <Item name=""></Item>
170
+ # <Item name=""></Item>
171
+ # </SimpleItems>
172
+ #</Credentials>
173
+ #
174
+ # global .credentials.xml file
175
+ # local .credentials.xml file
176
+ # cred = Credentials.new() # optionally specify filename or path or hash. if nil then use Dir.pwd
177
+ #
178
+ # def initialize(aSource)
179
+ # # load global namespace from ~/.credentials.xml
180
+ # # load global namespace from local .credentials.xml
181
+ # # load given namespace from ~/.credentials.xml
182
+ # # load given namespace from local .credentials.xml
183
+ # # merge all top to bottom
184
+ class Credentials < Hash
185
+
186
+ CRED_FILENAME = ".credentials.xml"
187
+
188
+ def find_file_upwards(aFilename,aStartPath=nil)
189
+ aStartPath ||= Dir.pwd
190
+ return nil if aFilename.nil? || aFilename.empty?
191
+ arrPath = aStartPath.split(File::SEPARATOR)
192
+ while arrPath.length > 0
193
+ path = File.join(arrPath.join(File::SEPARATOR),aFilename)
194
+ return path if File.exists?(path)
195
+ arrPath.pop
196
+ end
197
+ return nil
198
+ end
199
+
200
+ def get_all_credentials(aXmlRoot)
201
+ return nil unless aXmlRoot
202
+ result = {}
203
+ REXML::XPath.each(aXmlRoot, '/Credentials/SimpleItems') do |si|
204
+ ns = si.attributes['Namespace']
205
+ values = XmlUtils.read_simple_items(si)
206
+ result[ns.to_sym] = values.symbolize_keys if ns && values
207
+ end
208
+ return result
209
+ end
210
+
211
+ #XmlUtils.read_simple_items(@xmlRoot,'/Yore/SimpleItems')
212
+ def get_user_credentials
213
+ return get_all_credentials(XmlUtils.get_file_root(File.join(HOME_PATH,CRED_FILENAME)))
214
+ end
215
+
216
+ def get_local_credentials(aSource=nil)
217
+ aSource ||= Dir.pwd
218
+ # assume source is a directory path, but other types could be supported later
219
+ return nil unless file=find_file_upwards(CRED_FILENAME,aSource)
220
+ return get_all_credentials(XmlUtils.get_file_root(file))
221
+ end
222
+
223
+ def initialize(aNamespace=nil,aSource=nil)
224
+ #HOME_PATH can be preset by tests eg. ::Credentials.const_set('HOME_PATH',@user_dir)
225
+ Credentials.const_set("HOME_PATH", ENV['HOME']) unless Credentials.const_defined? "HOME_PATH"
226
+ arrCredentials = []
227
+ user_credentials = get_user_credentials()
228
+ local_credentials = get_local_credentials(aSource)
229
+ arrCredentials << user_credentials[:global] if user_credentials
230
+ arrCredentials << local_credentials[:global] if local_credentials
231
+ arrCredentials << user_credentials[aNamespace.to_sym] if aNamespace && user_credentials
232
+ arrCredentials << local_credentials[aNamespace.to_sym] if aNamespace && local_credentials
233
+ arrCredentials.compact!
234
+ arrCredentials.each do |c|
235
+ self.merge!(c)
236
+ end
237
+ end
238
+ end
239
+