master_config 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,295 @@
1
+ require "hashie"
2
+ require "json"
3
+
4
+ class MasterConfig
5
+ attr_accessor :config_path
6
+
7
+ def initialize(config,paths,app_name,&block)
8
+ @config_path = nil
9
+
10
+ # -- Load in the config values from the block -- #
11
+
12
+ @app_config = Hashie::Mash.new
13
+ @default_config = Hashie::Mash.new
14
+ yield @app_config, @default_config
15
+
16
+ @app_config.root ||= Rails::VERSION::MAJOR == 2 ? RAILS_ROOT : Rails.root
17
+
18
+ @jconfig = MasterConfig.read_json_config(:root => @app_config.root)
19
+
20
+ @jconfig = @default_config.merge!(@jconfig)
21
+
22
+ @config_path = @jconfig._config_path
23
+
24
+ # -- Update environment_path to use base_env -- #
25
+
26
+ if Rails::VERSION::MAJOR == 2
27
+ config.instance_variable_set( :@_base_env, @jconfig[:base_env] )
28
+ class << config
29
+ def environment_path
30
+ "#{root_path}/config/environments/#{@_base_env}.rb"
31
+ end
32
+ end
33
+ else
34
+ paths["config/environments"] = ["config/environments/#{@jconfig[:base_env]}.rb"]
35
+ end
36
+
37
+ # -- load the test gems if we're testing -- #
38
+
39
+ if @jconfig[:env] == 'test'
40
+ Bundler.require(:test)
41
+ end
42
+
43
+ # -- Load in session secret -- #
44
+
45
+ self._set_and_define config, :secret_token
46
+
47
+
48
+ # -- Load in application settings -- #
49
+ (@jconfig[app_name.to_sym]||{}).each do |k,v|
50
+ @app_config.send("#{k}=",v)
51
+ end
52
+
53
+ # Load in ActionMailer settings
54
+ config.action_mailer.default_url_options = {
55
+ :host => @jconfig[:host],
56
+ :protocol => @jconfig[:ssl] ? "https" : "http"
57
+ }
58
+
59
+ # Load in database settings
60
+ if @jconfig.database?
61
+ self._define_method config, "database_configuration", { @jconfig.env => @jconfig.database }
62
+ else
63
+ self._define_method config, "database_configuration", { @jconfig.env => nil }
64
+ end
65
+
66
+ # -- Configure New Relic -- #
67
+
68
+ self._set_and_define config, :new_relic do |val|
69
+ unless val.present?
70
+ val = Hashie::Mash.new
71
+ val.agent_enabled = false
72
+ val.monitor_mode = false
73
+ end
74
+ val
75
+ end
76
+
77
+ # -- Configure Resque Pool -- #
78
+
79
+ self._set_and_define config, :resque_pool
80
+
81
+ # -- Configure Data Path -- #
82
+
83
+ self._set_and_define config, :data_path
84
+
85
+ # -- Configure Redis Host -- #
86
+
87
+ self._set_and_define config, :redis_host
88
+
89
+ # -- Configure S3 Bucket -- #
90
+
91
+ self._set_and_define config, :s3_bucket
92
+
93
+ # Change paths for caching
94
+ config.cache_store = [ :file_store, File.join(@app_config.root,"tmp","cache") ]
95
+
96
+ if Rails::VERSION::MAJOR == 2
97
+ # -- Set our logging path -- #
98
+
99
+ config.log_path = File.join(@app_config.root,"log","#{@jconfig[:env]}.log")
100
+
101
+ else
102
+ config.assets.cache_store = [ :file_store, File.join(@app_config.root,"tmp","cache","assets") ]
103
+
104
+ if paths
105
+ # set our log path
106
+ paths["log"] = File.join(@app_config.root,"log","#{Rails.env}.log")
107
+ paths["tmp"] = File.join(@app_config.root,"tmp")
108
+ end
109
+
110
+ end
111
+
112
+ # -- Configure ActionMailer -- #
113
+
114
+ config.action_mailer.default_url_options = {
115
+ :host => @jconfig[:host],
116
+ :protocol => @jconfig[:ssl] ? "https" : "http"
117
+ }
118
+
119
+ # -- Return! -- #
120
+
121
+ self._define_method config, app_name, @app_config
122
+ self._define_method config, "master_config", self
123
+
124
+ end
125
+
126
+ #----------
127
+
128
+ def init_new_relic
129
+ if @new_relic && @new_relic.agent_enabled
130
+ Rails.logger.debug "Loading in JSON New Relic config"
131
+
132
+ # load in the defaults
133
+ defaults = Hashie::Mash.new(
134
+ YAML.load( File.read(File.expand_path("config/newrelic.yml",@app_config.root)) )
135
+ ).common
136
+
137
+ @new_relic_config = defaults.merge(@new_relic)
138
+ Rails.logger.info "New Relic config is #{@new_relic_config}"
139
+
140
+ # We create a new source class here so that it won't get blown away if
141
+ # NewRelic tries to create a different ManualSource (in its resque code,
142
+ # for instance)
143
+ mcpsource = Class.new(NewRelic::Agent::Configuration::ManualSource)
144
+ NewRelic::Agent.config.replace_or_add_config(mcpsource.new(@new_relic_config), 1)
145
+ NewRelic::Agent.manual_start()
146
+ else
147
+ # just let it do its thing and not be enabled
148
+ NewRelic::Control.instance.init_plugin :config => Rails.configuration
149
+ end
150
+ end
151
+
152
+ #----------
153
+
154
+ def env_defaults(&block)
155
+ env_config = ActiveSupport::OrderedOptions.new
156
+
157
+ yield env_config
158
+
159
+ env_config.each do |k,v|
160
+ if !@app_config.send(k)
161
+ @app_config.send("#{k}=",v)
162
+ end
163
+ end
164
+
165
+ true
166
+ end
167
+
168
+ #----------
169
+
170
+ def self.read_json_config(opts=nil)
171
+ # -- Try to find a config file -- #
172
+
173
+ config_json_path = nil
174
+
175
+ env = nil
176
+ root = nil
177
+ has_rails = false
178
+ has_rack = false
179
+
180
+ if defined?(Rails)
181
+ has_rails = true
182
+
183
+ if Rails::VERSION::MAJOR == 2
184
+ env = RAILS_ENV
185
+ else
186
+ env = Rails.env
187
+ end
188
+ puts "Set env from RAILS #{ env }"
189
+
190
+ root = Rails.root
191
+
192
+ elsif defined?(Rack)
193
+ has_rack = true
194
+ env = ENV["RACK_ENV"] || "development"
195
+ puts "Set env from RACK #{ env }"
196
+ else
197
+ puts "No Rails or Rack"
198
+ end
199
+
200
+ if opts && opts[:root]
201
+ root = opts[:root]
202
+ end
203
+
204
+ if ENV["MC_ENV_JSON"]
205
+ config_json_path = ENV["MC_ENV_JSON"]
206
+ elsif env =~ /\//
207
+ config_json_path = File.expand_path(env)
208
+ elsif env && root
209
+ config_json_path = File.expand_path("#{root}/config/#{env}.json")
210
+ else
211
+ config_json_path = File.expand_path("./config/#{env}.json")
212
+ end
213
+
214
+ if File.exist? config_json_path
215
+ jconfig_raw = nil
216
+ File.open(config_json_path) { |f| jconfig_raw = f.read() }
217
+ config_json = Hashie::Mash.new(JSON.parse jconfig_raw)
218
+
219
+ if config_json
220
+ ENV["MC_ENV_JSON"] = config_json_path
221
+
222
+ if has_rails
223
+ ENV["RAILS_ENV"] = config_json.env
224
+
225
+ if Rails::VERSION::MAJOR == 2
226
+
227
+ # -- set our environment key -- #
228
+
229
+ if defined? RAILS_ENV
230
+ RAILS_ENV.replace ENV["RAILS_ENV"]
231
+ end
232
+ else
233
+ Rails.env = ENV["RAILS_ENV"]
234
+ end
235
+ elsif has_rack
236
+ ENV["RACK_ENV"] = config_json.env
237
+ end
238
+
239
+ # Stash the config file path in our results
240
+ config_json._config_path = config_json_path
241
+
242
+ # -- Return config -- #
243
+
244
+ return config_json
245
+ else
246
+ raise "Unable to find JSON config file."
247
+ end
248
+ end
249
+ end
250
+
251
+
252
+ protected
253
+ def _define_method(config,name,value)
254
+ if config.respond_to?(:define_singleton_method)
255
+ config.instance_variable_set("@_#{name}".to_sym, value)
256
+ config.define_singleton_method name do
257
+ instance_variable_get("@_#{name}")
258
+ end
259
+ else
260
+ config.instance_variable_set("@_#{name}".to_sym, value)
261
+ meta_class = class << config; self end
262
+ meta_class.send(:define_method,name) do
263
+ instance_variable_get("@_#{name}")
264
+ end
265
+ end
266
+ end
267
+
268
+ def _set_and_define(config, key)
269
+ unless @jconfig.has_key?(key)
270
+ @jconfig[key] = nil
271
+ end
272
+
273
+ if @jconfig[key].respond_to?(:each)
274
+ value = Hashie::Mash.new
275
+ @jconfig[key].each do |k,v|
276
+ value[k] = v
277
+ end
278
+ else
279
+ value = @jconfig[key]
280
+ end
281
+
282
+ if block_given?
283
+ value = yield(value)
284
+ end
285
+
286
+ self.instance_variable_set("@#{key}", value)
287
+
288
+ if config.respond_to?("#{key}=")
289
+ config.send("#{key}=", value)
290
+ else
291
+ self._define_method config, key.to_s, value
292
+ end
293
+ end
294
+
295
+ end
@@ -0,0 +1,3 @@
1
+ class MasterConfig
2
+ VERSION = "0.3.3"
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: master_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Richardson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Configure Rails apps via a single JSON file
47
+ email: erichardson@emcien.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/master_config/version.rb
53
+ - lib/master_config.rb
54
+ homepage: https://github.com/emcien/master_config/
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.23
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Configure Rails apps via a single JSON file
78
+ test_files: []