Skip to content

Commit

Permalink
Initial commit for envyable gem
Browse files Browse the repository at this point in the history
  • Loading branch information
philnash committed Jan 6, 2014
0 parents commit 66544fa
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in envyable.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 Phil Nash

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Envyable

The simplest yaml to ENV config loader.

## Installation

Add this line to your application's Gemfile:

gem 'envyable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install envyable

## Usage

### Rails

Once installed in a Rails app, add a `config/env.yml` file with your config
separated into environments, like so:

```
development:
API_CLIENT_ID: development-id
test:
API_CLIENT_ID: test-id
```

The gem will load the correct environment on initialization of the application.

Do not check in the `config/env.yml` file as it may contain application secrets
that you do not want shared.

### Other applications

You can simply call `Envyable.load(path, environment)` and the yaml file will be loaded into ENV.

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new do |t|
t.test_files = FileList['spec/**/*_spec.rb']
t.verbose = true
end
task :spec => :test
task :default => :test
24 changes: 24 additions & 0 deletions envyable.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'envyable/version'

Gem::Specification.new do |spec|
spec.name = "envyable"
spec.version = Envyable::VERSION
spec.authors = ["Phil Nash"]
spec.email = ["philnash@gmail.com"]
spec.description = %q{The simplest yaml to ENV config loader}
spec.summary = %q{The simplest yaml to ENV config loader}
spec.homepage = "https://github.com/philnash/envyable"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "minitest"
spec.add_development_dependency "rake"
end
14 changes: 14 additions & 0 deletions lib/envyable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'yaml'
require 'envyable/version'
require 'envyable/railtie' if defined?(Rails)

module Envyable
def self.load(path, env='development')
yml = File.expand_path(path)
if File.exists? yml
Array(YAML.load_file(yml)[env]).each do |key,value|
ENV[key.to_s] = value.to_s
end
end
end
end
7 changes: 7 additions & 0 deletions lib/envyable/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Envyable
class Railtie < Rails::Railtie
initializer "envyable.load", :before => :load_environment_config do
Envyable.load Rails.root + 'config/env.yml', Rails.env

This comment has been minimized.

Copy link
@RKushnir

RKushnir Feb 26, 2015

Hey, I think Rails.root.join('config/env.yml') is a more conventional way to write this

This comment has been minimized.

Copy link
@philnash

philnash Feb 26, 2015

Author Owner

You might be right there! Pull requests are, of course, welcome :)

end
end
end
3 changes: 3 additions & 0 deletions lib/envyable/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Envyable
VERSION = "0.0.1"
end
19 changes: 19 additions & 0 deletions spec/envyable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require File.expand_path('../spec_helper', __FILE__)

describe Envyable do
describe '.load' do
it 'should load a yml settings file' do
Envyable.load 'spec/fixtures/env.yml'
ENV['CHUNKY'].must_equal 'bacon'
end

it 'should take an optional environment argument' do
Envyable.load 'spec/fixtures/env.yml', 'staging'
ENV['CHUNKY'].must_equal 'foxes'
end

it 'should not fail if file is not there' do
Envyable.load 'spec/fixtures/nothing.yml'
end
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
development:
CHUNKY: 'bacon'

staging:
CHUNKY: 'foxes'
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rubygems'
require 'minitest/autorun'

require File.join(File.dirname(__FILE__), '..', 'lib', 'envyable')

0 comments on commit 66544fa

Please sign in to comment.