configvar 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 37d5bee26a68074ec2e63a8e194291aeac3c68cc
4
- data.tar.gz: 3110ae9bf7e441707e9ec653c9dddd69466afdee
3
+ metadata.gz: 2db7f4b9d6800b17346be3aa65e6d21425b06cc4
4
+ data.tar.gz: b7d4b89e25feaee02447fb8015c0ca9ac06cd8f6
5
5
  SHA512:
6
- metadata.gz: e12a77bb4ce46bd1acb00786c607fd5864853ecca90d305c0aa5af115c494a5c75d11000db603a7f5df9b545ffd98880fbc5f6f011de7099a65f161d7ed9a7ba
7
- data.tar.gz: d2cf44ff80a9cde9d07bfefa626c4dab06a2384a01ee4cdf66888bee72fec63d2d66ca8eedc8b76eb2a1974f910394b3da479e7309509bcd5ab4fe7c82667c90
6
+ metadata.gz: 7b0915605672ce36a0c273463ae90ede1974d40b82674bf67913ca2f73ffef717d13e1caf9ec61294b823b560f8075f05be40b2d1cd555c879f35ac61c664aac
7
+ data.tar.gz: a9d04070a911fd5e8ab899a3f0053260ec9ddf869fff63df463b94eaba807cdf0a1d2b6452c469c882265682130c8bba65946450ce915c6cf58c058744922e39
@@ -0,0 +1 @@
1
+ pkg/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- configvar (0.0.1)
4
+ configvar (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build Status](https://magnum.travis-ci.com/heroku/configvar.svg?token=uNxDxTYyzRaxPpJGQ5yq)](https://magnum.travis-ci.com/heroku/configvar)
1
+ [![Build Status](https://travis-ci.org/heroku/configvar.svg?branch=master)](https://travis-ci.org/heroku/configvar)
2
2
 
3
3
  # ConfigVar
4
4
 
@@ -52,10 +52,8 @@ When simple string, integer and boolean values aren't sufficient you can
52
52
  provide a block to process values from the environment according to your
53
53
  needs. For example, if you want to load a required integer that must always
54
54
  be 0 or greater you can provide a custom block to do the required validation.
55
- It must take the environment to load values from and return a `Hash` of values
56
- to include in the loaded configuration. The block should raise a
57
- `ConfigVar::ConfigError` or `ArgumentError` exception if a required value is
58
- missing or if any loaded value is invalid, respectively.
55
+ It must take the environment to load values from and return a value to include
56
+ in the loaded configuration.
59
57
 
60
58
  ```ruby
61
59
  config = ConfigVar.define do
@@ -65,7 +63,7 @@ config = ConfigVar.define do
65
63
  if value < 0
66
64
  raise ArgumentError.new("#{value} for AGE must be a 0 or greater")
67
65
  end
68
- {age: value}
66
+ value
69
67
  else
70
68
  raise ConfigVar::ConfigError.new(name)
71
69
  end
@@ -73,10 +71,10 @@ config = ConfigVar.define do
73
71
  end
74
72
  ```
75
73
 
76
- Custom functions can return multiple key/value pairs and all of them will be
77
- included in the loaded configuration. For example, if you want to extract
78
- parts of a URL and make them available as individual configuration variables
79
- you can provide a custom block to do so:
74
+ Custom functions can return multiple key/value pairs in a `Hash` and all of
75
+ them will be included in the loaded configuration. For example, if you want
76
+ to extract parts of a URL and make them available as individual configuration
77
+ variables you can provide a custom block to do so:
80
78
 
81
79
  ```ruby
82
80
  config = ConfigVar.define do
@@ -60,9 +60,10 @@ module ConfigVar
60
60
 
61
61
  # Define a required custom config var. The block must take the
62
62
  # environment as a parameter, load and process values from the it, and
63
- # return a hash that will be merged into the collection of all config
64
- # vars. If a required value is not found in the environment the block
65
- # must raise a ConfigVar::ConfigError exception.
63
+ # return either a single value to bind to the name of the configuration
64
+ # variable or a `Hash` that will be merged into the collection of all
65
+ # config vars. If a required value is not found in the environment the
66
+ # block must raise a ConfigVar::ConfigError exception.
66
67
  def required_custom(name, &blk)
67
68
  define_config(name, &blk)
68
69
  end
@@ -100,10 +101,11 @@ module ConfigVar
100
101
  end
101
102
  end
102
103
 
103
- # Define a required custom config var. The block must take the
104
+ # Define an optional custom config var. The block must take the
104
105
  # environment as a parameter, load and process values from the it, and
105
- # return a hash that will be merged into the collection of all config
106
- # vars.
106
+ # return either a single value to bind to the name of the configuration
107
+ # variable or a `Hash` that will be merged into the collection of all
108
+ # config vars.
107
109
  def optional_custom(name, &blk)
108
110
  define_config(name, &blk)
109
111
  end
@@ -135,7 +137,14 @@ module ConfigVar
135
137
  if @definitions.has_key?(name)
136
138
  raise ConfigError.new("#{name.to_s.upcase} is already registered")
137
139
  end
138
- @definitions[name] = blk
140
+ @definitions[name] = Proc.new do |env|
141
+ value = yield env
142
+ if value.kind_of?(Hash)
143
+ value
144
+ else
145
+ {name => value}
146
+ end
147
+ end
139
148
  end
140
149
  end
141
150
  end
@@ -1,3 +1,3 @@
1
1
  module ConfigVar
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -112,6 +112,17 @@ class ContextTest < Minitest::Test
112
112
  assert_equal(42, context.age)
113
113
  end
114
114
 
115
+ # Context.reload associates a single value returned from the custom block
116
+ # with the name specified in the configuration definition.
117
+ def test_reload_optional_custom_with_single_value
118
+ context = ConfigVar::Context.new
119
+ context.required_custom :name do |env|
120
+ 'Bob'
121
+ end
122
+ context.reload({})
123
+ assert_equal('Bob', context.name)
124
+ end
125
+
115
126
  # Context.reload loads optional string values.
116
127
  def test_reload_optional_string
117
128
  context = ConfigVar::Context.new
@@ -195,6 +206,17 @@ class ContextTest < Minitest::Test
195
206
  assert_equal(42, context.age)
196
207
  end
197
208
 
209
+ # Context.reload associates a single value returned from the custom block
210
+ # with the name specified in the configuration definition.
211
+ def test_reload_optional_custom_with_single_value
212
+ context = ConfigVar::Context.new
213
+ context.optional_custom :name do |env|
214
+ 'Bob'
215
+ end
216
+ context.reload({})
217
+ assert_equal('Bob', context.name)
218
+ end
219
+
198
220
  # Context.reload reinitialized loaded values from the provided environment.
199
221
  def test_reload
200
222
  context = ConfigVar::Context.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configvar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jkakar
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-16 00:00:00.000000000 Z
12
+ date: 2015-02-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -61,6 +61,7 @@ executables: []
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
+ - ".gitignore"
64
65
  - ".travis.yml"
65
66
  - Gemfile
66
67
  - Gemfile.lock