rails-config 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/rails_config.rb +9 -0
- data/lib/rails_config/environment.rb +55 -0
- data/lib/rails_config/ext.rb +3 -0
- data/lib/rails_config/ext/hash_ext.rb +42 -0
- data/lib/rails_config/ext/ostruct_ext.rb +13 -0
- data/lib/rails_config/ext/recursive_ostruct.rb +65 -0
- data/lib/rails_config/ext/string_ext.rb +7 -0
- data/lib/rails_config/settings.rb +65 -0
- data/lib/rails_config/version.rb +3 -0
- data/rails-config.gemspec +25 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 35bd54f3c4d709f16d86de364b23f72e707e4129
|
4
|
+
data.tar.gz: 98e1b6c091669e96340d67f89fd2e7617e192df6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e1d9c4592c76d6a14ba242ac1386eeb8796fdfe8e763017291d01f837032e53d7d92cc0d768da9a97d273f86a994d8024027e0fcbaa615da90e4ed1ac4f89e8
|
7
|
+
data.tar.gz: d267f663430871cb09778b3bbd5d175838da9e5a437135cf187d8e596457125207262e3881b4d7f3bcadc79caa0d35cbcef31d17f4631bfb34ac872859917eee
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Marc Lennox
|
2
|
+
|
3
|
+
MIT License
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# RailsConfig
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rails-config'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rails-config
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/rails_config.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'rails_config/ext'
|
3
|
+
|
4
|
+
module RailsConfig
|
5
|
+
class Environment
|
6
|
+
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
def self.load(options={})
|
10
|
+
|
11
|
+
# sorry, you must supply the source_dir
|
12
|
+
raise "options must contain :source_dir" unless options[:source_dir]
|
13
|
+
|
14
|
+
opts={
|
15
|
+
log_debug: false,
|
16
|
+
#source_dir: File.expand_path("..", __FILE__),
|
17
|
+
keys: ['application', 'development'],
|
18
|
+
output_file: nil
|
19
|
+
}.merge(options)
|
20
|
+
|
21
|
+
# Load various environment files
|
22
|
+
puts "[#{self.name}] Loading application environment from files in #{opts[:source_dir]}" if opts[:log_debug]
|
23
|
+
opts[:keys].each do |key|
|
24
|
+
env_file=File.join(opts[:source_dir], "#{key}.env")
|
25
|
+
if File.exist?(env_file)
|
26
|
+
puts "[#{self.name}] Loading environment file #{env_file}" if opts[:log_debug]
|
27
|
+
File.new(env_file).each do |line|
|
28
|
+
if (m=/^\s*([A-Za-z0-9_-]+)=(.+)\s*$/.match(line))
|
29
|
+
ENV[m[1]]=m[2]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Write out the loaded environment if an output file is specified
|
36
|
+
if opts[:output_file]
|
37
|
+
env_string=ENV.to_hash.map { |k, v| "#{k}=#{v}\n" }.join
|
38
|
+
begin
|
39
|
+
File.open(opts[:output_file], 'w') do |f|
|
40
|
+
puts "[#{self.name}] Writing application environment to #{f.path}" if opts[:log_debug]
|
41
|
+
f.write(env_string)
|
42
|
+
f.close
|
43
|
+
end
|
44
|
+
rescue Exception => ex
|
45
|
+
puts ex.message
|
46
|
+
puts ex.backtrace.join("\n")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
ENV.to_hash
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
def deep_merge(other_hash)
|
4
|
+
dup.deep_merge!(other_hash)
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
def deep_merge!(other_hash)
|
9
|
+
other_hash.each_pair do |k, v|
|
10
|
+
tv = self[k]
|
11
|
+
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
|
12
|
+
end
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def deep_stringify_keys!
|
17
|
+
self.keys.each do |k|
|
18
|
+
v=self[k]
|
19
|
+
self[k.to_s] = self.delete(k)
|
20
|
+
v.deep_stringify_keys! if v.is_a?(Hash)
|
21
|
+
end
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def deep_stringify_keys
|
26
|
+
dup.deep_stringify_keys!
|
27
|
+
end
|
28
|
+
|
29
|
+
def deep_symobolize_keys!
|
30
|
+
self.keys.each do |k|
|
31
|
+
v=self[k]
|
32
|
+
self[k.to_sym] = self.delete(k)
|
33
|
+
v.deep_symobolize_keys! if v.is_a?(Hash)
|
34
|
+
end
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def deep_symobolize_keys
|
39
|
+
dup.deep_symobolize_keys!
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
class RecursiveOpenStruct < OpenStruct
|
4
|
+
|
5
|
+
def new_ostruct_member(name)
|
6
|
+
name = name.to_sym
|
7
|
+
unless self.respond_to?(name)
|
8
|
+
class << self;
|
9
|
+
self;
|
10
|
+
end.class_eval do
|
11
|
+
define_method(name) {
|
12
|
+
v = @table[name]
|
13
|
+
v.is_a?(Hash) ? RecursiveOpenStruct.new(v) : v
|
14
|
+
}
|
15
|
+
define_method("#{name}=") { |x| modifiable[name] = x }
|
16
|
+
define_method("#{name}_as_hash") { @table[name].with_indifferent_access }
|
17
|
+
define_method("#{name}_as_array") { @table[name].to_a }
|
18
|
+
define_method("as_hash") { @table.with_indifferent_access }
|
19
|
+
define_method("as_array") { @table.to_a }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
name
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_by_namespace(namespace)
|
26
|
+
result=self
|
27
|
+
namespace.split(':').map(&:to_sym).each do |name|
|
28
|
+
result=result.send(:[], name)
|
29
|
+
end
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
def debug_inspect(indent_level = 0, recursion_limit = 12)
|
34
|
+
display_recursive_open_struct(@table, indent_level, recursion_limit)
|
35
|
+
end
|
36
|
+
|
37
|
+
def display_recursive_open_struct(ostrct_or_hash, indent_level, recursion_limit)
|
38
|
+
if recursion_limit <= 0 then
|
39
|
+
# protection against recursive structure (like in the tests)
|
40
|
+
puts ' '*indent_level + '(recursion limit reached)'
|
41
|
+
else
|
42
|
+
#puts ostrct_or_hash.inspect
|
43
|
+
if ostrct_or_hash.is_a?(RecursiveOpenStruct) then
|
44
|
+
ostrct_or_hash = ostrct_or_hash.marshal_dump
|
45
|
+
end
|
46
|
+
# We'll display the key values like this : key = value
|
47
|
+
# to align display, we look for the maximum key length of the data that will be displayed
|
48
|
+
# (everything except hashes)
|
49
|
+
data_indent = ostrct_or_hash.reject { |k, v| v.is_a?(RecursiveOpenStruct) || v.is_a?(Hash) }.max { |a, b| a[0].to_s.length <=> b[0].to_s.length }[0].length
|
50
|
+
# puts "max length = #{data_indent}"
|
51
|
+
|
52
|
+
ostrct_or_hash.each do |key, value|
|
53
|
+
if (value.is_a?(RecursiveOpenStruct) || value.is_a?(Hash)) then
|
54
|
+
puts ' '*indent_level + key.to_s + '.'
|
55
|
+
display_recursive_open_struct(value, indent_level + 1, recursion_limit - 1)
|
56
|
+
else
|
57
|
+
puts ' '*indent_level + key.to_s + ' '*(data_indent - key.to_s.length) + ' = ' + value.inspect
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
true
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'yaml'
|
3
|
+
require 'rails_config/ext'
|
4
|
+
|
5
|
+
module RailsConfig
|
6
|
+
class Settings
|
7
|
+
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
def self.load(options={})
|
11
|
+
|
12
|
+
# sorry, you must supply the source_dir
|
13
|
+
raise "options must contain :source_dir" unless options[:source_dir]
|
14
|
+
|
15
|
+
opts={
|
16
|
+
log_debug: false,
|
17
|
+
#source_dir: File.expand_path("..", __FILE__),
|
18
|
+
environment: 'development',
|
19
|
+
output_file: nil
|
20
|
+
}.merge(options)
|
21
|
+
|
22
|
+
# Load various settings files
|
23
|
+
puts "[#{self.name}] Loading application settings from files in #{opts[:source_dir]}" if opts[:log_debug]
|
24
|
+
setting_files=Dir.glob(File.join(opts[:source_dir], '*.yml')).map { |p| File.new(p) }.inject({}) { |h, f| h[File.basename(f).downcase.split(/(.*)[.]yml/).join]=f; h }
|
25
|
+
settings=Hash.new
|
26
|
+
setting_files.each_pair do |key, file|
|
27
|
+
puts "[#{self.name}] Loading #{key} settings from file #{file.path}" if opts[:log_debug]
|
28
|
+
settings[key]=YAML.load_file(file)[opts[:environment]]
|
29
|
+
if (env=YAML.load_file(file)['env'])
|
30
|
+
env.keys.each do |k|
|
31
|
+
if (env_var=ENV[k.to_s.upcase])
|
32
|
+
value=env[k]['with'] || :to_s
|
33
|
+
value=[value] unless value.is_a?(Array)
|
34
|
+
value=value.inject(env_var) { |v, m| eval("v.#{m}") }
|
35
|
+
value_hash=(env[k]['to'] || k.downcase.slice("#{File.basename(file)}_")).split('/').reverse.reduce(value) { |r, e| {e => r} }
|
36
|
+
settings[key].deep_merge!(value_hash)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Write out the loaded settings if an output file is specified
|
43
|
+
if opts[:output_file]
|
44
|
+
hash=settings.deep_stringify_keys
|
45
|
+
method=hash.respond_to?(:ya2yaml) ? :ya2yaml : :to_yaml
|
46
|
+
yaml_string=hash.deep_stringify_keys.send(method)
|
47
|
+
yaml_string.gsub("!ruby/symbol ", ":").sub("---", "").split("\n").map(&:rstrip).join("\n").strip
|
48
|
+
begin
|
49
|
+
File.open(opts[:output_file], 'w') do |f|
|
50
|
+
puts "[#{self.name}] Writing application settings to #{f.path}" if opts[:log_debug]
|
51
|
+
f.write(yaml_string)
|
52
|
+
f.close
|
53
|
+
end
|
54
|
+
rescue Exception => ex
|
55
|
+
puts ex.message
|
56
|
+
puts ex.backtrace.join("\n")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
RecursiveOpenStruct.new(settings)
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_config/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails-config"
|
8
|
+
spec.version = RailsConfig::VERSION
|
9
|
+
spec.authors = ["Marc Lennox"]
|
10
|
+
spec.email = ["marc.lennox@gmail.com"]
|
11
|
+
spec.description = %q{Rails config utility that allows for YAML settings with environment override}
|
12
|
+
spec.summary = %q{Rails config utility that allows for YAML settings with environment override}
|
13
|
+
spec.homepage = "http://github.com/marclennox/rails-config"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "debugger"
|
24
|
+
spec.add_development_dependency "interactive_editor"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marc Lennox
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: debugger
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: interactive_editor
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Rails config utility that allows for YAML settings with environment override
|
70
|
+
email:
|
71
|
+
- marc.lennox@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/rails_config.rb
|
82
|
+
- lib/rails_config/environment.rb
|
83
|
+
- lib/rails_config/ext.rb
|
84
|
+
- lib/rails_config/ext/hash_ext.rb
|
85
|
+
- lib/rails_config/ext/ostruct_ext.rb
|
86
|
+
- lib/rails_config/ext/recursive_ostruct.rb
|
87
|
+
- lib/rails_config/ext/string_ext.rb
|
88
|
+
- lib/rails_config/settings.rb
|
89
|
+
- lib/rails_config/version.rb
|
90
|
+
- rails-config.gemspec
|
91
|
+
homepage: http://github.com/marclennox/rails-config
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.1.9
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Rails config utility that allows for YAML settings with environment override
|
115
|
+
test_files: []
|