Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.bundle/
Gemfile.lock
vendor/
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
```

Expand All @@ -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
```
2 changes: 1 addition & 1 deletion conf_conf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 15 additions & 1 deletion lib/conf_conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
33 changes: 32 additions & 1 deletion spec/conf_conf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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