Global
The 'global' gem provides accessor methods for your configuration data and share configuration across backend and frontend. The data is stored in yaml files.
Installation
Add to Gemfile:
gem 'global'
Configuration
> Global.environment = "YOUR_ENV_HERE"
> Global.config_directory = "PATH_TO_DIRECTORY_WITH_FILES"
> Global.yaml_whitelist_classes = [] # optional configuration
Or you can use configure
block:
Global.configure do |config|
config.environment = "YOUR_ENV_HERE"
config.config_directory = "PATH_TO_DIRECTORY_WITH_FILES"
config.yaml_whitelist_classes = [] # optional configuration
end
For rails put initialization into config/initializers/global.rb
Global.configure do |config|
config.environment = Rails.env.to_s
config.config_directory = Rails.root.join('config/global').to_s
config.yaml_whitelist_classes = [] # optional configuration
end
The yaml_whitelist_classes
configuration allows you to deserialize other classes from your .yml
Usage
General
Config file config/global/hosts.yml
:
test:
web: localhost
api: api.localhost
development:
web: localhost
api: api.localhost
production:
web: myhost.com
api: api.myhost.com
In the development environment we now have:
> Global.hosts
=> { "api" => "api.localhost", "web" => "localhost" }
> Global.hosts.api
=> "api.localhost"
Deserialize other classes from .yml
Config file config/global/validations.yml
:
default:
regexp:
email: !ruby/regexp /.@.+\../
Ensure that Regexp
is included in the yaml_whitelist_classes
array
Global.validations.regexp.email === '[email protected]'
=> true
Namespacing
Config file config/global/web/basic_auth.yml
with:
test:
username: test_user
password: secret
development:
username: development_user
password: secret
production:
username: production_user
password: supersecret
After that in development environment we have:
> Global.web.basic_auth
=> { "username" => "development_user", "password" => "secret" }
> Global.web.basic_auth.username
=> "development_user"
Default section
Config file example:
default:
web: localhost
api: api.localhost
production:
web: myhost.com
api: api.myhost.com
Data from the default section is used until it's overridden in a specific environment.
Nested configurations
Config file global/nested.yml
with:
test:
group:
key: "test value"
development:
group:
key: "development value"
production:
group:
key: "production value"
Nested options can then be accessed as follows:
> Global.nested.group.key
=> "development value"
Environment files
Config file global/aws.yml
with:
:default:
activated: false
staging:
activated: true
api_key: 'nothing'
And file global/aws.production.yml
with:
:activated: true
:api_key: 'some api key'
:api_secret: 'some secret'
Provide such configuration on Global.environment = 'production'
environment:
> Global.aws.activated
=> true
> Global.aws.api_key
=> 'some api key'
> Global.aws.api_secret
=> 'some secret'
Warning: files with dot(s) in name will be skipped by Global (except this env files).
ERB support
Config file global/file_name.yml
with:
test:
key: <%=1+1%>
development:
key: <%=2+2%>
production:
key: <%=3+3%>
As a result, in the development environment we have:
> Global.file_name.key
=> 4
Reload configuration data
> Global.reload!
JavaScript in Rails support
Configuration
Global.configure do |config|
config.namespace = "JAVASCRIPT_OBJECT_NAME" # default Global
config.except = ["LIST_OF_FILES_TO_EXCLUDE_ON_FRONT_END"] # default :all
config.only = ["LIST_OF_FILES_TO_INCLUDE_ON_FRONT_END"] # default []
end
By default all files are excluded due to security reasons. Don't include files which contain protected information like api keys or credentials.
Require global file in application.js
:
/*
= require global-js
*/
Advanced Configuration
In case you need different configurations for different parts of your application, you should create the files manually.
If your application has admin
and application
namespace:
# app/assets/javascripts/admin/global.js.erb
<%= Global.generate_js(namespace: "AdminSettings", only: %w(admin hosts)) %>
# app/assets/javascripts/admin.js.coffee
#= require admin/global
# app/assets/javascripts/application/global.js.erb
<%= Global.generate_js(namespace: "AppSettings", except: %w(admin credentials)) %>
# app/assets/javascripts/application.js.coffee
#= require application/global
Usage
Config file example global/hosts.yml
:
development:
web: localhost
api: api.localhost
production:
web: myhost.com
api: api.myhost.com
After that in development environment we have:
Global.hosts.web
=> "localhost"
And in production:
Global.hosts.web
=> "myhost.com"
Contributing to global
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
- Fork the project.
- Start a feature/bugfix branch.
- Commit and push until you are happy with your contribution.
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright (c) Railsware LLC. See LICENSE.txt for further details.