envied 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d3ba3eb81e06018962bb52faa7caf1fdf044aca
4
- data.tar.gz: a92a83a6a9272771f39eca36b3f0126aac9fd439
3
+ metadata.gz: 69f7fe8ec05529012ad80743d4f247714b22d369
4
+ data.tar.gz: ffbe2982c00e40da4d468447305b6670721de047
5
5
  SHA512:
6
- metadata.gz: 7d4643b26e39b59b534285cc8f58f0228285468ec3860c3479b0a1cd3b5041a2d7fae2b40ce176c985105d4a793f705d226eef3ccf9591bc1c87a50f9d1b90a5
7
- data.tar.gz: 2407bd5bf9a1700b76d81d77de7a4de9b2b7da1fba44257119444afb9e0e08ed2e4badf19d0f8d7cdecdf314c9a3affb4ef89119d42e0f3a9426bd0ac1d10bae
6
+ metadata.gz: fa4e3adebfadefb43a023b953bfa0f92bb31b87199a8c6404ea0ffdcef542257b58d1981a36ed4105b9e3bb5a348f779a2b368f4c6425d9819b1106afc812b91
7
+ data.tar.gz: 43666f2884ea8a9bc6ffb6d1b1e24f102330d94787e9f51b72a7a6815e91802d7fa516b6eb95b405fe4c4530acb9fb8a4abbd0b066a8a89d3d87b67eb0b3a971
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ # 0.5.0 / unreleased
2
+
3
+ * add Array Hash types
4
+
5
+ ```ruby
6
+ # in env.rb
7
+ ENVied.configure { variable :TAGS, :Array; variable :HASH, :Hash }
8
+ ENVied.require
9
+
10
+ $ HASH=a=1&b=2 TAGS=tag1,tag2 ruby -renvied -r./env.rb -e 'p ENVied.TAGS'
11
+ # ["tag1", "tag2"]
12
+ $ HASH='a=1&b=2' TAGS=tag1,tag2 ruby -renvied -r./env.rb -e 'p ENVied.HASH'
13
+ # {'a' => '1', 'b' => '2'}
14
+ ```
15
+
1
16
  # 0.4.0 / 2014-05-16
2
17
 
3
18
  * groups added
data/README.md CHANGED
@@ -2,45 +2,38 @@
2
2
 
3
3
  ### TL;DR ensure presence and type of your app's ENV-variables.
4
4
 
5
- For applications that are configured via ENV-variables, this gem will provide:
5
+ This gem will provide:
6
6
 
7
- * A fail-fast check for presence of required ENV-variables
8
- * A fail-fast check for type of required ENV-variables
7
+ * A fail-fast check for presence of ENV-variables
8
+ * A fail-fast check whether the values can be coerced to the correct type
9
9
  * Access to typed ENV-variables (instead of just strings)
10
10
 
11
- ### Current status
12
-
13
- ![](underconstruction.gif)
14
-
15
- ## Usage
11
+ ## Quickstart
16
12
 
17
13
  ### 1) Configure
18
14
 
19
15
  Let's configure the ENV-variables we need:
20
16
 
21
17
  ```ruby
22
- # e.g. config/application.rb
23
18
  ENVied.configure do
24
19
  variable :FORCE_SSL, :Boolean
25
20
  variable :PORT, :Integer
26
21
  end
27
22
  ```
28
23
 
29
- ### 2) Check for presence and type
24
+ ### 2) Check for presence and coercibility
30
25
 
31
26
  ```ruby
32
27
  ENVied.require
33
28
  ```
34
29
 
35
- This will throw an error if not:
36
- * both `ENV['FORCE_SSL']` and `ENV['PORT']` are present
37
- * both the values of `ENV['FORCE_SSL']` and `ENV['PORT']` can be coerced (to resp. Boolean or Integer).
38
-
39
- (The error contains exactly what ENV-variables are missing/faulty.)
30
+ This will throw an error if:
31
+ * not both `ENV['FORCE_SSL']` and `ENV['PORT']` are present.
32
+ * the values can't be coerced to resp. Boolean and Integer.
40
33
 
41
- ### 3) Use typed variables
34
+ ### 3) Use coerced variables
42
35
 
43
- Variables accessed via ENVied have the configured type:
36
+ Variables accessed via ENVied are of the correct type:
44
37
 
45
38
  ```ruby
46
39
  ENVied.PORT # => 3001
@@ -52,7 +45,7 @@ ENVied.FORCE_SSL # => false
52
45
  ### Groups
53
46
 
54
47
  Groups give you more flexibility to define when variables are needed.
55
- It's just like you Gemfile:
48
+ It's similar to groups in a Gemfile:
56
49
 
57
50
  ```ruby
58
51
  ENVied.configure do
@@ -85,6 +78,8 @@ The following types are supported:
85
78
  * `:Symbol`
86
79
  * `:Date` (e.g. '2014-3-26')
87
80
  * `:Time` (e.g. '14:00')
81
+ * `:Hash` (e.g. 'a=1&b=2' becomes `{'a' => '1', 'b' => '2'}`)
82
+ * `:Array` (e.g. 'tag1,tag2' becomes `['tag1', 'tag2']`)
88
83
 
89
84
  ### Defaults
90
85
 
@@ -104,7 +99,7 @@ Please remember that ENVied only **reads** from ENV; it doesn't set ENV-variable
104
99
  Don't let setting a default for, say `RAILS_ENV`, give you the impression that `ENV['RAILS_ENV']` is set.
105
100
  As a rule of thumb you should only use defaults:
106
101
  * for local development
107
- * for ENV-variables that your application introduces (i.e. for `ENV['DEFAULT_SENDER']` not for `ENV['REDIS_URL']`)
102
+ * for ENV-variables that your application introduces (i.e. for `ENV['STAFF_EMAILS']` not for `ENV['REDIS_URL']`)
108
103
 
109
104
  ### A more extensive example:
110
105
 
@@ -126,7 +121,7 @@ ENVied.configure(enable_defaults: &not_production_nor_ci) do
126
121
 
127
122
  group :production do
128
123
  variable :MAIL_PAAS_USERNAME
129
- variable :DATABASE_URL, :Symbol
124
+ variable :DATABASE_URL
130
125
  end
131
126
 
132
127
  group :ci do
data/envied.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = ENVied::VERSION
9
9
  spec.authors = ["Gert Goet"]
10
10
  spec.email = ["gert@thinkcreate.nl"]
11
- spec.summary = %q{ENV on EPO}
12
- spec.description = %q{ENV on EPO. Or: ensure presence and type of your app's ENV-variables.}
11
+ spec.summary = %q{Ensure presence and type of ENV-variables}
12
+ spec.description = %q{Ensure presence and type of your app's ENV-variables.}
13
13
  spec.homepage = "https://github.com/eval/envied"
14
14
  spec.license = "MIT"
15
15
 
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "virtus", '~> 1.0.1'
22
+ spec.add_dependency "rack"
22
23
  spec.add_development_dependency "bundler", "~> 1.5"
23
24
  spec.add_development_dependency "rake"
24
- spec.add_development_dependency "rspec", '3.0.0.beta2'
25
+ spec.add_development_dependency "rspec", '~> 3.0.0'
25
26
  end
data/lib/envied.rb CHANGED
@@ -1,11 +1,25 @@
1
1
  require 'virtus'
2
2
 
3
3
  class ENVied
4
+ module Hashable
5
+ def to_hash
6
+ require 'rack/utils'
7
+ ::Rack::Utils.parse_nested_query(self)
8
+ end
9
+ end
10
+
11
+ module Arrayable
12
+ def to_a
13
+ self.split(/(?<!\\), ?/).map{|i| i.gsub(/\\,/,',') }
14
+ end
15
+ end
16
+
4
17
  class Configuration
5
18
  include Virtus.model
6
19
 
7
20
  def self.variable(name, type = :String, options = {})
8
- options = { strict: true, group: self.current_group }.merge(options)
21
+ options = { default: nil, strict: true, group: self.current_group }.merge(options)
22
+ type = Array if type == :Array
9
23
  attribute(name, type, options)
10
24
  end
11
25
 
@@ -36,7 +50,7 @@ class ENVied
36
50
  def self.configuration(options = {}, &block)
37
51
  if block_given?
38
52
  @configuration = build_configuration(&block).tap do |c|
39
- options.each{|k, v| c.public_send("#{k}=", v) }
53
+ options.each {|k, v| c.public_send("#{k}=", v) }
40
54
  end
41
55
  end
42
56
  @configuration ||= build_configuration
@@ -70,7 +84,7 @@ class ENVied
70
84
  @attribute_set << v
71
85
  end
72
86
  end
73
- @instance = group_configuration.new(ENV.to_hash)
87
+ @instance = group_configuration.new(env)
74
88
  end
75
89
 
76
90
  def self.error_on_missing_variables!
@@ -80,6 +94,7 @@ class ENVied
80
94
  end
81
95
 
82
96
  def self.error_on_uncoercible_variables!
97
+ # TODO default values should have defined type
83
98
  if non_coercible_variables.any?
84
99
  single_error = "ENV['%{name}'] ('%{value}' can't be coerced to %{type})"
85
100
  errors = non_coercible_variables.map do |v|
@@ -92,7 +107,13 @@ class ENVied
92
107
  end
93
108
 
94
109
  def self.env_value(variable)
95
- ENV[variable.name.to_s]
110
+ env[variable.name.to_s]
111
+ end
112
+
113
+ def self.env
114
+ @env ||= begin
115
+ Hash[ENV.to_hash.map {|k,v| [k, v.dup.extend(Hashable, Arrayable)] }]
116
+ end
96
117
  end
97
118
 
98
119
  def self.env_value_or_default(variable)
@@ -1,3 +1,3 @@
1
1
  class ENVied
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
data/spec/envied_spec.rb CHANGED
@@ -6,6 +6,19 @@ describe ENVied do
6
6
  it { should respond_to :require }
7
7
  it { should respond_to :configure }
8
8
 
9
+ before do
10
+ reset_env
11
+ reset_configuration
12
+ end
13
+
14
+ def reset_configuration
15
+ ENVied.instance_eval { @configuration = nil }
16
+ end
17
+
18
+ def reset_env
19
+ ENVied.instance_eval { @env = nil }
20
+ end
21
+
9
22
  context 'configured' do
10
23
 
11
24
  def unconfigured
@@ -14,13 +27,11 @@ describe ENVied do
14
27
  end
15
28
 
16
29
  def configure(options = {}, &block)
17
- described_class.instance_eval { @configuration = nil }
18
30
  described_class.configure(options, &block)
19
31
  self
20
32
  end
21
33
 
22
34
  def configured_with(hash = {})
23
- described_class.instance_eval { @configuration = nil }
24
35
  described_class.configure do
25
36
  hash.each do |name, type|
26
37
  variable(name, *type)
@@ -148,6 +159,7 @@ describe ENVied do
148
159
  end
149
160
  end
150
161
  end
162
+
151
163
  describe "groups" do
152
164
  context 'a variable in a group' do
153
165
  before do
@@ -189,6 +201,51 @@ describe ENVied do
189
201
  end
190
202
  end
191
203
  end
204
+
205
+ describe 'Hashable' do
206
+ before do
207
+ configure do
208
+ variable :foo, :Hash
209
+ variable :bar, :Hash
210
+ end.and_ENV('foo' => 'a=1&b=&c', 'bar' => '')
211
+ ENVied.require
212
+ end
213
+
214
+ it 'yields hash from string' do
215
+ expect(ENVied.foo).to eq Hash['a'=> '1', 'b' => '', 'c' => nil]
216
+ end
217
+
218
+ it 'yields hash from an empty string' do
219
+ expect(ENVied.bar).to eq Hash.new
220
+ end
221
+
222
+ context 'with defaults enabled' do
223
+ before do
224
+ configure(enable_defaults: true) do
225
+ variable :baz, :Hash
226
+ end.and_no_ENV
227
+ end
228
+
229
+ it 'has no default by default' do
230
+ # fixes a bug where variables of type :Hash had a default even
231
+ # when none was configured.
232
+ expect { ENVied.require(:default) }.to raise_error
233
+ end
234
+ end
235
+ end
236
+
237
+ describe 'Arrayable' do
238
+ before do
239
+ configure do
240
+ variable :moar, :Array
241
+ end.and_ENV('moar' => 'a, b, and\, c')
242
+ ENVied.require
243
+ end
244
+
245
+ it 'yields array from string' do
246
+ expect(ENVied.moar).to eq ['a','b','and, c']
247
+ end
248
+ end
192
249
  end
193
250
  end
194
251
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: envied
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gert Goet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-16 00:00:00.000000000 Z
11
+ date: 2014-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -56,17 +70,17 @@ dependencies:
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - '='
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: 3.0.0.beta2
75
+ version: 3.0.0
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - '='
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: 3.0.0.beta2
69
- description: 'ENV on EPO. Or: ensure presence and type of your app''s ENV-variables.'
82
+ version: 3.0.0
83
+ description: Ensure presence and type of your app's ENV-variables.
70
84
  email:
71
85
  - gert@thinkcreate.nl
72
86
  executables: []
@@ -106,10 +120,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
120
  version: '0'
107
121
  requirements: []
108
122
  rubyforge_project:
109
- rubygems_version: 2.2.2
123
+ rubygems_version: 2.3.0
110
124
  signing_key:
111
125
  specification_version: 4
112
- summary: ENV on EPO
126
+ summary: Ensure presence and type of ENV-variables
113
127
  test_files:
114
128
  - spec/envied_spec.rb
115
129
  - spec/spec_helper.rb