application_configuration 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  require 'singleton'
2
2
  require 'yaml'
3
3
  require 'erb'
4
+ require 'open-uri'
4
5
  module Application
5
6
 
6
7
  class Configuration
@@ -24,6 +25,25 @@ module Application
24
25
 
25
26
  end
26
27
 
28
+ class Location
29
+
30
+ attr_accessor :type
31
+ attr_accessor :file_location
32
+
33
+ def initialize(loc, type = Application::Configuration::Location::FILE)
34
+ self.type = type
35
+ self.file_location = loc
36
+ end
37
+
38
+ def self.supported_types
39
+ [Application::Configuration::Location::FILE, Application::Configuration::Location::URL]
40
+ end
41
+
42
+ FILE = :file
43
+ URL = :url
44
+
45
+ end
46
+
27
47
 
28
48
  include Singleton
29
49
 
@@ -51,8 +71,8 @@ module Application
51
71
  @last_reload_time = Time.now # set the first load time
52
72
  @is_rails = Object.const_defined?("RAILS_ENV") # set whether it's rails
53
73
  if self.is_rails
54
- self.loaded_files << "#{RAILS_ROOT}/config/application_configuration.yml"
55
- self.loaded_files << "#{RAILS_ROOT}/config/application_configuration_#{RAILS_ENV}.yml"
74
+ self.loaded_files << Application::Configuration::Location.new("#{RAILS_ROOT}/config/application_configuration.yml")
75
+ self.loaded_files << Application::Configuration::Location.new("#{RAILS_ROOT}/config/application_configuration_#{RAILS_ENV}.yml")
56
76
  self.loaded_files.uniq!
57
77
  end
58
78
  reload # do the work!
@@ -60,36 +80,29 @@ module Application
60
80
 
61
81
  def load_file(path_to_file)
62
82
  unless path_to_file.nil?
63
- settings = load_from_file(path_to_file)
64
-
65
- @final_configuration_settings.merge!(settings)
66
-
67
- self.final_configuration_settings.each_pair do |k,v|
68
- k = k.to_s.downcase
69
- # start a list of methods that need to be generated
70
- # methods can't have :: in them so convert these to __.
71
- e_meths = [k.gsub("::", "__")]
72
- if k.match("::")
73
- vars = k.split("::") # get the namespace and the method names
74
- mod = vars.first # namespace
75
- e_meths << mod # generate a method for the namespace
76
- # create a new Namespace object for this name space and assign it to the final_configuration_settings hash.
77
- self.final_configuration_settings[mod.to_s] = Application::Configuration::Namespace.new(mod.to_s)
78
- # add an entry for the __ version of the key so our __ method can call it.
79
- self.final_configuration_settings[e_meths.first] = v
80
- end
81
- # generate all the necessary getter methods
82
- e_meths.each do |m|
83
- eval %{
84
- def #{m}
85
- self.final_configuration_settings["#{m}"]
86
- end
87
- }
88
- end
83
+ begin
84
+ path_to_file = Application::Configuration::Location.new(path_to_file) if path_to_file.is_a? String
85
+ settings = load_from_file(path_to_file.file_location)
86
+ handle_settings(settings, path_to_file)
87
+ return self
88
+ rescue Exception => e
89
+ puts e.message
90
+ return nil
91
+ end
92
+ end
93
+ end
94
+
95
+ def load_url(path_to_file)
96
+ unless path_to_file.nil?
97
+ begin
98
+ path_to_file = Application::Configuration::Location.new(path_to_file, Application::Configuration::Location::URL) if path_to_file.is_a? String
99
+ settings = load_from_url(path_to_file.file_location)
100
+ handle_settings(settings, path_to_file)
101
+ return self
102
+ rescue Exception => e
103
+ puts e.message
104
+ return nil
89
105
  end
90
-
91
- @loaded_files << path_to_file
92
- @loaded_files.uniq!
93
106
  end
94
107
  end
95
108
 
@@ -99,8 +112,15 @@ module Application
99
112
  @final_configuration_settings = {} # reset the configuration
100
113
 
101
114
  self.loaded_files.each do |lf|
102
- puts "Loading Configuration Settings from file: #{lf}"
103
- load_file(lf)
115
+ puts "Loading Configuration Settings from file: #{lf.file_location}"
116
+ case lf.type
117
+ when Application::Configuration::Location::FILE
118
+ load_file(lf)
119
+ when Application::Configuration::Location::URL
120
+ load_url(lf)
121
+ else
122
+ raise TypeError.new("The Application::Configuration::Location type '#{lf.type}' is not supported!")
123
+ end
104
124
  end
105
125
 
106
126
  @last_reload_time = Time.now
@@ -124,6 +144,42 @@ module Application
124
144
  YAML.load(template.result)
125
145
  end
126
146
 
147
+ def load_from_url(file)
148
+ template = ERB.new(open(file).read)
149
+ YAML.load(template.result)
150
+ end
151
+
152
+ def handle_settings(settings, path_to_file)
153
+ @final_configuration_settings.merge!(settings)
154
+
155
+ self.final_configuration_settings.each_pair do |k,v|
156
+ k = k.to_s.downcase
157
+ # start a list of methods that need to be generated
158
+ # methods can't have :: in them so convert these to __.
159
+ e_meths = [k.gsub("::", "__")]
160
+ if k.match("::")
161
+ vars = k.split("::") # get the namespace and the method names
162
+ mod = vars.first # namespace
163
+ e_meths << mod # generate a method for the namespace
164
+ # create a new Namespace object for this name space and assign it to the final_configuration_settings hash.
165
+ self.final_configuration_settings[mod.to_s] = Application::Configuration::Namespace.new(mod.to_s)
166
+ # add an entry for the __ version of the key so our __ method can call it.
167
+ self.final_configuration_settings[e_meths.first] = v
168
+ end
169
+ # generate all the necessary getter methods
170
+ e_meths.each do |m|
171
+ eval %{
172
+ def #{m}
173
+ self.final_configuration_settings["#{m}"]
174
+ end
175
+ }
176
+ end
177
+ end
178
+
179
+ @loaded_files << path_to_file
180
+ @loaded_files.uniq!
181
+ end
182
+
127
183
  end
128
184
 
129
185
  end
@@ -2,4 +2,4 @@
2
2
  gem_name: application_configuration
3
3
  package: application_configuration
4
4
  project: magrathea
5
- version: 1.0.2
5
+ version: 1.1.0
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: application_configuration
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.2
7
- date: 2007-09-17 00:00:00 -04:00
6
+ version: 1.1.0
7
+ date: 2007-09-18 00:00:00 -04:00
8
8
  summary: application_configuration
9
9
  require_paths:
10
10
  - lib