application_configuration 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'application_configuration'
@@ -0,0 +1,180 @@
1
+ require 'singleton'
2
+ require 'yaml'
3
+ require 'erb'
4
+ module Application
5
+
6
+ class Configuration
7
+
8
+ # this class is used to namespace configurations
9
+ class Namespace
10
+
11
+ attr_accessor :namespace_name
12
+
13
+ def initialize(name)
14
+ self.namespace_name = name
15
+ end
16
+
17
+ def to_s
18
+ self.namespace_name
19
+ end
20
+
21
+ def method_missing(sym, *args)
22
+ Application::Configuration.send("#{self}__#{sym}", *args)
23
+ end
24
+
25
+ end
26
+
27
+
28
+ include Singleton
29
+
30
+ class << self
31
+
32
+ # attr_reader :config_file_location
33
+ #
34
+ # def init_config
35
+ #
36
+ # end
37
+
38
+ def method_missing(sym, *args)
39
+ inst = Application::Configuration.instance
40
+ # check to see if the configuration needs to be reloaded.
41
+ if (Time.now - (inst.reload_settings_every || 0)) > inst.last_reload_time
42
+ inst.reload
43
+ end
44
+ inst.send(sym, *args)
45
+ end
46
+
47
+ end
48
+
49
+ attr_reader :final_configuration_settings #stores the final configuration settings
50
+ attr_reader :last_reload_time #stores the last time settings were reloaded
51
+ attr_reader :is_rails #stores whether or not we're in a rails app
52
+ attr_reader :loaded_files
53
+
54
+ def initialize
55
+ @loaded_files = []
56
+ @final_configuration_settings = {}
57
+ @last_reload_time = Time.now # set the first load time
58
+ @is_rails = Object.const_defined?("RAILS_ENV") # set whether it's rails
59
+ if self.is_rails
60
+ self.loaded_files << "#{RAILS_ROOT}/config/application_configuration.yml"
61
+ self.loaded_files << "#{RAILS_ROOT}/config/application_configuration_#{RAILS_ENV}.yml"
62
+ self.loaded_files.uniq!
63
+ end
64
+ reload # do the work!
65
+ end
66
+
67
+ def load_file(path_to_file)
68
+ unless path_to_file.nil?
69
+ settings = load_from_file(path_to_file)
70
+
71
+ @final_configuration_settings.merge!(settings)
72
+
73
+ self.final_configuration_settings.each_pair do |k,v|
74
+ k = k.to_s.downcase
75
+ # start a list of methods that need to be generated
76
+ # methods can't have :: in them so convert these to __.
77
+ e_meths = [k.gsub("::", "__")]
78
+ if k.match("::")
79
+ vars = k.split("::") # get the namespace and the method names
80
+ mod = vars.first # namespace
81
+ e_meths << mod # generate a method for the namespace
82
+ # create a new Namespace object for this name space and assign it to the final_configuration_settings hash.
83
+ self.final_configuration_settings[mod.to_s] = Application::Configuration::Namespace.new(mod.to_s)
84
+ # add an entry for the __ version of the key so our __ method can call it.
85
+ self.final_configuration_settings[e_meths.first] = v
86
+ end
87
+ # generate all the necessary getter methods
88
+ e_meths.each do |m|
89
+ eval %{
90
+ def #{m}
91
+ self.final_configuration_settings["#{m}"]
92
+ end
93
+ }
94
+ end
95
+ end
96
+
97
+ @loaded_files << path_to_file
98
+ @loaded_files.uniq!
99
+ end
100
+ end
101
+
102
+ def reload
103
+ puts "Loading Configuration Settings!"
104
+ settings = {:reload_settings_every => 1.minute} # default reload time
105
+ @final_configuration_settings = {} # reset the configuration
106
+
107
+ self.loaded_files.each do |lf|
108
+ load_file(lf)
109
+ end
110
+
111
+ # if it's rails:
112
+ # if self.is_rails
113
+ # # load the default set of configurations for the app.
114
+ # settings.merge!(load_from_file("#{RAILS_ROOT}/config/application_configuration.yml"))
115
+ # puts settings.inspect
116
+ # begin
117
+ # # load the specific environment configurations if they exist.
118
+ # settings.merge!(load_from_file("#{RAILS_ROOT}/config/#{RAILS_ENV}_application_configuration.yml"))
119
+ # puts settings.inspect
120
+ # rescue Exception => e
121
+ # end
122
+ # end
123
+
124
+ # @final_configuration_settings = settings
125
+ #
126
+ # self.final_configuration_settings.each_pair do |k,v|
127
+ # k = k.to_s.downcase
128
+ # # start a list of methods that need to be generated
129
+ # # methods can't have :: in them so convert these to __.
130
+ # e_meths = [k.gsub("::", "__")]
131
+ # if k.match("::")
132
+ # vars = k.split("::") # get the namespace and the method names
133
+ # mod = vars.first # namespace
134
+ # e_meths << mod # generate a method for the namespace
135
+ # # create a new Namespace object for this name space and assign it to the final_configuration_settings hash.
136
+ # self.final_configuration_settings[mod.to_s] = Application::Configuration::Namespace.new(mod.to_s)
137
+ # # add an entry for the __ version of the key so our __ method can call it.
138
+ # self.final_configuration_settings[e_meths.first] = v
139
+ # end
140
+ # # generate all the necessary getter methods
141
+ # e_meths.each do |m|
142
+ # eval %{
143
+ # def #{m}
144
+ # self.final_configuration_settings["#{m}"]
145
+ # end
146
+ # }
147
+ # end
148
+ # end
149
+ @last_reload_time = Time.now
150
+ end
151
+
152
+ def dump_to_screen
153
+ y = self.final_configuration_settings.to_yaml
154
+ puts y
155
+ y
156
+ end
157
+
158
+ def dump_to_file(file_name = (self.is_rails ? "#{RAILS_ROOT}/config/application_configuration_dump.yml" : "application_configuration_dump.yml"))
159
+ File.open(file_name, "w") {|file| file.puts self.final_configuration_settings.to_yaml}
160
+ puts "Dumped configuration settings to: #{file_name}"
161
+ dump_to_screen
162
+ end
163
+
164
+ private
165
+ def load_from_file(file)
166
+ template = ERB.new(File.open(file).read)
167
+ YAML.load(template.result)
168
+ end
169
+
170
+ end
171
+
172
+ end
173
+
174
+ class Object
175
+
176
+ def app_config
177
+ Application::Configuration
178
+ end
179
+
180
+ end
@@ -0,0 +1,5 @@
1
+ --- !ruby/object:RubyForgeConfig
2
+ gem_name: application_configuration
3
+ package: application_configuration
4
+ project: magrathea
5
+ version: 1.0.1
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: application_configuration
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.1
7
+ date: 2007-09-17 00:00:00 -04:00
8
+ summary: application_configuration
9
+ require_paths:
10
+ - lib
11
+ - lib
12
+ - lib
13
+ - lib/tasks
14
+ email:
15
+ homepage:
16
+ rubyforge_project: magrathea
17
+ description: "application_configuration was developed by: markbates"
18
+ autorequire:
19
+ - application_configuration
20
+ - application_configuration
21
+ default_executable:
22
+ bindir: bin
23
+ has_rdoc: false
24
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
+ requirements:
26
+ - - ">"
27
+ - !ruby/object:Gem::Version
28
+ version: 0.0.0
29
+ version:
30
+ platform: ruby
31
+ signing_key:
32
+ cert_chain:
33
+ post_install_message:
34
+ authors:
35
+ - markbates
36
+ files:
37
+ - init.rb
38
+ - lib/application_configuration.rb
39
+ - lib/tasks/rubyforge_config.yml
40
+ test_files: []
41
+
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements: []
51
+
52
+ dependencies: []
53
+