diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a8d370 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.bundle/ +Gemfile.lock +vendor/ diff --git a/README.md b/README.md index cc90bf2..8bb1e69 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ A utility for verifying the correctness of environment variables at application ## Add ConfConf to your Rails/Ruby Project - Add `gem 'conf_conf'` to your application's Gemfile and `bundle install` Better yet, go to https://rubygems.org/gems/conf_conf and configure a specific version requirement. @@ -40,8 +39,8 @@ config :public_key, default: "XYZ123" You can adjust the value from the environment and typecast it or perform additional validation by passing a block to `config`: ```ruby -config(:admin) { |admin| - admin ? true : false +config(:admin) { |admin| + admin ? true : false } ``` @@ -64,3 +63,46 @@ ConfConf.rails_configuration do config :secret_key end ``` + +### Default Values + +```ruby +# Sets $configuration.public_key from ENV["PUBLIC_KEY"], or uses the default if not available in ENV +config :public_key, default: "XYZ123" +``` + +### Casting + +You can adjust the value from the environment and typecast it or perform +additional validation by passing a block to `config`: + +```ruby +# Sets $configuration.admin to a boolean value of true or false based on truthiness of ENV key, app fails to boot if not present +config(:admin) { |admin| + admin ? true : false +} +``` + +### Varying Names + +If you'd like to reference a configuration value with a different name, you can +use the `from` key as an option to `config` and pass it the name to expect from +the environment. + +```ruby +# Sets $configuration.public_key from ENV["PUBLIC_KEY_WITH_ALT_NAME"] +config :public_key, from: "PUBLIC_KEY_WITH_ALT_NAME" +``` + +## Rails.configuration + +To assign directly to Rails.configuration instead of CONFCONF, you can +use `ConfConf.rails_configuration` method. + +```ruby +# config/initializers/conf_conf.rb +ConfConf.rails_configuration do + # Sets Rails.configuration.secret_key + config :secret_key +end +``` diff --git a/conf_conf.gemspec b/conf_conf.gemspec index d59c73d..5e4e962 100644 --- a/conf_conf.gemspec +++ b/conf_conf.gemspec @@ -6,7 +6,7 @@ Gem::Specification.new do |s| s.name = "conf_conf" s.version = "1.0.2" s.licenses = ["MIT"] - s.authors = ["James Kassemi"] + s.authors = ["James Kassemi"] s.email = ["jkassemi@gmail.com"] s.homepage = "https://github.com/jkassemi/conf_conf" s.description = "Verify correctness of environment variables" diff --git a/lib/conf_conf.rb b/lib/conf_conf.rb index 53a434c..4a1fb4e 100644 --- a/lib/conf_conf.rb +++ b/lib/conf_conf.rb @@ -6,6 +6,8 @@ module ConfConf class MissingConfigurationValueError < StandardError; end; class << self + attr_accessor :log_config + def configuration(&block) OpenStruct.new(Configuration.new(&block).parsed_values) end @@ -17,6 +19,8 @@ def rails_configuration(&block) configuration.parsed_values.each do |key, value| Rails.configuration.send("#{key}=", value) end + + ConfigLogger.new.dump_config configuration if log_config end end @@ -46,7 +50,7 @@ def config(key, options={}) class Reference < Struct.new(:key, :options) def value environment_value || default_value - end + end private def default_value @@ -65,4 +69,14 @@ def environment_key options[:from] || key.to_s.upcase end end + + class ConfigLogger + def dump_config(configuration) + Rails.logger.info "Application Configuration\n\n#{format(configuration)}" + end + + def format(configuration) + configuration.parsed_values.collect { |pair| "#{pair[0]}: #{pair[1]}" }.join("\n") + end + end end diff --git a/spec/conf_conf_spec.rb b/spec/conf_conf_spec.rb index 5ebf878..85c3250 100644 --- a/spec/conf_conf_spec.rb +++ b/spec/conf_conf_spec.rb @@ -8,7 +8,7 @@ module Rails; end; ENV["TEST_KEY"] = "hey" configuration = ConfConf.configuration { - config :test_key + config :test_key } expect(configuration.test_key).to eq("hey") @@ -64,5 +64,36 @@ module Rails; end; end end end + + it "logs the configuration if told to" do + logger = double() + allow(logger).to receive(:info) + allow(Rails).to receive(:logger).and_return logger + expect(configuration).to receive(:integer_value=).with(2) + + ConfConf.log_config = true + + ConfConf.rails_configuration do + config :integer_value do |value| + value.to_i + end + end + end + + it "logs the configuration if told to" do + logger = double() + expect(logger).to receive(:info) + expect(Rails).to receive(:logger).and_return logger + expect(configuration).to receive(:integer_value=).with(2) + + + ConfConf.log_config = true + + ConfConf.rails_configuration do + config :integer_value do |value| + value.to_i + end + end + end end end