envied 0.2.0 → 0.3.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 +4 -4
- data/README.md +13 -6
- data/lib/envied.rb +28 -11
- data/spec/envied_spec.rb +19 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f0f26f023604dac352019bbee3aecde27fc9da2
|
4
|
+
data.tar.gz: 653204a1873319f4e4e15420fd17e659a98e187a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88c894f8addd2f09ce5406048f3010b7d4bd561aad6a0bd1b8461e6d5e8170c559f314edb45aab1d3d4efec2c9afe51b705e696dacde64eaa10a2ea2a5b4f92c
|
7
|
+
data.tar.gz: a43d8d92ef633b6fc1b390939ce440e768dac363a4ec05b229238cebc23894ca68d8b160bca65c93d9f9007328a81c53d82d2390a99411cb627653e193d63edb
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ end
|
|
32
32
|
ENVied.require!
|
33
33
|
```
|
34
34
|
Excecution will halt unless ENV is something like
|
35
|
-
`{'FORCE_SSL' => '
|
35
|
+
`{'FORCE_SSL' => 'false', 'PORT' => '3001'}`.
|
36
36
|
|
37
37
|
A meaningful error will in this case explain what key and type is needed.
|
38
38
|
|
@@ -41,7 +41,7 @@ A meaningful error will in this case explain what key and type is needed.
|
|
41
41
|
Variables accessed via ENVied have the configured type:
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
ENVied.port # =>
|
44
|
+
ENVied.port # => 3001
|
45
45
|
ENVied.force_ssl # => false
|
46
46
|
```
|
47
47
|
|
@@ -60,16 +60,23 @@ The following types are supported:
|
|
60
60
|
|
61
61
|
### Defaults
|
62
62
|
|
63
|
-
|
63
|
+
In order to let other developers easily bootstrap the application, you can assign defaults to variables.
|
64
|
+
Defaults can be a value or a `Proc` (see example below).
|
65
|
+
|
66
|
+
Note that 'easily bootstrap' is rather different than 'fail-fast when not all ENV-variables are present'. Therefor it's disabled by default and you should explicitly state whén it is allowed:
|
64
67
|
|
65
68
|
```ruby
|
66
|
-
ENVied.configure do |env|
|
69
|
+
ENVied.configure(enable_defaults: Rails.env.development?) do |env|
|
67
70
|
env.variable :port, :Integer, default: proc {|env, variable| env.force_ssl ? 443 : 80 }
|
68
|
-
env.variable :force_ssl, :Boolean, default:
|
71
|
+
env.variable :force_ssl, :Boolean, default: false
|
69
72
|
end
|
70
73
|
```
|
71
74
|
|
72
|
-
Please remember that ENVied only **reads** from ENV; don't let setting a default for, say `rails_env`, give you or your team the impression that `ENV['RAILS_ENV']` is set.
|
75
|
+
Please remember that ENVied only **reads** from ENV; don't let setting a default for, say `rails_env`, give you or your team the impression that `ENV['RAILS_ENV']` is set.
|
76
|
+
As a rule of thumb: you should only use defaults:
|
77
|
+
* in a development-environment
|
78
|
+
* for ENV-variables that your application introduces (i.e. for `ENV['DEFAULT_SENDER']` not for `ENV['REDIS_URL']`)
|
79
|
+
|
73
80
|
|
74
81
|
## Installation
|
75
82
|
|
data/lib/envied.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class ENVied
|
2
|
-
VERSION = "0.
|
2
|
+
VERSION = "0.3.0"
|
3
3
|
module Configurable
|
4
4
|
require 'virtus'
|
5
5
|
|
@@ -30,6 +30,9 @@ class ENVied
|
|
30
30
|
base.class_eval do
|
31
31
|
include Virtus.model
|
32
32
|
|
33
|
+
class << self
|
34
|
+
attr_accessor :enable_defaults
|
35
|
+
end
|
33
36
|
end
|
34
37
|
base.extend ClassMethods
|
35
38
|
end
|
@@ -58,19 +61,21 @@ class ENVied
|
|
58
61
|
new(atts)
|
59
62
|
end
|
60
63
|
|
64
|
+
# Define a variable.
|
65
|
+
#
|
66
|
+
# @param name [Symbol] name of the variable
|
67
|
+
# @param type [Symbol] type (one of :String (default), :Symbol, :Integer, :Boolean,
|
68
|
+
# :Date, :Time)
|
69
|
+
# @param options [Hash]
|
70
|
+
# @option options [String, Integer, Boolean, #call] :default (nil) what value will be
|
71
|
+
# used when no ENV-variable is present.
|
72
|
+
# @note Defaults are ignored by default, see {configure}.
|
73
|
+
#
|
61
74
|
def variable(name, type = :String, options = {})
|
75
|
+
options.delete(:default) unless self.enable_defaults
|
62
76
|
attribute name, type, { strict: true }.merge(options)
|
63
77
|
end
|
64
78
|
|
65
|
-
def default_if?(cond, value = nil, &block)
|
66
|
-
options = { default: (value || block) }
|
67
|
-
cond ? options : Hash.new
|
68
|
-
end
|
69
|
-
|
70
|
-
def default_unless?(cond, value = nil, &block)
|
71
|
-
default_if?(!cond, value, &block)
|
72
|
-
end
|
73
|
-
|
74
79
|
private
|
75
80
|
def try_coercion(variable, value, default)
|
76
81
|
value ||= begin
|
@@ -90,8 +95,20 @@ class ENVied
|
|
90
95
|
attr_accessor :configuration
|
91
96
|
end
|
92
97
|
|
93
|
-
|
98
|
+
# Configure ENVied.
|
99
|
+
#
|
100
|
+
# @param options [Hash]
|
101
|
+
# @option options [Boolean] :enable_defaults (false) whether or not defaults are used.
|
102
|
+
#
|
103
|
+
# @example
|
104
|
+
# ENVied.configure(enable_defaults: Rails.env.development?) do
|
105
|
+
# variable :force_ssl, :Boolean, default: false
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
def self.configure(options = {}, &block)
|
109
|
+
options = { enable_defaults: false }.merge(options)
|
94
110
|
@configuration = Class.new { include Configurable }.tap do |k|
|
111
|
+
k.enable_defaults = options[:enable_defaults]
|
95
112
|
k.instance_eval(&block)
|
96
113
|
end
|
97
114
|
# or define this thing as ENVied::Configuration? prolly not threadsafe
|
data/spec/envied_spec.rb
CHANGED
@@ -13,6 +13,11 @@ describe ENVied do
|
|
13
13
|
self
|
14
14
|
end
|
15
15
|
|
16
|
+
def configure(options = {}, &block)
|
17
|
+
described_class.configure(options, &block)
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
16
21
|
def configured_with(hash = {})
|
17
22
|
described_class.configure do |env|
|
18
23
|
hash.each do |name, type|
|
@@ -68,13 +73,25 @@ describe ENVied do
|
|
68
73
|
end
|
69
74
|
|
70
75
|
describe 'variable with default' do
|
71
|
-
it
|
76
|
+
it "are disabled by default" do
|
72
77
|
configured_with(a: [:Integer, default: 1]).and_no_ENV
|
78
|
+
|
79
|
+
expect { ENVied.a }.to raise_error
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'can be a value' do
|
83
|
+
configure(enable_defaults: true) do
|
84
|
+
variable :a, :Integer, default: 1
|
85
|
+
end.and_no_ENV
|
86
|
+
|
73
87
|
expect(ENVied.a).to eq 1
|
74
88
|
end
|
75
89
|
|
76
90
|
it "can be anything callable" do
|
77
|
-
|
91
|
+
configure(enable_defaults: true) do
|
92
|
+
variable :a, :Integer, default: proc { 1 }
|
93
|
+
end.and_no_ENV
|
94
|
+
|
78
95
|
expect(ENVied.a).to eq 1
|
79
96
|
end
|
80
97
|
end
|