conf_conf 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63aae1d8878cbe6e4d5f09c34dcc18ecb499e2f9
4
+ data.tar.gz: f6d2bda882b8536b8cae8cd28e2675e3341b63fc
5
+ SHA512:
6
+ metadata.gz: 9639ee0edcc66ff8811851e52f68885b473eeb6f67042b0859a6e6af27b6f4e036b54be94e1e4061684113f27f82952f7e5105d0e714a225cba1fc90dd43c7e5
7
+ data.tar.gz: 6a89fdba345053101e40e54585fbfaf000479132656438c24df9bf0f676d940357989cef74b0932796f95c52c99d314cd71a231242374b76f795b3b0dc49ba0e
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # ConfConf
2
+
3
+ Twelve factor applications pull configuration values from the
4
+ environment. These variables should be verified at application
5
+ boot to prevent exceptions and unexpected behavior during
6
+ run time.
7
+
8
+ ConfConf is a simple pattern and utility for verifying the
9
+ correctness of the environment variables at application boot so
10
+ we can fail fast when there's a configuration problem.
11
+
12
+ ## Installation
13
+
14
+ Add `gem 'conf_conf'` to your application's Gemfile.
15
+
16
+ ## Rails
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.
21
+
22
+ ```ruby
23
+ # config/initializers/conf_conf.rb
24
+ ConfConf.rails_configuration do
25
+ # Sets Rails.configuration.secret_key, app fails to boot if not present
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
+ end
34
+ ```
35
+
36
+ In the case above, if SECRET_KEY is not present, then
37
+ `ConfConf::MissingConfigurationValueError` is raised:
38
+
39
+ ```
40
+ $ bin/rails s
41
+ ...
42
+ Exiting
43
+ conf_conf/lib/conf_conf.rb:50:in `default_value': Please set SECRET_KEY or supply a default value
44
+ (ConfConf::MissingConfigurationValueError)
45
+ from conf_conf/lib/conf_conf.rb:42
46
+ ...
47
+ ```
data/conf_conf.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "conf_conf"
7
+ s.version = "1.0.0"
8
+ s.licenses = ["MIT"]
9
+ s.authors = ["James Kassemi"]
10
+ s.email = ["jkassemi@gmail.com"]
11
+ s.homepage = "https://github.com/jkassemi/conf_conf"
12
+ s.description = "Verify correctness of environment variables"
13
+ s.summary = "A simple pattern and utility for verifying the correctness of the environment variables at application boot so we can fail fast when there's a configuration problem."
14
+ s.files = `git ls-files`.split($/)
15
+
16
+ s.add_development_dependency 'rspec', '~> 3.0'
17
+ end
data/lib/conf_conf.rb ADDED
@@ -0,0 +1,62 @@
1
+ Dir["tasks/**/*.rake"].each { |ext| load ext } if defined?(Rake)
2
+
3
+ module ConfConf
4
+ class MissingConfigurationValueError < StandardError; end;
5
+
6
+ class << self
7
+ def rails_configuration(&block)
8
+ configuration = Configuration.new
9
+
10
+ configuration.run(block)
11
+
12
+ configuration.parsed_values.each do |key, value|
13
+ Rails.configuration.send("#{key}=", value)
14
+ end
15
+ end
16
+ end
17
+
18
+ class Configuration
19
+ attr_reader :parsed_values
20
+
21
+ def initialize
22
+ @parsed_values = {}
23
+ end
24
+
25
+ def run(block)
26
+ instance_eval(&block)
27
+ end
28
+
29
+ def config(key, options={})
30
+ value = Reference.new(key, options).value
31
+
32
+ if block_given?
33
+ value = yield(value)
34
+ end
35
+
36
+ @parsed_values[key] = value
37
+ end
38
+ end
39
+
40
+ class Reference < Struct.new(:key, :options)
41
+ def value
42
+ environment_value || default_value
43
+ end
44
+
45
+ private
46
+ def default_value
47
+ if options.has_key? :default
48
+ options[:default]
49
+ else
50
+ raise MissingConfigurationValueError.new("Please set #{environment_key} or supply a default value")
51
+ end
52
+ end
53
+
54
+ def environment_value
55
+ ENV[environment_key]
56
+ end
57
+
58
+ def environment_key
59
+ key.to_s.upcase
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,45 @@
1
+ require 'conf_conf'
2
+
3
+ module Rails; end;
4
+
5
+ describe ConfConf do
6
+ let(:configuration){ double() }
7
+ before { allow(Rails).to receive(:configuration).and_return(configuration) }
8
+
9
+ it "sets a value from the environment" do
10
+ ENV["TEST_KEY"] = "hey"
11
+ expect(configuration).to receive(:test_key=).with("hey")
12
+
13
+ ConfConf.rails_configuration do
14
+ config :test_key
15
+ end
16
+ end
17
+
18
+ it "sets the default value when key not present" do
19
+ expect(configuration).to receive(:key_not_present=).with("hey")
20
+
21
+ ConfConf.rails_configuration do
22
+ config :key_not_present, default: "hey"
23
+ end
24
+ end
25
+
26
+ it "throws an exception when required key not present" do
27
+ expect {
28
+ ConfConf.rails_configuration do
29
+ config :key_not_present
30
+ end
31
+ }.to raise_error(ConfConf::MissingConfigurationValueError)
32
+ end
33
+
34
+ it "evaluates a block to establish the actual configuration value" do
35
+ ENV["INTEGER_VALUE"] = "2"
36
+
37
+ expect(configuration).to receive(:integer_value=).with(2)
38
+
39
+ ConfConf.rails_configuration do
40
+ config :integer_value do |value|
41
+ value.to_i
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,78 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, make a
10
+ # separate helper file that requires this one and then use it only in the specs
11
+ # that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # The settings below are suggested to provide a good initial experience
19
+ # with RSpec, but feel free to customize to your heart's content.
20
+ =begin
21
+ # These two settings work together to allow you to limit a spec run
22
+ # to individual examples or groups you care about by tagging them with
23
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
24
+ # get run.
25
+ config.filter_run :focus
26
+ config.run_all_when_everything_filtered = true
27
+
28
+ # Many RSpec users commonly either run the entire suite or an individual
29
+ # file, and it's useful to allow more verbose output when running an
30
+ # individual spec file.
31
+ if config.files_to_run.one?
32
+ # Use the documentation formatter for detailed output,
33
+ # unless a formatter has already been configured
34
+ # (e.g. via a command-line flag).
35
+ config.default_formatter = 'doc'
36
+ end
37
+
38
+ # Print the 10 slowest examples and example groups at the
39
+ # end of the spec run, to help surface which specs are running
40
+ # particularly slow.
41
+ config.profile_examples = 10
42
+
43
+ # Run specs in random order to surface order dependencies. If you find an
44
+ # order dependency and want to debug it, you can fix the order by providing
45
+ # the seed, which is printed after each run.
46
+ # --seed 1234
47
+ config.order = :random
48
+
49
+ # Seed global randomization in this process using the `--seed` CLI option.
50
+ # Setting this allows you to use `--seed` to deterministically reproduce
51
+ # test failures related to randomization by passing the same `--seed` value
52
+ # as the one that triggered the failure.
53
+ Kernel.srand config.seed
54
+
55
+ # rspec-expectations config goes here. You can use an alternate
56
+ # assertion/expectation library such as wrong or the stdlib/minitest
57
+ # assertions if you prefer.
58
+ config.expect_with :rspec do |expectations|
59
+ # Enable only the newer, non-monkey-patching expect syntax.
60
+ # For more details, see:
61
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
62
+ expectations.syntax = :expect
63
+ end
64
+
65
+ # rspec-mocks config goes here. You can use an alternate test double
66
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
67
+ config.mock_with :rspec do |mocks|
68
+ # Enable only the newer, non-monkey-patching expect syntax.
69
+ # For more details, see:
70
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ mocks.syntax = :expect
72
+
73
+ # Prevents you from mocking or stubbing a method that does not exist on
74
+ # a real object. This is generally recommended.
75
+ mocks.verify_partial_doubles = true
76
+ end
77
+ =end
78
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conf_conf
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - James Kassemi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Verify correctness of environment variables
28
+ email:
29
+ - jkassemi@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - README.md
36
+ - conf_conf.gemspec
37
+ - lib/conf_conf.rb
38
+ - spec/conf_conf_spec.rb
39
+ - spec/spec_helper.rb
40
+ homepage: https://github.com/jkassemi/conf_conf
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.2.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: A simple pattern and utility for verifying the correctness of the environment
64
+ variables at application boot so we can fail fast when there's a configuration problem.
65
+ test_files: []