drtom_rails_config 0.5.0.beta1
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/Appraisals +11 -0
- data/CHANGELOG.md +24 -0
- data/Gemfile +6 -0
- data/LICENSE.md +27 -0
- data/README.md +317 -0
- data/Rakefile +53 -0
- data/gemfiles/rails_3.gemfile +7 -0
- data/gemfiles/rails_4.1.gemfile +7 -0
- data/gemfiles/rails_4.gemfile +7 -0
- data/lib/generators/rails_config/install_generator.rb +32 -0
- data/lib/generators/rails_config/templates/rails_config.rb +3 -0
- data/lib/generators/rails_config/templates/settings.local.yml +0 -0
- data/lib/generators/rails_config/templates/settings.yml +0 -0
- data/lib/generators/rails_config/templates/settings/development.yml +0 -0
- data/lib/generators/rails_config/templates/settings/production.yml +0 -0
- data/lib/generators/rails_config/templates/settings/test.yml +0 -0
- data/lib/rails_config.rb +64 -0
- data/lib/rails_config/deep_merge.rb +32 -0
- data/lib/rails_config/engine.rb +5 -0
- data/lib/rails_config/integration/rails.rb +34 -0
- data/lib/rails_config/integration/sinatra.rb +26 -0
- data/lib/rails_config/options.rb +118 -0
- data/lib/rails_config/rack/reloader.rb +15 -0
- data/lib/rails_config/railtie.rb +9 -0
- data/lib/rails_config/sources/yaml_source.rb +26 -0
- data/lib/rails_config/tasks.rb +59 -0
- data/lib/rails_config/tasks/heroku.rake +8 -0
- data/lib/rails_config/version.rb +3 -0
- data/rails_config.gemspec +40 -0
- data/spec/app/rails_3/Rakefile +7 -0
- data/spec/app/rails_3/app/assets/javascripts/application.js +13 -0
- data/spec/app/rails_3/app/assets/stylesheets/application.css +13 -0
- data/spec/app/rails_3/app/controllers/application_controller.rb +3 -0
- data/spec/app/rails_3/app/helpers/application_helper.rb +2 -0
- data/spec/app/rails_3/app/models/.gitkeep +0 -0
- data/spec/app/rails_3/app/views/layouts/application.html.erb +14 -0
- data/spec/app/rails_3/bin/bundle +3 -0
- data/spec/app/rails_3/bin/rails +4 -0
- data/spec/app/rails_3/bin/rake +4 -0
- data/spec/app/rails_3/config.ru +4 -0
- data/spec/app/rails_3/config/application.rb +29 -0
- data/spec/app/rails_3/config/boot.rb +5 -0
- data/spec/app/rails_3/config/database.yml +25 -0
- data/spec/app/rails_3/config/environment.rb +5 -0
- data/spec/app/rails_3/config/environments/development.rb +42 -0
- data/spec/app/rails_3/config/environments/production.rb +72 -0
- data/spec/app/rails_3/config/environments/test.rb +42 -0
- data/spec/app/rails_3/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/app/rails_3/config/initializers/inflections.rb +15 -0
- data/spec/app/rails_3/config/initializers/mime_types.rb +5 -0
- data/spec/app/rails_3/config/initializers/secret_token.rb +7 -0
- data/spec/app/rails_3/config/initializers/session_store.rb +8 -0
- data/spec/app/rails_3/config/initializers/wrap_parameters.rb +14 -0
- data/spec/app/rails_3/config/locales/en.yml +5 -0
- data/spec/app/rails_3/config/routes.rb +2 -0
- data/spec/app/rails_3/config/settings.yml +7 -0
- data/spec/app/rails_3/db/.gitkeep +0 -0
- data/spec/app/rails_3/public/404.html +26 -0
- data/spec/app/rails_3/public/422.html +26 -0
- data/spec/app/rails_3/public/500.html +25 -0
- data/spec/app/rails_3/script/rails +6 -0
- data/spec/app/rails_4.1/Rakefile +6 -0
- data/spec/app/rails_4.1/app/assets/javascripts/application.js +13 -0
- data/spec/app/rails_4.1/app/assets/stylesheets/application.css +15 -0
- data/spec/app/rails_4.1/app/controllers/application_controller.rb +5 -0
- data/spec/app/rails_4.1/app/helpers/application_helper.rb +2 -0
- data/spec/app/rails_4.1/app/models/.keep +0 -0
- data/spec/app/rails_4.1/app/views/layouts/application.html.erb +14 -0
- data/spec/app/rails_4.1/bin/bundle +3 -0
- data/spec/app/rails_4.1/bin/rails +4 -0
- data/spec/app/rails_4.1/bin/rake +4 -0
- data/spec/app/rails_4.1/config.ru +4 -0
- data/spec/app/rails_4.1/config/application.rb +27 -0
- data/spec/app/rails_4.1/config/boot.rb +5 -0
- data/spec/app/rails_4.1/config/database.yml +25 -0
- data/spec/app/rails_4.1/config/environment.rb +5 -0
- data/spec/app/rails_4.1/config/environments/development.rb +42 -0
- data/spec/app/rails_4.1/config/environments/production.rb +88 -0
- data/spec/app/rails_4.1/config/environments/test.rb +44 -0
- data/spec/app/rails_4.1/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/app/rails_4.1/config/initializers/cookies_serializer.rb +3 -0
- data/spec/app/rails_4.1/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/app/rails_4.1/config/initializers/inflections.rb +16 -0
- data/spec/app/rails_4.1/config/initializers/mime_types.rb +4 -0
- data/spec/app/rails_4.1/config/initializers/session_store.rb +3 -0
- data/spec/app/rails_4.1/config/initializers/wrap_parameters.rb +14 -0
- data/spec/app/rails_4.1/config/locales/en.yml +23 -0
- data/spec/app/rails_4.1/config/routes.rb +2 -0
- data/spec/app/rails_4.1/config/secrets.yml +22 -0
- data/spec/app/rails_4.1/config/settings.yml +7 -0
- data/spec/app/rails_4.1/db/.keep +0 -0
- data/spec/app/rails_4.1/public/404.html +67 -0
- data/spec/app/rails_4.1/public/422.html +67 -0
- data/spec/app/rails_4.1/public/500.html +66 -0
- data/spec/app/rails_4/Rakefile +6 -0
- data/spec/app/rails_4/app/assets/javascripts/application.js +13 -0
- data/spec/app/rails_4/app/assets/stylesheets/application.css +13 -0
- data/spec/app/rails_4/app/controllers/application_controller.rb +5 -0
- data/spec/app/rails_4/app/helpers/application_helper.rb +2 -0
- data/spec/app/rails_4/app/models/.keep +0 -0
- data/spec/app/rails_4/app/views/layouts/application.html.erb +14 -0
- data/spec/app/rails_4/bin/bundle +3 -0
- data/spec/app/rails_4/bin/rails +4 -0
- data/spec/app/rails_4/bin/rake +4 -0
- data/spec/app/rails_4/config.ru +4 -0
- data/spec/app/rails_4/config/application.rb +27 -0
- data/spec/app/rails_4/config/boot.rb +5 -0
- data/spec/app/rails_4/config/database.yml +25 -0
- data/spec/app/rails_4/config/environment.rb +5 -0
- data/spec/app/rails_4/config/environments/development.rb +34 -0
- data/spec/app/rails_4/config/environments/production.rb +85 -0
- data/spec/app/rails_4/config/environments/test.rb +41 -0
- data/spec/app/rails_4/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/app/rails_4/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/app/rails_4/config/initializers/inflections.rb +16 -0
- data/spec/app/rails_4/config/initializers/mime_types.rb +5 -0
- data/spec/app/rails_4/config/initializers/secret_token.rb +12 -0
- data/spec/app/rails_4/config/initializers/session_store.rb +3 -0
- data/spec/app/rails_4/config/initializers/wrap_parameters.rb +14 -0
- data/spec/app/rails_4/config/locales/en.yml +23 -0
- data/spec/app/rails_4/config/routes.rb +2 -0
- data/spec/app/rails_4/config/settings.yml +7 -0
- data/spec/app/rails_4/db/.keep +0 -0
- data/spec/app/rails_4/public/404.html +58 -0
- data/spec/app/rails_4/public/422.html +58 -0
- data/spec/app/rails_4/public/500.html +57 -0
- data/spec/fixtures/bool_override/config1.yml +2 -0
- data/spec/fixtures/bool_override/config2.yml +2 -0
- data/spec/fixtures/custom_types/hash.yml +7 -0
- data/spec/fixtures/deep_merge/config1.yml +28 -0
- data/spec/fixtures/deep_merge/config2.yml +28 -0
- data/spec/fixtures/deep_merge2/config1.yml +3 -0
- data/spec/fixtures/deep_merge2/config2.yml +2 -0
- data/spec/fixtures/development.yml +4 -0
- data/spec/fixtures/empty1.yml +0 -0
- data/spec/fixtures/empty2.yml +0 -0
- data/spec/fixtures/env/settings.yml +4 -0
- data/spec/fixtures/malformed.yml +6 -0
- data/spec/fixtures/settings.yml +21 -0
- data/spec/fixtures/settings2.yml +1 -0
- data/spec/fixtures/with_erb.yml +4 -0
- data/spec/rails_config_helper.rb +22 -0
- data/spec/rails_config_spec.rb +311 -0
- data/spec/sources/yaml_source_spec.rb +77 -0
- data/spec/spec_helper.rb +47 -0
- data/spec/support/shared_contexts/rake.rb +8 -0
- data/spec/tasks/db_spec.rb +9 -0
- metadata +476 -0
@@ -0,0 +1,32 @@
|
|
1
|
+
module RailsConfig
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
desc "Generates a custom Rails Config initializer file."
|
5
|
+
|
6
|
+
def self.source_root
|
7
|
+
@_rails_config_source_root ||= File.expand_path("../templates", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
def copy_initializer
|
11
|
+
template "rails_config.rb", "config/initializers/rails_config.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_settings
|
15
|
+
template "settings.yml", "config/settings.yml"
|
16
|
+
template "settings.local.yml", "config/settings.local.yml"
|
17
|
+
directory "settings", "config/settings"
|
18
|
+
end
|
19
|
+
|
20
|
+
def modify_gitignore
|
21
|
+
create_file '.gitignore' unless File.exists? '.gitignore'
|
22
|
+
|
23
|
+
append_to_file '.gitignore' do
|
24
|
+
"\n" +
|
25
|
+
"config/settings.local.yml\n" +
|
26
|
+
"config/settings/*.local.yml\n" +
|
27
|
+
"config/environments/*.local.yml\n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/lib/rails_config.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
+
|
3
|
+
require 'rails_config/options'
|
4
|
+
require 'rails_config/version'
|
5
|
+
require 'rails_config/engine' if defined?(::Rails)
|
6
|
+
require 'rails_config/sources/yaml_source'
|
7
|
+
require 'rails_config/deep_merge' unless defined?(DeepMerge)
|
8
|
+
|
9
|
+
module RailsConfig
|
10
|
+
# ensures the setup only gets run once
|
11
|
+
@@_ran_once = false
|
12
|
+
|
13
|
+
mattr_accessor :const_name, :use_env
|
14
|
+
@@const_name = "Settings"
|
15
|
+
@@use_env = false
|
16
|
+
|
17
|
+
def self.setup
|
18
|
+
yield self if @@_ran_once == false
|
19
|
+
@@_ran_once = true
|
20
|
+
end
|
21
|
+
|
22
|
+
# Create a populated Options instance from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
|
23
|
+
# if the first file if they exist in the first file.
|
24
|
+
def self.load_files(*files)
|
25
|
+
config = Options.new
|
26
|
+
|
27
|
+
# add yaml sources
|
28
|
+
[files].flatten.compact.uniq.each do |file|
|
29
|
+
config.add_source!(file.to_s)
|
30
|
+
end
|
31
|
+
|
32
|
+
config.load!
|
33
|
+
config.load_env! if @@use_env
|
34
|
+
config
|
35
|
+
end
|
36
|
+
|
37
|
+
# Loads and sets the settings constant!
|
38
|
+
def self.load_and_set_settings(*files)
|
39
|
+
Kernel.send(:remove_const, RailsConfig.const_name) if Kernel.const_defined?(RailsConfig.const_name)
|
40
|
+
Kernel.const_set(RailsConfig.const_name, RailsConfig.load_files(files))
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.setting_files(config_root, env)
|
44
|
+
[
|
45
|
+
File.join(config_root, "settings.yml").to_s,
|
46
|
+
File.join(config_root, "settings", "#{env}.yml").to_s,
|
47
|
+
File.join(config_root, "environments", "#{env}.yml").to_s,
|
48
|
+
|
49
|
+
File.join(config_root, "settings.local.yml").to_s,
|
50
|
+
File.join(config_root, "settings", "#{env}.local.yml").to_s,
|
51
|
+
File.join(config_root, "environments", "#{env}.local.yml").to_s
|
52
|
+
].freeze
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.reload!
|
56
|
+
Kernel.const_get(RailsConfig.const_name).reload!
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# add rails integration
|
61
|
+
require('rails_config/integration/rails') if defined?(::Rails)
|
62
|
+
|
63
|
+
# add sinatra integration
|
64
|
+
require('rails_config/integration/sinatra') if defined?(::Sinatra)
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright (C) 2014, 2015 Dr. Thomas Schank (DrTom@schank.ch, Thomas.Schank@algocon.ch)
|
2
|
+
#
|
3
|
+
require 'set'
|
4
|
+
module DeepMerge
|
5
|
+
class << self
|
6
|
+
def deep_merge(obj1, obj2)
|
7
|
+
if obj1.is_a?(Hash) && obj2.is_a?(Hash)
|
8
|
+
deep_merge_hashes obj1, obj2
|
9
|
+
else
|
10
|
+
obj2
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def keys(h1, h2)
|
17
|
+
Set.new(h1.keys) + Set.new(h2.keys)
|
18
|
+
end
|
19
|
+
|
20
|
+
def deep_merge_hashes(h1, h2)
|
21
|
+
Hash[(keys(h1, h2)).map do |k|
|
22
|
+
[k, if h1.key?(k) && h2.key?(k)
|
23
|
+
deep_merge(h1[k], h2[k])
|
24
|
+
elsif h2.key?(k)
|
25
|
+
h2[k]
|
26
|
+
else
|
27
|
+
h1[k]
|
28
|
+
end]
|
29
|
+
end]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module RailsConfig
|
2
|
+
module Integration
|
3
|
+
module Rails
|
4
|
+
if defined?(::Rails::Railtie)
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
# Load rake tasks (eg. Heroku)
|
7
|
+
rake_tasks do
|
8
|
+
Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveSupport.on_load :before_configuration, :yield => true do
|
12
|
+
# Manually load the custom initializer before everything else
|
13
|
+
initializer = ::Rails.root.join("config", "initializers", "rails_config.rb")
|
14
|
+
require initializer if File.exist?(initializer)
|
15
|
+
|
16
|
+
# Parse the settings before any of the initializers
|
17
|
+
RailsConfig.load_and_set_settings(
|
18
|
+
RailsConfig.setting_files(::Rails.root.join("config"), ::Rails.env)
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Rails Dev environment should reload the Settings on every request
|
23
|
+
if ::Rails.env.development?
|
24
|
+
initializer :rails_config_reload_on_development do
|
25
|
+
ActionController::Base.class_eval do
|
26
|
+
prepend_before_filter { ::RailsConfig.reload! }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "rails_config/rack/reloader"
|
2
|
+
|
3
|
+
module RailsConfig
|
4
|
+
# provide helper to register within your Sinatra app
|
5
|
+
#
|
6
|
+
# set :root, File.dirname(__FILE__)
|
7
|
+
# register RailsConfig
|
8
|
+
#
|
9
|
+
def self.registered(app)
|
10
|
+
app.configure do |inner_app|
|
11
|
+
|
12
|
+
env = inner_app.environment || ENV["RACK_ENV"]
|
13
|
+
root = inner_app.root
|
14
|
+
|
15
|
+
# use Padrino settings if applicable
|
16
|
+
if defined?(Padrino)
|
17
|
+
env = Padrino.env
|
18
|
+
root = Padrino.root
|
19
|
+
end
|
20
|
+
|
21
|
+
RailsConfig.load_and_set_settings(RailsConfig.setting_files(File.join(root, 'config'), env))
|
22
|
+
|
23
|
+
inner_app.use(::RailsConfig::Rack::Reloader) if inner_app.development?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
module RailsConfig
|
3
|
+
|
4
|
+
class Options < OpenStruct
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def keys() marshal_dump.keys; end
|
8
|
+
def empty?() marshal_dump.empty?; end
|
9
|
+
|
10
|
+
def add_source!(source)
|
11
|
+
# handle yaml file paths
|
12
|
+
source = (Sources::YAMLSource.new(source)) if source.is_a?(String)
|
13
|
+
|
14
|
+
@config_sources ||= []
|
15
|
+
@config_sources << source
|
16
|
+
end
|
17
|
+
|
18
|
+
def reload_env!
|
19
|
+
return self if ENV.nil? || ENV.empty?
|
20
|
+
conf = Hash.new
|
21
|
+
ENV.each do |key, value|
|
22
|
+
next unless key.to_s.index(RailsConfig.const_name) == 0
|
23
|
+
hash = value
|
24
|
+
key.to_s.split('.').reverse.each do |element|
|
25
|
+
hash = {element => hash}
|
26
|
+
end
|
27
|
+
conf= DeepMerge.deep_merge(conf,hash)
|
28
|
+
end
|
29
|
+
|
30
|
+
merge!(conf[RailsConfig.const_name] || {})
|
31
|
+
end
|
32
|
+
|
33
|
+
alias :load_env! :reload_env!
|
34
|
+
|
35
|
+
# look through all our sources and rebuild the configuration
|
36
|
+
def reload!
|
37
|
+
conf = {}
|
38
|
+
@config_sources.each do |source|
|
39
|
+
source_conf = source.load
|
40
|
+
|
41
|
+
if conf.empty?
|
42
|
+
conf = source_conf
|
43
|
+
else
|
44
|
+
conf= DeepMerge.deep_merge(conf, source_conf)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# swap out the contents of the OStruct with a hash (need to recursively convert)
|
49
|
+
marshal_load(__convert(conf).marshal_dump)
|
50
|
+
|
51
|
+
reload_env! if RailsConfig.use_env
|
52
|
+
|
53
|
+
return self
|
54
|
+
end
|
55
|
+
|
56
|
+
alias :load! :reload!
|
57
|
+
|
58
|
+
def reload_from_files(*files)
|
59
|
+
RailsConfig.load_and_set_settings(files)
|
60
|
+
reload!
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_hash
|
64
|
+
result = {}
|
65
|
+
marshal_dump.each do |k, v|
|
66
|
+
result[k] = v.instance_of?(RailsConfig::Options) ? v.to_hash : v
|
67
|
+
end
|
68
|
+
result
|
69
|
+
end
|
70
|
+
|
71
|
+
def each(*args, &block)
|
72
|
+
marshal_dump.each(*args, &block)
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_json(*args)
|
76
|
+
require "json" unless defined?(JSON)
|
77
|
+
to_hash.to_json(*args)
|
78
|
+
end
|
79
|
+
|
80
|
+
def merge!(hash)
|
81
|
+
current = to_hash
|
82
|
+
current = DeepMerge.deep_merge(current,hash)
|
83
|
+
marshal_load(__convert(current).marshal_dump)
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
# An alternative mechanism for property access.
|
88
|
+
# This let's you do foo['bar'] along with foo.bar.
|
89
|
+
def [](param)
|
90
|
+
send("#{param}")
|
91
|
+
end
|
92
|
+
|
93
|
+
def []=(param, value)
|
94
|
+
send("#{param}=", value)
|
95
|
+
end
|
96
|
+
|
97
|
+
protected
|
98
|
+
|
99
|
+
# Recursively converts Hashes to Options (including Hashes inside Arrays)
|
100
|
+
def __convert(h) #:nodoc:
|
101
|
+
s = self.class.new
|
102
|
+
|
103
|
+
h.each do |k, v|
|
104
|
+
k = k.to_s if !k.respond_to?(:to_sym) && k.respond_to?(:to_s)
|
105
|
+
s.new_ostruct_member(k)
|
106
|
+
|
107
|
+
if v.is_a?(Hash)
|
108
|
+
v = v["type"] == "hash" ? v["contents"] : __convert(v)
|
109
|
+
elsif v.is_a?(Array)
|
110
|
+
v = v.collect { |e| e.instance_of?(Hash) ? __convert(e) : e }
|
111
|
+
end
|
112
|
+
|
113
|
+
s.send("#{k}=".to_sym, v)
|
114
|
+
end
|
115
|
+
s
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module RailsConfig
|
2
|
+
module Rack
|
3
|
+
# Rack middleware the reloads RailsConfig on every request (only use in dev mode)
|
4
|
+
class Reloader
|
5
|
+
def initialize(app)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
RailsConfig.reload!
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
module RailsConfig
|
5
|
+
module Sources
|
6
|
+
class YAMLSource
|
7
|
+
attr_accessor :path
|
8
|
+
|
9
|
+
def initialize(path)
|
10
|
+
@path = path
|
11
|
+
end
|
12
|
+
|
13
|
+
# returns a config hash from the YML file
|
14
|
+
def load
|
15
|
+
if @path and File.exist?(@path.to_s)
|
16
|
+
result = YAML.load(ERB.new(IO.read(@path.to_s)).result)
|
17
|
+
end
|
18
|
+
result || {}
|
19
|
+
rescue Psych::SyntaxError => e
|
20
|
+
raise "YAML syntax error occurred while parsing #{@path}. " \
|
21
|
+
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
|
22
|
+
"Error: #{e.message}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "bundler"
|
2
|
+
|
3
|
+
module RailsConfig
|
4
|
+
module Tasks
|
5
|
+
class Heroku < Struct.new(:app)
|
6
|
+
def invoke
|
7
|
+
puts 'Setting vars...'
|
8
|
+
heroku_command = "config:set #{vars}"
|
9
|
+
heroku(heroku_command)
|
10
|
+
puts 'Vars set:'
|
11
|
+
puts heroku_command
|
12
|
+
end
|
13
|
+
|
14
|
+
def vars
|
15
|
+
# Load only local options to Heroku
|
16
|
+
RailsConfig.load_and_set_settings(
|
17
|
+
Rails.root.join("config", "settings.local.yml").to_s,
|
18
|
+
Rails.root.join("config", "settings", "#{environment}.local.yml").to_s,
|
19
|
+
Rails.root.join("config", "environments", "#{environment}.local.yml").to_s
|
20
|
+
)
|
21
|
+
|
22
|
+
out = ''
|
23
|
+
dotted_hash = to_dotted_hash Kernel.const_get(RailsConfig.const_name).to_hash, {}, RailsConfig.const_name
|
24
|
+
dotted_hash.each {|key, value| out += " #{key}=#{value} "}
|
25
|
+
out
|
26
|
+
end
|
27
|
+
|
28
|
+
def environment
|
29
|
+
heroku("run 'echo $RAILS_ENV'").chomp[/(\w+)\z/]
|
30
|
+
end
|
31
|
+
|
32
|
+
def heroku(command)
|
33
|
+
with_app = app ? " --app #{app}" : ""
|
34
|
+
`heroku #{command}#{with_app}`
|
35
|
+
end
|
36
|
+
|
37
|
+
def `(command)
|
38
|
+
Bundler.with_clean_env { super }
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_dotted_hash(source, target = {}, namespace = nil)
|
42
|
+
prefix = "#{namespace}." if namespace
|
43
|
+
case source
|
44
|
+
when Hash
|
45
|
+
source.each do |key, value|
|
46
|
+
to_dotted_hash(value, target, "#{prefix}#{key}")
|
47
|
+
end
|
48
|
+
when Array
|
49
|
+
source.each_with_index do |value, index|
|
50
|
+
to_dotted_hash(value, target, "#{prefix}#{index}")
|
51
|
+
end
|
52
|
+
else
|
53
|
+
target[namespace] = source
|
54
|
+
end
|
55
|
+
target
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|