environs 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
- - "1.8.7"
4
- - "1.9.2"
5
3
  - "1.9.3"
6
4
  - "2.0.0"
7
- - jruby-18mode # JRuby in 1.8 mode
5
+ - "2.1.0"
8
6
  - jruby-19mode # JRuby in 1.9 mode
9
- - rbx-18mode
10
- - rbx-19mode
7
+ notifications:
8
+ campfire:
9
+ secure: "GnEk4I+ZRjW8h7x6rtZaI/2BfhErJkeELkbjamg3a4CnRQgAKzQZUNwQL8NH\nfbxmAcVSZfVPF6unw0XMY54pgO0+6Fp6fkypaV4fpDgSJEE+6QsSJxBEPN0w\n0bkXKa7/gsRpPLx6GwkQCqSAQIf+aVB0zTV7m1spMoRE40cXZss="
10
+ on_success: always
11
+ on_failure: always
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in environs.gemspec
4
4
  gemspec
5
+
6
+ gem 'coveralls', :require => false
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Environs
2
+ [![Code Climate](https://codeclimate.com/repos/51d37914c7f3a376e005daec/badges/df1a7c1a328f0eda6db1/gpa.png)](https://codeclimate.com/repos/51d37914c7f3a376e005daec/feed)
3
+ [![Build Status](https://travis-ci.org/primedia/environs.png)](https://travis-ci.org/primedia/environs)
4
+ [![Coverage Status](https://coveralls.io/repos/primedia/environs/badge.png?branch=dev)](https://coveralls.io/r/primedia/environs?branch=dev)
5
+ [![Dependency Status](https://gemnasium.com/primedia/environs.png)](https://gemnasium.com/primedia/environs)
6
+ [![Gem Version](https://badge.fury.io/rb/environs.png)](http://badge.fury.io/rb/environs)
2
7
 
3
- TODO: Write a gem description
8
+ A simple wrapper around the ENV hash that by default raises an error when value requested is not set.
4
9
 
5
10
  ## Installation
6
11
 
@@ -18,7 +23,19 @@ Or install it yourself as:
18
23
 
19
24
  ## Usage
20
25
 
21
- TODO: Write usage instructions here
26
+ This gem provides an `Env` class that dynamically retrieves environment variables based on the name of the method called on the `Env` class object. Method names are converted to `SCREAMING_SNAKE_CASE`, e.g. `Env.foo_bar` tries to retrieve `ENV['FOO_BAR']`. If a non-nil value is not returned for the key, then an error is raised. Nil values can be allowed by passing a Hash with the `allow_nil` key set to true.
27
+
28
+ ```ruby
29
+ require 'environs'
30
+
31
+ ENV['FOO'] = nil
32
+
33
+ Env.foo
34
+ # => 'MissingEnvVarError "The FOO environment variable is not set."'
35
+
36
+ Env.foo(allow_nil: true)
37
+ # => nil
38
+ ```
22
39
 
23
40
  ## Contributing
24
41
 
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency 'bundler', '~> 1.3'
21
+ spec.add_development_dependency 'bundler'
22
22
  spec.add_development_dependency 'rake'
23
23
  spec.add_development_dependency 'rspec'
24
24
  end
@@ -3,20 +3,22 @@ require 'environs/missing_env_var_error'
3
3
  class Env
4
4
  class << self
5
5
  def method_missing(method, *args, &block)
6
- key = method.to_s.upcase
7
- if key.match(/\w+/)
8
- ENV[key] || key_not_found_response(args.first, key)
9
- else
10
- super
11
- end
6
+ key = method.to_s.upcase
7
+ key.match(/\w+/) ? env_key(key, *args) : super
12
8
  end
13
9
 
14
- def key_not_found_response(potential_hash, key)
15
- if potential_hash.respond_to?(:fetch) && potential_hash.fetch(:allow_nil)
16
- nil
17
- else
18
- raise MissingEnvVarError, "The #{key} environment variable is not set."
19
- end
10
+ private
11
+
12
+ def env_key(key, nil_allowance = {})
13
+ ENV.fetch(key) { |k| key_not_found(nil_allowance, key) }
14
+ end
15
+
16
+ def key_not_found(nil_allowance, key)
17
+ nil_allowance[:allow_nil] ? nil : env_var_error(key)
18
+ end
19
+
20
+ def env_var_error(key)
21
+ raise MissingEnvVarError, "The #{key} environment variable is not set."
20
22
  end
21
23
  end
22
24
  end
@@ -1,3 +1,3 @@
1
1
  module Environs
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'environs'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Env, "#foo" do
4
4
 
@@ -7,11 +7,16 @@ describe Env, "#foo" do
7
7
  Env.foo.should eq 'bar'
8
8
  end
9
9
 
10
+ it "converts method name to SCREAMING_SNAKE_CASE for lookup" do
11
+ ENV['FOO_BAR_BAZ'] = 'bar'
12
+ Env.foo_bar_baz.should eq 'bar'
13
+ end
14
+
10
15
  describe "default behavior" do
11
16
 
12
17
  it "returns an error if no corresponding ENV variable is set" do
13
- ENV['FOO'] = nil
14
- expect { Env.foo }.to raise_error
18
+ ENV['FOO_NO_VALUE'] = nil
19
+ expect { Env.foo_no_value }.to raise_error
15
20
  end
16
21
 
17
22
  end
@@ -19,8 +24,8 @@ describe Env, "#foo" do
19
24
  describe "'allow_nil: true' is passed along" do
20
25
 
21
26
  it "returns nil if no corresponding ENV var is set" do
22
- ENV['FOO'] = nil
23
- Env.foo(allow_nil: true).should be_nil
27
+ ENV['FOO_ALLOW_NIL'] = nil
28
+ Env.foo_allow_nil(:allow_nil => true).should be_nil
24
29
  end
25
30
 
26
31
  end
@@ -0,0 +1,4 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'environs'
metadata CHANGED
@@ -1,64 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: environs
3
3
  version: !ruby/object:Gem::Version
4
+ version: 1.1.0
4
5
  prerelease:
5
- version: 1.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Colin Rymer
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-01 00:00:00.000000000 Z
12
+ date: 2014-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- version_requirements: !ruby/object:Gem::Requirement
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - ~>
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
- version: '1.3'
20
- none: false
21
- name: bundler
21
+ version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- requirement: !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
25
26
  requirements:
26
- - - ~>
27
+ - - ! '>='
27
28
  - !ruby/object:Gem::Version
28
- version: '1.3'
29
- none: false
29
+ version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- version_requirements: !ruby/object:Gem::Requirement
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
32
34
  requirements:
33
35
  - - ! '>='
34
36
  - !ruby/object:Gem::Version
35
37
  version: '0'
36
- none: false
37
- name: rake
38
38
  type: :development
39
39
  prerelease: false
40
- requirement: !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
41
42
  requirements:
42
43
  - - ! '>='
43
44
  - !ruby/object:Gem::Version
44
45
  version: '0'
45
- none: false
46
46
  - !ruby/object:Gem::Dependency
47
- version_requirements: !ruby/object:Gem::Requirement
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
48
50
  requirements:
49
51
  - - ! '>='
50
52
  - !ruby/object:Gem::Version
51
53
  version: '0'
52
- none: false
53
- name: rspec
54
54
  type: :development
55
55
  prerelease: false
56
- requirement: !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
57
58
  requirements:
58
59
  - - ! '>='
59
60
  - !ruby/object:Gem::Version
60
61
  version: '0'
61
- none: false
62
62
  description: A simple wrapper around the ENV hash that by default raises an error
63
63
  when value requested is not set.
64
64
  email:
@@ -79,6 +79,7 @@ files:
79
79
  - lib/environs/missing_env_var_error.rb
80
80
  - lib/environs/version.rb
81
81
  - spec/environs/env_spec.rb
82
+ - spec/spec_helper.rb
82
83
  homepage: https://github.com/primedia/environs
83
84
  licenses:
84
85
  - MIT
@@ -87,17 +88,17 @@ rdoc_options: []
87
88
  require_paths:
88
89
  - lib
89
90
  required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
90
92
  requirements:
91
93
  - - ! '>='
92
94
  - !ruby/object:Gem::Version
93
95
  version: '0'
94
- none: false
95
96
  required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
96
98
  requirements:
97
99
  - - ! '>='
98
100
  - !ruby/object:Gem::Version
99
101
  version: '0'
100
- none: false
101
102
  requirements: []
102
103
  rubyforge_project:
103
104
  rubygems_version: 1.8.23
@@ -107,4 +108,5 @@ summary: A simple wrapper around the ENV hash that by default raises an error wh
107
108
  value requested is not set.
108
109
  test_files:
109
110
  - spec/environs/env_spec.rb
111
+ - spec/spec_helper.rb
110
112
  has_rdoc: