envied 0.0.3 → 0.1.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -2
  3. data/lib/envied.rb +20 -2
  4. data/spec/envied_spec.rb +27 -2
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79a82b4ddfab5f725abe511edb302a7f82e74a31
4
- data.tar.gz: b1627d78490f9ee58372b53c8699fecf572b36ef
3
+ metadata.gz: 36d17e2dd8294749d6092e79e10c5e4ba738e30e
4
+ data.tar.gz: 20154eb0fc0d036ba2fd0e05016db9442304d944
5
5
  SHA512:
6
- metadata.gz: 9275f7c89ccd8159d921aae002558fa7eaedff0831944481c80a18d5c320cfc81fad2e7e049a81c78c349e463f8c5d19cf4143b4f1e98247a389ef3f694722b5
7
- data.tar.gz: 0e98638933ffa8fdb57278ab0d6b1172ef62e6148eef582cf994727d70f62ab1faeedf931353a1bf38246cc85940979527bcdd79213b058297a9235f294cddc4
6
+ metadata.gz: 9a7b3f6e04cb7faf1704feff910ca6aa30b68ea416d1427de0b2a977477467c0da1791fd34f89ab823d999a5389d1e026ef04e9036bb9adb5e48f545d9aea224
7
+ data.tar.gz: 5e513482e36a44737af281bf239f19b55f8b007f9e03cca22e4c632b5b08c6b6b49a55dea140b393b4745a142b3e28c9655ae72e12dae515d639a56b968d2735
data/README.md CHANGED
@@ -41,10 +41,36 @@ 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 => 1
45
- ENVied.force_ssl => false
44
+ ENVied.port # => 1
45
+ ENVied.force_ssl # => false
46
46
  ```
47
47
 
48
+ ## Configuration
49
+
50
+ ### Types
51
+
52
+ The following types are supported:
53
+
54
+ * `:String` (implied)
55
+ * `:Boolean` (e.g. '0'/'1', 'f'/'t', 'false'/'true', 'off'/'on', 'yes','no' for resp. true or false)
56
+ * `:Integer`
57
+ * `:Symbol`
58
+ * `:Date` (e.g. '2014-3-26')
59
+ * `:Time` (e.g. '14:00')
60
+
61
+ ### Defaults
62
+
63
+ Variables can have defaults. It can be value or a proc (with arity 0, 1 or 2).
64
+
65
+ ```ruby
66
+ ENVied.configure do |env|
67
+ env.variable :port, :Integer, default: ->(env, variable){ env.force_ssl ? 443 : 80 }
68
+ env.variable :force_ssl, :Boolean, default: true
69
+ end
70
+ ```
71
+
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.
73
+
48
74
  ## Installation
49
75
 
50
76
  Add this line to your application's Gemfile:
@@ -1,5 +1,5 @@
1
1
  class ENVied
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  module Configurable
4
4
  require 'virtus'
5
5
 
@@ -44,7 +44,10 @@ class ENVied
44
44
  def parse_env(env)
45
45
  atts = attribute_set.map(&:name).each_with_object({}) do |name, result|
46
46
  @variable = attribute_set[name]
47
- unless result[name] = env[name.to_s] || env[name.to_s.upcase]
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)
48
51
  raise VariableMissingError, @variable
49
52
  end
50
53
  end
@@ -55,8 +58,23 @@ class ENVied
55
58
  end
56
59
 
57
60
  def variable(name, type = :String, options = {})
61
+ options[:default] &&= flexible_arity(options[:default])
58
62
  attribute name, type, { strict: true }.merge(options)
59
63
  end
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
76
+ end
77
+ end
60
78
  end
61
79
  end
62
80
 
@@ -16,7 +16,7 @@ describe ENVied do
16
16
  def configured_with(hash = {})
17
17
  described_class.configure do |env|
18
18
  hash.each do |name, type|
19
- env.variable(name, type)
19
+ env.variable(name, *type)
20
20
  end
21
21
  end
22
22
  self
@@ -50,7 +50,7 @@ describe ENVied do
50
50
  }.to raise_error(ENVied::Configurable::VariableMissingError)
51
51
  end
52
52
 
53
- it 'raises EnvMissing when interacted with and configured variable not present in ENV' do
53
+ it 'raises EnvMissing when interacted with' do
54
54
  expect {
55
55
  ENVied.any_missing_method
56
56
  }.to raise_error(ENVied::Configurable::VariableMissingError)
@@ -66,5 +66,30 @@ describe ENVied do
66
66
  }.to raise_error(ENVied::Configurable::VariableTypeError)
67
67
  end
68
68
  end
69
+
70
+ describe 'variable with default' do
71
+ it 'can be a value' do
72
+ configured_with(a: [:Integer, default: 1]).and_no_ENV
73
+ expect(ENVied.a).to eq 1
74
+ end
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
92
+ end
93
+ end
69
94
  end
70
95
  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.0.3
4
+ version: 0.1.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-03-09 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubyforge_project:
106
- rubygems_version: 2.2.0
106
+ rubygems_version: 2.2.2
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: ENV on EPO