yamalicious 0.0.1.alpha
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +96 -0
- data/Rakefile +2 -0
- data/lib/yamalicious.rb +34 -0
- data/lib/yamalicious/version.rb +3 -0
- data/yamalicious.gemspec +20 -0
- metadata +86 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 LocalJobVentures
|
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,96 @@
|
|
1
|
+
# Yamalicious
|
2
|
+
|
3
|
+
The purpose of this gem is to load configuration information from YAML files and environment variables into a globally available
|
4
|
+
nested hash (actually a Hashie::Mash rather than a hash).
|
5
|
+
|
6
|
+
## Goals:
|
7
|
+
|
8
|
+
1. Store configuration info in YAML, since it's easy to read and change
|
9
|
+
2. Allow common settings to be shared among all environments
|
10
|
+
3. Allow each environment to override the common settings.
|
11
|
+
4. Allow the YAML to be loaded from two separate files (public and private), so that
|
12
|
+
the developer can store some settings in source control and others outside of source control
|
13
|
+
5. Allow environment variables to be used instead of YAML files (for Heroku, for example).
|
14
|
+
|
15
|
+
## Usage:
|
16
|
+
|
17
|
+
Install the gem and setup an intializer.
|
18
|
+
|
19
|
+
### Gem
|
20
|
+
|
21
|
+
In your gemfile,
|
22
|
+
|
23
|
+
# Gemfile
|
24
|
+
gem 'yamalicious'
|
25
|
+
|
26
|
+
### Initializer
|
27
|
+
|
28
|
+
In an initializer,
|
29
|
+
|
30
|
+
# config/initializers/_load_config.rb
|
31
|
+
APP_CONFIG = Yamalicious.load_config
|
32
|
+
|
33
|
+
This will load YAML from `yamalicious.yml`, `yamalicious.local.yml`, and from an environment variable named `yamalicious`.
|
34
|
+
|
35
|
+
### Configuration
|
36
|
+
|
37
|
+
If you wish to adjust which files are loaded and what environment variable is used, just pass a hash containing `:file_prefix` to `load_config` like this:
|
38
|
+
|
39
|
+
# config/initializers/_load_config.rb
|
40
|
+
APP_CONFIG = Yamalicious.load_config(:file_prefix => "settings")
|
41
|
+
|
42
|
+
This will load YAML from `settings.yml`, `settings.local.yml`, and from an environment variable named `settings`
|
43
|
+
|
44
|
+
### Avoiding global constant
|
45
|
+
|
46
|
+
If you don't like using a global constant like `APP_CONFIG`, you can use Rails' built in configuration object like this:
|
47
|
+
|
48
|
+
# config/initializers/_load_config.rb
|
49
|
+
Rails.configuration.yamalicious = Yamalicious.load_config
|
50
|
+
|
51
|
+
### YAML files
|
52
|
+
|
53
|
+
The YAML should contain a section for each environment as well as a section named default, like this:
|
54
|
+
|
55
|
+
# yamalicious.yml
|
56
|
+
default:
|
57
|
+
api_key: ASDGLKASDG
|
58
|
+
|
59
|
+
development:
|
60
|
+
api_key: HJASDOSHS
|
61
|
+
|
62
|
+
production:
|
63
|
+
api_key: ALSJHDGYD
|
64
|
+
|
65
|
+
The YAML is loaded into a nested hash using `YAML.load`
|
66
|
+
|
67
|
+
The environment-specific section of the YAML is deep merged into the default section.
|
68
|
+
|
69
|
+
### Environment variables
|
70
|
+
|
71
|
+
The environment variables should be strict base64 encoded YAML, in the same format as the YAML files, with a default section and a section for each relevant environment.
|
72
|
+
|
73
|
+
### Deep merging
|
74
|
+
|
75
|
+
The YAML from the two files and the environment variable are deep merged together in the following order of precedence:
|
76
|
+
|
77
|
+
1. `yamalicious` environment variable
|
78
|
+
2. `yamalicious.local.yml`
|
79
|
+
3. `yamalicious.yml`
|
80
|
+
|
81
|
+
### Result
|
82
|
+
|
83
|
+
The result of the `load_config` method is a Hashie::Mash, which you can treat as a hash that is accessible with loose object notation like this:
|
84
|
+
|
85
|
+
APP_CONFIG.section.nested_section.value
|
86
|
+
|
87
|
+
You can read more about that [here] [1]
|
88
|
+
|
89
|
+
### Loading more than one set of files:
|
90
|
+
|
91
|
+
# config/initializers/_load_config.rb
|
92
|
+
EMAIL_CONFIG = Yamalicious.load_config(:file_prefix => "email_settings")
|
93
|
+
OTHER_CONFIG = Yamalicious.load_config(:file_prefix => "other_settings")
|
94
|
+
|
95
|
+
|
96
|
+
[1]: https://github.com/intridea/hashie/
|
data/Rakefile
ADDED
data/lib/yamalicious.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "yamalicious/version"
|
2
|
+
require 'i18n/core_ext/hash'
|
3
|
+
require 'hashie/mash'
|
4
|
+
|
5
|
+
# Note deep_merge is in ActiveSupport::CoreExtensionsHash and deep_symbolize_keys is in i18n::CoreExtensions::Hash
|
6
|
+
|
7
|
+
module Yamalicious
|
8
|
+
def self.load_config(options = {})
|
9
|
+
file_prefix = options[:file_prefix] || "yamalicious"
|
10
|
+
config_path = [Rails.root, 'config'].join('/')
|
11
|
+
|
12
|
+
|
13
|
+
yaml_files = {:public => "#{file_prefix}.yml",
|
14
|
+
:private => "#{file_prefix}.local.yml"}
|
15
|
+
|
16
|
+
settings = {}
|
17
|
+
|
18
|
+
yaml_files.each do |k, filename|
|
19
|
+
settings[k] = YAML.load_file([config_path, filename].join("/")) || {} rescue {}
|
20
|
+
end
|
21
|
+
|
22
|
+
settings[:env] = YAML.load(Base64.strict_decode64(ENV[file_prefix.to_s])) || {} rescue {}
|
23
|
+
|
24
|
+
combined = {}
|
25
|
+
|
26
|
+
[:public, :private, :env].each do |name|
|
27
|
+
default_settings = settings[name]["default"] || {}
|
28
|
+
environment_settings = settings[name][Rails.env] || {}
|
29
|
+
combined.deep_merge!(default_settings.deep_merge(environment_settings))
|
30
|
+
end
|
31
|
+
|
32
|
+
::Hashie::Mash.new(combined.deep_symbolize_keys)
|
33
|
+
end
|
34
|
+
end
|
data/yamalicious.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/yamalicious/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nathan Wenneker"]
|
6
|
+
gem.email = ["nathan@gladtocode.com"]
|
7
|
+
gem.description = %q{Load configuration from YAML files and base64 encoded YAML environment variables into a Rails app}
|
8
|
+
gem.summary = %q{YAML configuration loader for Rails apps}
|
9
|
+
gem.homepage = "https://github.com/naw/yamalicious"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "yamalicious"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Yamalicious::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'hashie'
|
19
|
+
gem.add_dependency 'rails'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yamalicious
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.alpha
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nathan Wenneker
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-03 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: rails
|
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: Load configuration from YAML files and base64 encoded YAML environment
|
47
|
+
variables into a Rails app
|
48
|
+
email:
|
49
|
+
- nathan@gladtocode.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/yamalicious.rb
|
60
|
+
- lib/yamalicious/version.rb
|
61
|
+
- yamalicious.gemspec
|
62
|
+
homepage: https://github.com/naw/yamalicious
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>'
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 1.3.1
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.24
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: YAML configuration loader for Rails apps
|
86
|
+
test_files: []
|