env_bang 0.2.7 → 0.2.8

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: b4a31d98f7b91fa5c4bbad7088e70e2acb7b8403
4
- data.tar.gz: e4285280d46906f66d633a79ec99297ef7fdc132
3
+ metadata.gz: 75f9cc5013ad1fb2d5f0ee0662da09b492e28470
4
+ data.tar.gz: 30f9f2473f6d747fb9079082221d0fffd94560c7
5
5
  SHA512:
6
- metadata.gz: 825091731f2bcd8b74aa435a2612d099a936797840fb3a035097254c9897049984614aaf14619f53e1960425e7998ffe661aaed8e17f8f45d2140c8cdffe089f
7
- data.tar.gz: bdb5e802b7e705a9d84b55a1bcdb11b6ffe58ee9b67494f9202a73935caa5aedeea254261af8de852d459bfc6115a28dbd5cbc700b8961ae9863aa7c0e8cc94f
6
+ metadata.gz: 485ac95c1327653a3b9e0224e728202484217ab3753267b6f8457c3065c00fbd75d2b9b25d72d26b426fba9da90e03fdcd82ff371dce348ab5c4c2c6af1ce4e5
7
+ data.tar.gz: 82e8491ea7ad0a668c6b57cd388b56e1e0eb516a3bfcef0b817ac0205d50a1036f4e1c908a3ff87f1f119a39e9c1f172456758b03f9c44c8ee93b8f9e829ada3
data/.travis.yml CHANGED
@@ -1,5 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
3
  - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - ruby-head
7
+ - jruby-19mode
8
+ - jruby-head
5
9
  bundler_args: --without=guard
10
+ cache: bundler
data/README.md CHANGED
@@ -11,48 +11,55 @@ Do a bang-up job managing your environment variables.
11
11
  ENV! provides a thin wrapper around ENV to accomplish a few things:
12
12
 
13
13
  - Provide a central place to specify all your app’s environment variables.
14
- - Fail loudly and helpfully if environment variables are missing.
14
+ - Fail loudly and helpfully if any environment variables are missing.
15
15
  - Prevent an application from starting up with missing environment variables.
16
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.)
17
+ continue running the old code until the server is configured for a new revision.)
20
18
 
21
19
  ## Installation
22
20
 
23
- Add this line to your application's Gemfile:
21
+ Add this line to your applications Gemfile:
24
22
 
25
- gem 'env_bang'
23
+ ```ruby
24
+ gem 'env_bang'
25
+ ```
26
26
 
27
- And then execute:
27
+ Or for Rails apps, use `env_bang-rails` instead for more convenience:
28
28
 
29
- $ bundle
29
+ ```ruby
30
+ gem 'env_bang-rails
31
+ ```
30
32
 
31
- Or install it yourself as:
33
+ And then execute:
32
34
 
33
- $ gem install env_bang
35
+ ```sh
36
+ $ bundle
37
+ ```
34
38
 
35
39
  ## Usage
36
40
 
37
41
  ### Basic Configuration
38
42
 
39
43
  First, configure your environment variables somewhere in your app’s
40
- startup process. In a Rails app, this could live in `config/initializers/env.rb`.
44
+ startup process. If you use the env_bang-rails gem, place this in `config/env.rb`
45
+ to load before application configuration.
46
+
47
+ Example configuration:
41
48
 
42
49
  ```ruby
43
50
  ENV!.config do
44
- use 'APP_HOST'
45
- use 'RAILS_SECRET_TOKEN'
46
- use 'STRIPE_SECRET_KEY'
47
- use 'STRIPE_PUBLISHABLE_KEY'
51
+ use :APP_HOST
52
+ use :RAILS_SECRET_TOKEN
53
+ use :STRIPE_SECRET_KEY
54
+ use :STRIPE_PUBLISHABLE_KEY
48
55
  # ... etc.
49
56
  end
50
57
  ```
51
58
 
52
59
  A single variable can also be configured with `ENV!.use MY_VAR`, but the `ENV!.config` block
53
- will typically contain all the variables for your app.
60
+ will typically be a cleaner place for all the variables for your app.
54
61
 
55
- Once a variable is specified with `ENV!.use`, access it with
62
+ Once a variable is specified with the `use` method, access it with
56
63
 
57
64
  ```ruby
58
65
  ENV!['MY_VAR']
@@ -65,13 +72,13 @@ is not met, a KeyError will be raised with an explanation of what needs to be co
65
72
  ### Adding a default value
66
73
 
67
74
  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:
75
+ environment to ommit the variable for default behavios. You can accomplish this with the
76
+ `:default` option:
70
77
 
71
78
  ```ruby
72
79
  ENV!.config do
73
80
  # ...
74
- use MAIL_DELIVERY_METHOD, default: :smtp
81
+ use :MAIL_DELIVERY_METHOD, default: 'smtp'
75
82
  # ...
76
83
  end
77
84
  ```
@@ -79,9 +86,8 @@ end
79
86
  ### Adding a description
80
87
 
81
88
  When a new team member installs or deploys your project, they may run into a missing
82
- environment variable error. It can save them a great deal of time to include documentation
83
- along with the error that is raised. To accomplish this, provide a description (of any
84
- length) to the `use` method:
89
+ environment variable error. Save them time by including documentation along with the error
90
+ that is raised. To accomplish this, provide a description (of any length) to the `use` method:
85
91
 
86
92
  ```ruby
87
93
  ENV!.config do
@@ -93,6 +99,74 @@ end
93
99
  Now if someone installs or deploys the app without setting the RAILS_SECRET_KEY_BASE variable,
94
100
  they will see these instructions immediately upon running the app.
95
101
 
102
+ ### Automatic type conversion
103
+
104
+ ENV! can convert your environment variables for you, keeping that tedium out of your application
105
+ code. To specify a type, use the `:class` option:
106
+
107
+ ```ruby
108
+ ENV!.config do
109
+ use :COPYRIGHT_YEAR, class: Integer
110
+ use :MEMCACHED_SERVERS, class: Array
111
+ use :MAIL_DELIVERY_METHOD, class: Symbol, default: :smtp
112
+ use :DEFAULT_FRACTION, class: Float
113
+ use :ENABLE_SOUNDTRACK, class: :boolean
114
+ end
115
+ ```
116
+
117
+ Note that arrays will be derived by splitting the value on commas (','). To get arrays
118
+ of a specific type of value, use the `:of` option:
119
+
120
+ ```ruby
121
+ ENV!.config do
122
+ use :YEARS_OF_INTEREST, class: Array, of: Integer
123
+ end
124
+ ```
125
+
126
+ #### Default type conversion behavior
127
+
128
+ If you don’t specify a `:class` option for a variable, ENV! defaults to a special
129
+ type conversion called `:StringUnlessFalsey`. This coversion returns a string, unless
130
+ the value is a "falsey" string ('false', 'no', 'off', '0', 'disable', or 'disabled').
131
+ To turn off this magic for one variable, pass in `class: String`. To disable it globally,
132
+ set
133
+
134
+ ```ruby
135
+ ENV!.config do
136
+ Classes.default_class = String
137
+ end
138
+
139
+ #### Custom type conversion
140
+
141
+ Suppose your app needs a special type conversion that doesn’t come with ENV_BANG. You can
142
+ implement the conversion yourself with the `add_class` method in the `ENV!.config` block.
143
+ For example, to convert one of your environment variables to type `Set`, you could write
144
+ the following configuration:
145
+
146
+ ```sh
147
+ # In your environment:
148
+ export NUMBER_SET=1,3,5,7,9
149
+ ```
150
+
151
+ ```ruby
152
+ # In your env.rb configuration file:
153
+ require 'set'
154
+
155
+ ENV!.config do
156
+ add_class Set do |value, options|
157
+ Set.new self.Array(value, options || {})
158
+ end
159
+
160
+ use :NUMBER_SET, class: Set, of: Integer
161
+ end
162
+ ```
163
+
164
+ ```ruby
165
+ # Somewhere in your application:
166
+ ENV!['NUMBER_SET']
167
+ #=> #<Set: {1, 3, 5, 7, 9}>
168
+ ```
169
+
96
170
  ## Contributing
97
171
 
98
172
  1. Fork it
data/lib/env_bang.rb CHANGED
@@ -12,6 +12,7 @@ class ENV_BANG
12
12
  end
13
13
 
14
14
  def use(var, *args)
15
+ var = var.to_s
15
16
  description = args.first.is_a?(String) && args.shift
16
17
  options = args.last.is_a?(Hash) ? args.pop : {}
17
18
 
@@ -39,6 +40,7 @@ class ENV_BANG
39
40
  end
40
41
 
41
42
  def [](var)
43
+ var = var.to_s
42
44
  raise KeyError.new("ENV_BANG is not configured to use var #{var}") unless vars.has_key?(var)
43
45
 
44
46
  Classes.cast ENV[var], vars[var]
@@ -47,12 +49,30 @@ class ENV_BANG
47
49
  def method_missing(method, *args, &block)
48
50
  ENV.send(method, *args, &block)
49
51
  end
52
+
53
+ def add_class(klass, &block)
54
+ Classes.send :define_singleton_method, klass.to_s, &block
55
+ end
56
+
57
+ def default_class
58
+ Classes.default_class
59
+ end
60
+
61
+ def default_class=(value)
62
+ Classes.default_class = value
63
+ end
50
64
  end
51
65
 
52
66
  module Classes
53
67
  class << self
68
+ attr_writer :default_class
69
+
70
+ def default_class
71
+ @default_class ||= :StringUnlessFalsey
72
+ end
73
+
54
74
  def cast(value, options = {})
55
- public_send(:"#{options.fetch(:class, :StringUnlessFalsey)}", value, options)
75
+ public_send(:"#{options.fetch(:class, default_class)}", value, options)
56
76
  end
57
77
 
58
78
  def boolean(value, options)
@@ -1,3 +1,3 @@
1
1
  class ENV_BANG
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
3
3
  end
@@ -117,6 +117,31 @@ describe ENV_BANG do
117
117
  ENV!['FALSE'].class.must_equal String
118
118
  end
119
119
 
120
+ it "allows default class to be overridden" do
121
+ ENV!.config { default_class = String }
122
+ ENV['FALSE'] = falsey_values.sample
123
+ ENV!.use 'FALSE', class: String
124
+
125
+ ENV!['FALSE'].class.must_equal String
126
+ end
127
+
128
+ it "allows addition of custom types" do
129
+ require 'set'
130
+
131
+ ENV['NUMBER_SET'] = '1,3,5,7,9'
132
+ ENV!.config do
133
+ add_class Set do |value, options|
134
+ Set.new self.Array(value, options || {})
135
+ end
136
+
137
+ use :NUMBER_SET, class: Set, of: Integer
138
+ end
139
+
140
+ ENV!['NUMBER_SET'].must_equal Set.new [1, 3, 5, 7, 9]
141
+ end
142
+ end
143
+
144
+ describe "Hash-like behavior" do
120
145
  it "provides configured keys" do
121
146
  ENV['VAR1'] = 'something'
122
147
  ENV['VAR2'] = 'something else'
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.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Camenisch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-31 00:00:00.000000000 Z
11
+ date: 2014-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake