envied 0.1.0 → 0.2.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 +2 -2
- data/lib/envied.rb +32 -22
- data/spec/envied_spec.rb +3 -16
- 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: 4a2483497b0ea0cac789287167585eee37cfbd70
|
4
|
+
data.tar.gz: fa76d7207586ed263d2b8f1ca284b18a5711b41f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c484a2e67953278b2d8ac7b50ccbc849a4b3eb22e4ec33715194fe42b58ab968cec2a808e6d5ff85d8797cbea3bb299770835c9db71cf9d9337c94e219afe475
|
7
|
+
data.tar.gz: 16b0ab7de0f0b6185836fefe0ee82b4a179493be2ab702721f4e25ea2edf8e5a1f69e45b4731960d5799f75e8f0686091cd8756e158e7bcfedce3a83bc15b671
|
data/README.md
CHANGED
@@ -60,11 +60,11 @@ The following types are supported:
|
|
60
60
|
|
61
61
|
### Defaults
|
62
62
|
|
63
|
-
Variables can have defaults. It can be value or a proc
|
63
|
+
Variables can have defaults. It can be a value or a proc.
|
64
64
|
|
65
65
|
```ruby
|
66
66
|
ENVied.configure do |env|
|
67
|
-
env.variable :port, :Integer, default:
|
67
|
+
env.variable :port, :Integer, default: proc {|env, variable| env.force_ssl ? 443 : 80 }
|
68
68
|
env.variable :force_ssl, :Boolean, default: true
|
69
69
|
end
|
70
70
|
```
|
data/lib/envied.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class ENVied
|
2
|
-
VERSION = "0.
|
2
|
+
VERSION = "0.2.0"
|
3
3
|
module Configurable
|
4
4
|
require 'virtus'
|
5
5
|
|
@@ -29,6 +29,7 @@ class ENVied
|
|
29
29
|
def self.included(base)
|
30
30
|
base.class_eval do
|
31
31
|
include Virtus.model
|
32
|
+
|
32
33
|
end
|
33
34
|
base.extend ClassMethods
|
34
35
|
end
|
@@ -43,37 +44,44 @@ class ENVied
|
|
43
44
|
# @param env [Hash] the env
|
44
45
|
def parse_env(env)
|
45
46
|
atts = attribute_set.map(&:name).each_with_object({}) do |name, result|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
raise VariableMissingError, @variable
|
47
|
+
variable = attribute_set[name]
|
48
|
+
default = variable.options[:default]
|
49
|
+
value = env[name.to_s] || env[name.to_s.upcase]
|
50
|
+
if !(value || default)
|
51
|
+
raise VariableMissingError, variable
|
52
52
|
end
|
53
|
+
|
54
|
+
try_coercion(variable, value, default)
|
55
|
+
result[name] = value if value
|
53
56
|
end
|
54
57
|
|
55
58
|
new(atts)
|
56
|
-
rescue Virtus::CoercionError => e
|
57
|
-
raise VariableTypeError, @variable
|
58
59
|
end
|
59
60
|
|
60
61
|
def variable(name, type = :String, options = {})
|
61
|
-
options[:default] &&= flexible_arity(options[:default])
|
62
62
|
attribute name, type, { strict: true }.merge(options)
|
63
63
|
end
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
+
private
|
75
|
+
def try_coercion(variable, value, default)
|
76
|
+
value ||= begin
|
77
|
+
default unless default.respond_to?(:call)
|
76
78
|
end
|
79
|
+
return unless value
|
80
|
+
@variable = variable
|
81
|
+
|
82
|
+
variable.coerce(value)
|
83
|
+
rescue Virtus::CoercionError => e
|
84
|
+
raise VariableTypeError.new(@variable)
|
77
85
|
end
|
78
86
|
end
|
79
87
|
end
|
@@ -83,7 +91,9 @@ class ENVied
|
|
83
91
|
end
|
84
92
|
|
85
93
|
def self.configure(&block)
|
86
|
-
@configuration = Class.new { include Configurable }.tap
|
94
|
+
@configuration = Class.new { include Configurable }.tap do |k|
|
95
|
+
k.instance_eval(&block)
|
96
|
+
end
|
87
97
|
# or define this thing as ENVied::Configuration? prolly not threadsafe
|
88
98
|
ensure
|
89
99
|
@instance = nil
|
data/spec/envied_spec.rb
CHANGED
@@ -73,22 +73,9 @@ describe ENVied do
|
|
73
73
|
expect(ENVied.a).to eq 1
|
74
74
|
end
|
75
75
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
case arity
|
80
|
-
when 2
|
81
|
-
->(a,b){ 1 }
|
82
|
-
when 1
|
83
|
-
->(a){ 1 }
|
84
|
-
when 0
|
85
|
-
->(){ 1 }
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
configured_with(a: [:Integer, default: default]).and_no_ENV
|
90
|
-
expect(ENVied.a).to eq 1
|
91
|
-
end
|
76
|
+
it "can be anything callable" do
|
77
|
+
configured_with(a: [:Integer, default: proc { 1 }]).and_no_ENV
|
78
|
+
expect(ENVied.a).to eq 1
|
92
79
|
end
|
93
80
|
end
|
94
81
|
end
|