envied 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/envied.rb +32 -22
  4. data/spec/envied_spec.rb +3 -16
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36d17e2dd8294749d6092e79e10c5e4ba738e30e
4
- data.tar.gz: 20154eb0fc0d036ba2fd0e05016db9442304d944
3
+ metadata.gz: 4a2483497b0ea0cac789287167585eee37cfbd70
4
+ data.tar.gz: fa76d7207586ed263d2b8f1ca284b18a5711b41f
5
5
  SHA512:
6
- metadata.gz: 9a7b3f6e04cb7faf1704feff910ca6aa30b68ea416d1427de0b2a977477467c0da1791fd34f89ab823d999a5389d1e026ef04e9036bb9adb5e48f545d9aea224
7
- data.tar.gz: 5e513482e36a44737af281bf239f19b55f8b007f9e03cca22e4c632b5b08c6b6b49a55dea140b393b4745a142b3e28c9655ae72e12dae515d639a56b968d2735
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 (with arity 0, 1 or 2).
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: ->(env, variable){ env.force_ssl ? 443 : 80 }
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.1.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
- @variable = attribute_set[name]
47
- has_default = !!@variable.options[:default]
48
- var_value = env[name.to_s] || env[name.to_s.upcase]
49
- result[name] = var_value if var_value
50
- if !(result[name] || has_default)
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
- protected
66
- def flexible_arity(default)
67
- return default unless default.respond_to?(:call)
68
-
69
- case default.arity
70
- when 1
71
- ->(env, _){ default[env] }
72
- when 0
73
- ->(*){ default[] }
74
- else
75
- default
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(&block)
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
- (0..2).each do |arity|
77
- it "can be a proc with arity #{arity}" do
78
- default = begin
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: envied
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gert Goet