conf_conf 1.0.1 → 1.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: b97132adaed0144f56e5fd294aeff6346d2a9e6b
4
- data.tar.gz: e8a6488828ee9923f258a5b74d3e0329ede4fbcb
3
+ metadata.gz: 5ae8997ac5c6fb20fe8a764c3cdcc8b1ef08a07e
4
+ data.tar.gz: 0d96b60522f33f63984c9e1bda6b2aa106f5353e
5
5
  SHA512:
6
- metadata.gz: 5061abb3d321fc3cc58bd7d60a12d57cffcbb4b3a6b348fb54fa00d5c30d80aa12fa9ab1910ecdec122c8ff98656d2812e6b6250658b1bdebbaa7b7535dd6187
7
- data.tar.gz: c0dc60294413944698ba812d936d00a187ac922eba19f1285f54855a66d4a2fc35e41caa2f000ec73d724ede71ee989bbd3cb68939640aad27aa301e87278257
6
+ metadata.gz: 38d308e844b7cf90e7dbc58ca78465bde0cdbd6f4277193886520ccc48779d15aca22454ef9726206892f30fb02cc0129f2c86f92bebdd4eabaf8c064f0b1bd2
7
+ data.tar.gz: 5ddbb1345649fa3564c3abb98d7cfc0e2c8e791ca230ce25f2238d6f9e88dda5ccab3bafb73088e0aa8445c94a0c9b7e8ba690bd152a216ac52bdbde26ffb211
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 James Kassemi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -13,26 +13,17 @@ we can fail fast when there's a configuration problem.
13
13
 
14
14
  Add `gem 'conf_conf'` to your application's Gemfile.
15
15
 
16
- ## Rails
16
+ ## Usage
17
17
 
18
- Add a new initializer - if you use conf_conf.rb, as a matter
19
- of convention you shouldn't add ConfConf configuration blocks
20
- to other initializers.
18
+ Add a new initializer - if you use conf_conf.rb , as a matter of
19
+ convention you shouldn't add ConfConf configuration blocks to
20
+ other initializers.
21
21
 
22
22
  ```ruby
23
23
  # config/initializers/conf_conf.rb
24
- ConfConf.rails_configuration do
25
- # Sets Rails.configuration.secret_key, app fails to boot if not present
24
+ $configuration = ConfConf.configuration do
25
+ # Sets $configuration.secret_key, app fails to boot if not present
26
26
  config :secret_key
27
-
28
- # Sets Rails.configuration.public_key from ENV["public_key"], or uses the default if not available in ENV
29
- config :public_key, default: "XYZ123"
30
-
31
- # Sets Rails.configuration.admin to a boolean value of true or false, app fails to boot if not present
32
- config :admin, { |admin| admin ? true : false }
33
-
34
- # Sets Rails.configuration.public_key from ENV["PUBLIC_KEY_WITH_ALT_NAME"]
35
- config :public_key, from: "PUBLIC_KEY_WITH_ALT_NAME"
36
27
  end
37
28
  ```
38
29
 
@@ -48,3 +39,46 @@ In the case above, if SECRET_KEY is not present, then
48
39
  from conf_conf/lib/conf_conf.rb:42
49
40
  ...
50
41
  ```
42
+
43
+ ### Default Values
44
+
45
+ ```ruby
46
+ # Sets $configuration.public_key from ENV["PUBLIC_KEY"], or uses the default if not available in ENV
47
+ config :public_key, default: "XYZ123"
48
+ ```
49
+
50
+ ### Casting
51
+
52
+ You can adjust the value from the environment and typecast it or perform
53
+ additional validation by passing a block to `config`:
54
+
55
+ ```ruby
56
+ # Sets $configuration.admin to a boolean value of true or false based on truthiness of ENV key, app fails to boot if not present
57
+ config(:admin) { |admin|
58
+ admin ? true : false
59
+ }
60
+ ```
61
+
62
+ ### Varying Names
63
+
64
+ If you'd like to reference a configuration value with a different name, you can
65
+ use the `from` key as an option to `config` and pass it the name to expect from
66
+ the environment.
67
+
68
+ ```ruby
69
+ # Sets $configuration.public_key from ENV["PUBLIC_KEY_WITH_ALT_NAME"]
70
+ config :public_key, from: "PUBLIC_KEY_WITH_ALT_NAME"
71
+ ```
72
+
73
+ ## Rails.configuration
74
+
75
+ To assign directly to Rails.configuration instead of CONFCONF, you can
76
+ use `ConfConf.rails_configuration` method.
77
+
78
+ ```ruby
79
+ # config/initializers/conf_conf.rb
80
+ ConfConf.rails_configuration do
81
+ # Sets Rails.configuration.secret_key
82
+ config :secret_key
83
+ end
84
+ ```
data/conf_conf.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "conf_conf"
7
- s.version = "1.0.1"
7
+ s.version = "1.0.2"
8
8
  s.licenses = ["MIT"]
9
9
  s.authors = ["James Kassemi"]
10
10
  s.email = ["jkassemi@gmail.com"]
data/lib/conf_conf.rb CHANGED
@@ -1,12 +1,17 @@
1
+ require 'ostruct'
2
+
1
3
  Dir["tasks/**/*.rake"].each { |ext| load ext } if defined?(Rake)
2
4
 
3
5
  module ConfConf
4
6
  class MissingConfigurationValueError < StandardError; end;
5
7
 
6
8
  class << self
9
+ def configuration(&block)
10
+ OpenStruct.new(Configuration.new(&block).parsed_values)
11
+ end
12
+
7
13
  def rails_configuration(&block)
8
14
  configuration = Configuration.new
9
-
10
15
  configuration.run(block)
11
16
 
12
17
  configuration.parsed_values.each do |key, value|
@@ -18,8 +23,9 @@ module ConfConf
18
23
  class Configuration
19
24
  attr_reader :parsed_values
20
25
 
21
- def initialize
26
+ def initialize(&block)
22
27
  @parsed_values = {}
28
+ run(block) if block
23
29
  end
24
30
 
25
31
  def run(block)
@@ -3,51 +3,65 @@ require 'conf_conf'
3
3
  module Rails; end;
4
4
 
5
5
  describe ConfConf do
6
- let(:configuration){ double() }
7
- before { allow(Rails).to receive(:configuration).and_return(configuration) }
6
+ context "#configuration" do
7
+ it "sets a value from the environment" do
8
+ ENV["TEST_KEY"] = "hey"
8
9
 
9
- it "sets a value from the environment" do
10
- ENV["TEST_KEY"] = "hey"
11
- expect(configuration).to receive(:test_key=).with("hey")
10
+ configuration = ConfConf.configuration {
11
+ config :test_key
12
+ }
12
13
 
13
- ConfConf.rails_configuration do
14
- config :test_key
14
+ expect(configuration.test_key).to eq("hey")
15
15
  end
16
16
  end
17
17
 
18
- it "sets the default value when key not present" do
19
- expect(configuration).to receive(:key_not_present=).with("hey")
18
+ context "#rails_configuration" do
19
+ let(:configuration){ double() }
20
+ before { allow(Rails).to receive(:configuration).and_return(configuration) }
20
21
 
21
- ConfConf.rails_configuration do
22
- config :key_not_present, default: "hey"
22
+ it "sets a value from the environment" do
23
+ ENV["TEST_KEY"] = "hey"
24
+ expect(configuration).to receive(:test_key=).with("hey")
25
+
26
+ ConfConf.rails_configuration do
27
+ config :test_key
28
+ end
23
29
  end
24
- end
25
30
 
26
- it "sets value from specified environment key" do
27
- ENV["TEST_KEY"] = "hey"
28
- expect(configuration).to receive(:other_key=).with("hey")
31
+ it "sets the default value when key not present" do
32
+ expect(configuration).to receive(:key_not_present=).with("hey")
29
33
 
30
- ConfConf.rails_configuration do
31
- config :other_key, from: "TEST_KEY"
34
+ ConfConf.rails_configuration do
35
+ config :key_not_present, default: "hey"
36
+ end
32
37
  end
33
- end
34
38
 
35
- it "throws an exception when required key not present" do
36
- expect {
39
+ it "sets value from specified environment key" do
40
+ ENV["TEST_KEY"] = "hey"
41
+ expect(configuration).to receive(:other_key=).with("hey")
42
+
37
43
  ConfConf.rails_configuration do
38
- config :key_not_present
44
+ config :other_key, from: "TEST_KEY"
39
45
  end
40
- }.to raise_error(ConfConf::MissingConfigurationValueError)
41
- end
46
+ end
42
47
 
43
- it "evaluates a block to establish the actual configuration value" do
44
- ENV["INTEGER_VALUE"] = "2"
48
+ it "throws an exception when required key not present" do
49
+ expect {
50
+ ConfConf.rails_configuration do
51
+ config :key_not_present
52
+ end
53
+ }.to raise_error(ConfConf::MissingConfigurationValueError)
54
+ end
55
+
56
+ it "evaluates a block to establish the actual configuration value" do
57
+ ENV["INTEGER_VALUE"] = "2"
45
58
 
46
- expect(configuration).to receive(:integer_value=).with(2)
59
+ expect(configuration).to receive(:integer_value=).with(2)
47
60
 
48
- ConfConf.rails_configuration do
49
- config :integer_value do |value|
50
- value.to_i
61
+ ConfConf.rails_configuration do
62
+ config :integer_value do |value|
63
+ value.to_i
64
+ end
51
65
  end
52
66
  end
53
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conf_conf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Kassemi
@@ -32,6 +32,7 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - Gemfile
35
+ - LICENSE
35
36
  - README.md
36
37
  - conf_conf.gemspec
37
38
  - lib/conf_conf.rb