env_bang 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 220e4f75a1ec92e0ce04098d70ef4e163af873cf
4
- data.tar.gz: e919ad22b321abf7936a2eb1f00d81d51d2078f5
3
+ metadata.gz: f61fe363d185a95ba2baba7dfe930515d5b484ae
4
+ data.tar.gz: a89ea79b5655e95d808c5c18bd362e2c671010a9
5
5
  SHA512:
6
- metadata.gz: 00edd476fb5125e6899e15e7c2e606d0f007bdebbf96557fe409acb0ef61d8ffa8f6d53465a5117695c955813893a645fdc407b35519e79f3dc122236e8b91a9
7
- data.tar.gz: 3cc0b1d0080929e7215dc29ea449c1ca91c6b09c5fd941f6d307946a66a125567540ba457b8242b22989b4714532fe61ca8a4ab8ba3f876a4612c7d13fead935
6
+ metadata.gz: 8fe82b08b5a0646f74a1507d5c086be3782f0efb46d289263fc81eca1427dc0760029114e973188d6b03774a1065bbabc1da14132dbe77ca0f41ca8721702df6
7
+ data.tar.gz: 9c61fb918a554319585259bc50d09537b89d0dbd5457209fdad34042bde68a34709da0bb2755ed7b781d5e6b1d248d4904e5c49f3154bb33714281bb6902d0bf
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Gem Version](https://badge.fury.io/rb/env_bang.png)](https://rubygems.org/gems/env_bang)
1
2
  [![Build Status](https://secure.travis-ci.org/jcamenisch/ENV_BANG.png?branch=master)](https://travis-ci.org/jcamenisch/ENV_BANG)
2
3
  [![Dependency Status](https://gemnasium.com/jcamenisch/ENV_BANG.png)](https://gemnasium.com/jcamenisch/ENV_BANG)
3
4
  [![Code Climate](https://codeclimate.com/github/jcamenisch/ENV_BANG.png)](https://codeclimate.com/github/jcamenisch/ENV_BANG)
@@ -9,13 +10,13 @@ Do a bang-up job managing your environment variables.
9
10
 
10
11
  ENV! provides a thin wrapper around ENV to accomplish a few things:
11
12
 
12
- - Provide a central place to specify what environment variables you intend to use
13
- - Fail loudly with an informative error message if environment variables are not
14
- properly configured.
15
- - Prevent an application from starting up if a needed environment variable is not set.
16
- (This is especially helpful in environment like Heroku, as it prevents launch of a
17
- new version of your app if it depends on missing environment variables—a problem
18
- that might go unnoticed until your customers come across a 500 error.)
13
+ - Provide a central place to specify all your app’s environment variables.
14
+ - Fail loudly and helpfully if environment variables are missing.
15
+ - Prevent an application from starting up with missing environment variables.
16
+ (This is especially helpful in environments like Heroku, as your app will
17
+ continue running the old code until the server is configured for a new revision.
18
+ You discover the missing variable immediately instead of your customers
19
+ discovering it for you later.)
19
20
 
20
21
  ## Installation
21
22
 
@@ -51,22 +52,21 @@ end
51
52
  A single variable can also be configured with `ENV!.use MY_VAR`, but the `ENV!.config` block
52
53
  will typically contain all the variables for your app.
53
54
 
54
- Once a variable is specified with `ENV!.use`, you can access it with
55
+ Once a variable is specified with `ENV!.use`, access it with
55
56
 
56
57
  ```ruby
57
58
  ENV!['MY_VAR']
58
59
  ```
59
60
 
60
- This will function just like accessing the environment variable directly, except that it will
61
- require the variable to be specified with `ENV!.use`, and present in the current environment.
62
- If either of these conditions is not met, a KeyError will be raised with an explanation of
63
- what needs to be configured.
61
+ This will function just like accessing `ENV` directly, except that it will require the variable
62
+ to have been specified, and be present in the current environment. If either of these conditions
63
+ is not met, a KeyError will be raised with an explanation of what needs to be configured.
64
64
 
65
65
  ### Adding a default value
66
66
 
67
- In some cases, you don't need to require each environment to set a variable explicitly.
68
- You might have a reasonable default value that can be overridden. You can specify a default
69
- value with the `:default` option:
67
+ For some variables, you’ll want to include a default value in your code, and allow each
68
+ environment to specify the variable only if the default needs to be overriden. You can
69
+ accomplish this with the `:default` option:
70
70
 
71
71
  ```ruby
72
72
  ENV!.config do
@@ -78,10 +78,10 @@ end
78
78
 
79
79
  ### Adding a description
80
80
 
81
- When a new team members installs or deploys your project, they may run into a missing
81
+ When a new team member installs or deploys your project, they may run into a missing
82
82
  environment variable error. It can save them a great deal of time to include documentation
83
- on the missing environment variable directly in the error that is raised. To accomplish this,
84
- provide a description (of any length) to the `use` method:
83
+ along with the error that is raised. To accomplish this, provide a description (of any
84
+ length) to the `use` method:
85
85
 
86
86
  ```ruby
87
87
  ENV!.config do
@@ -90,8 +90,8 @@ ENV!.config do
90
90
  end
91
91
  ```
92
92
 
93
- If someone installs or deploys the app and forgets to set the RAILS_SECRET_KEY_BASE variable in
94
- their environment, they will see these instructions immediately upon running the app.
93
+ Now if someone installs or deploys the app without setting the RAILS_SECRET_KEY_BASE variable,
94
+ they will see these instructions immediately upon running the app.
95
95
 
96
96
  ## Contributing
97
97
 
data/env_bang.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Jonathan Camenisch"]
10
10
  spec.email = ["jonathan@camenisch.net"]
11
11
  spec.summary = %q{Do a bang-up job managing your environment variables}
12
- spec.description = %q{}
13
- spec.homepage = ""
12
+ spec.description = %q{ENV! provides a thin wrapper around ENV to encourage central, self-documenting configuration and helpful error message.}
13
+ spec.homepage = "https://github.com/jcamenisch/ENV_BANG"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -1,3 +1,3 @@
1
1
  class ENV_BANG
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
data/lib/env_bang.rb CHANGED
@@ -11,31 +11,57 @@ class ENV_BANG
11
11
  options = args.last.is_a?(Hash) ? args.pop : {}
12
12
 
13
13
  unless ENV.has_key?(var)
14
- ENV[var] = options.fetch(:default) do
15
- message = "Missing required environment variable: #{var}"
16
- message << "--#{description}" if description
17
- indent = ' '
18
- raise KeyError.new "\n#{indent}#{message.gsub "\n", "\n#{indent}"}\n"
19
- end
14
+ ENV[var] = options.fetch(:default) { formatted_error(var, description) }.to_s
20
15
  end
21
16
 
22
- used_vars << var
17
+ # Store the variable, converted to requested class
18
+ klass = :"#{options.fetch(:class, String)}"
19
+ vars[var] = Classes.cast ENV[var], klass, options
23
20
  end
24
21
 
25
- def used_vars
26
- @used_vars ||= Set.new
22
+ def formatted_error(var, description)
23
+ message = "Missing required environment variable: #{var}"
24
+ message << "\n#{description}" if description
25
+ indent = ' '
26
+ raise KeyError.new "\n#{indent}#{message.gsub "\n", "\n#{indent}"}\n"
27
+ end
28
+
29
+ def vars
30
+ @vars ||= {}
27
31
  end
28
32
 
29
33
  def [](var)
30
- raise KeyError.new("ENV_BANG is not configured to use var #{var}") unless used_vars.include?(var)
34
+ raise KeyError.new("ENV_BANG is not configured to use var #{var}") unless vars.has_key?(var)
31
35
 
32
- ENV[var]
36
+ vars[var]
33
37
  end
34
38
 
35
39
  def method_missing(method, *args, &block)
36
40
  ENV.send(method, *args, &block)
37
41
  end
38
42
  end
43
+
44
+ module Classes
45
+ class << self
46
+ def cast(value, klass, options = {})
47
+ public_send(:"#{klass}", value, options)
48
+ end
49
+
50
+ def boolean(value, options)
51
+ !(value =~ /^(|0|disabled?|false|no|off)$/i)
52
+ end
53
+
54
+ def Array(value, options)
55
+ klass = options.fetch(:of, String)
56
+ value.split(',').map { |value| cast(value.strip, klass) }
57
+ end
58
+
59
+ # Delegate methods like Integer(), Float(), String(), etc. to the Kernel module
60
+ def method_missing(klass, value, options, &block)
61
+ Kernel.send(klass, value)
62
+ end
63
+ end
64
+ end
39
65
  end
40
66
 
41
67
  def ENV!
@@ -44,4 +44,63 @@ describe ENV_BANG do
44
44
  end
45
45
  ENV!['PRESENT'].must_equal 'present in environment'
46
46
  end
47
+
48
+ describe "Type casting" do
49
+ let(:truthy_values) { %w[true on yes yo yup anything] }
50
+ let(:falsey_values) { %w[false no off disable disabled 0] << '' }
51
+ let(:integers) { %w[0 1 10 -42 -55] }
52
+ let(:floats) { %w[0.1 1.3 10 -42.3 -55] }
53
+
54
+ it "Casts Integers" do
55
+ integer = integers.sample
56
+ ENV['INTEGER'] = integer
57
+ ENV!.use 'INTEGER', class: Integer
58
+
59
+ ENV!['INTEGER'].must_equal integer.to_i
60
+ end
61
+
62
+ it "Casts Floats" do
63
+ float = floats.sample
64
+ ENV['FLOAT'] = float
65
+ ENV!.use 'FLOAT', class: Float
66
+
67
+ ENV!['FLOAT'].must_equal float.to_f
68
+ ENV!['FLOAT'].class.must_equal Float
69
+ end
70
+
71
+ it "Casts Arrays" do
72
+ ENV['ARRAY'] = 'one,two , three, four'
73
+ ENV!.use 'ARRAY', class: Array
74
+
75
+ ENV!['ARRAY'].must_equal %w[one two three four]
76
+ end
77
+
78
+ it "Casts Arrays of Integers" do
79
+ ENV['INTEGERS'] = integers.join(',')
80
+ ENV!.use 'INTEGERS', class: Array, of: Integer
81
+
82
+ ENV!['INTEGERS'].must_equal integers.map(&:to_i)
83
+ end
84
+
85
+ it "Casts Arrays of Floats" do
86
+ ENV['FLOATS'] = floats.join(',')
87
+ ENV!.use 'FLOATS', class: Array, of: Float
88
+
89
+ ENV!['FLOATS'].must_equal floats.map(&:to_f)
90
+ end
91
+
92
+ it "Casts false" do
93
+ ENV['TRUE'] = truthy_values.sample
94
+ ENV!.use 'TRUE', class: :boolean
95
+
96
+ ENV!['TRUE'].must_equal true
97
+ end
98
+
99
+ it "Casts false" do
100
+ ENV['FALSE'] = falsey_values.sample
101
+ ENV!.use 'FALSE', class: :boolean
102
+
103
+ ENV!['FALSE'].must_equal false
104
+ end
105
+ end
47
106
  end
data/test/test_helper.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'simplecov'
2
2
  require 'coveralls'
3
3
 
4
+ require 'minitest/autorun'
4
5
  require 'minitest/spec'
5
6
  require 'minitest/pride'
6
- require 'minitest/autorun'
7
7
 
8
8
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
9
9
  SimpleCov::Formatter::HTMLFormatter,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_bang
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Camenisch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-07 00:00:00.000000000 Z
11
+ date: 2013-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,7 +38,8 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '5.1'
41
- description: ''
41
+ description: ENV! provides a thin wrapper around ENV to encourage central, self-documenting
42
+ configuration and helpful error message.
42
43
  email:
43
44
  - jonathan@camenisch.net
44
45
  executables: []
@@ -56,7 +57,7 @@ files:
56
57
  - lib/env_bang/version.rb
57
58
  - test/env_bang_test.rb
58
59
  - test/test_helper.rb
59
- homepage: ''
60
+ homepage: https://github.com/jcamenisch/ENV_BANG
60
61
  licenses:
61
62
  - MIT
62
63
  metadata: {}