improved configuration utility
This commit is contained in:
@ -1,3 +1,3 @@
|
||||
YAML.load_file(Rails.root.join('config', 'action_mailer.yml'))[Rails.env].each do |key, value|
|
||||
CodeOcean::Config.new(:action_mailer).read.each do |key, value|
|
||||
CodeOcean::Application.config.action_mailer.send(:"#{key}=", value.respond_to?(:symbolize_keys) ? value.symbolize_keys : value)
|
||||
end
|
||||
|
@ -1,3 +1,3 @@
|
||||
if CodeOcean::Application.config.action_mailer.delivery_method == :sendmail
|
||||
CodeOcean::Application.config.action_mailer.sendmail_settings = CodeOcean::Config.new('sendmail').read
|
||||
CodeOcean::Application.config.action_mailer.sendmail_settings = CodeOcean::Config.new(:sendmail).read
|
||||
end
|
||||
|
@ -1,3 +1,3 @@
|
||||
if CodeOcean::Application.config.action_mailer.delivery_method == :smtp
|
||||
CodeOcean::Application.config.action_mailer.sendmail_settings = CodeOcean::Config.new('smtp').read
|
||||
CodeOcean::Application.config.action_mailer.sendmail_settings = CodeOcean::Config.new(:smtp).read
|
||||
end
|
||||
|
@ -4,11 +4,17 @@ module CodeOcean
|
||||
@filename = filename
|
||||
end
|
||||
|
||||
def read
|
||||
path = Rails.root.join('config', "#{@filename}.yml")
|
||||
if File.exists?(path)
|
||||
YAML.load_file(path)[Rails.env].symbolize_keys
|
||||
def read(options = {})
|
||||
path = Rails.root.join('config', "#{@filename}.yml#{options[:erb] ? '.erb' : ''}")
|
||||
if ::File.exists?(path)
|
||||
content = options[:erb] ? YAML.load(ERB.new(::File.new(path, 'r').read).result) : YAML.load_file(path)
|
||||
content[Rails.env].with_indifferent_access
|
||||
else
|
||||
raise Error.new("Configuration file not found: #{path}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Config::Error < RuntimeError
|
||||
end
|
||||
end
|
||||
|
@ -1,5 +1,4 @@
|
||||
class DockerClient
|
||||
CONFIG_PATH = Rails.root.join('config', 'docker.yml.erb')
|
||||
CONTAINER_WORKSPACE_PATH = '/workspace'
|
||||
LOCAL_WORKSPACE_ROOT = Rails.root.join('tmp', 'files', Rails.env)
|
||||
|
||||
@ -29,7 +28,7 @@ class DockerClient
|
||||
private :command_substitutions
|
||||
|
||||
def self.config
|
||||
YAML.load(ERB.new(File.new(CONFIG_PATH, 'r').read).result)[Rails.env].with_indifferent_access
|
||||
@config ||= CodeOcean::Config.new(:docker).read(erb: true)
|
||||
end
|
||||
|
||||
def copy_file_to_workspace(options = {})
|
||||
|
48
spec/lib/code_ocean/config_spec.rb
Normal file
48
spec/lib/code_ocean/config_spec.rb
Normal file
@ -0,0 +1,48 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe CodeOcean::Config do
|
||||
describe '#read' do
|
||||
let(:content) { {foo: 'bar'} }
|
||||
let(:filename) { :foo }
|
||||
|
||||
context 'with a .yml file' do
|
||||
let(:path) { Rails.root.join('config', "#{filename}.yml") }
|
||||
let(:read) { CodeOcean::Config.new(filename).read }
|
||||
|
||||
context 'when the file is present' do
|
||||
before(:each) { File.write(path, {Rails.env => content}.to_yaml) }
|
||||
after(:each) { FileUtils.rm(path) }
|
||||
|
||||
it 'returns the environment-specific content' do
|
||||
expect(read).to eq(content.with_indifferent_access)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the file is not present' do
|
||||
it 'raises an error' do
|
||||
expect { read }.to raise_error(CodeOcean::Config::Error)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a .yml.erb file' do
|
||||
let(:path) { Rails.root.join('config', "#{filename}.yml.erb") }
|
||||
let(:read) { CodeOcean::Config.new(filename).read(erb: true) }
|
||||
|
||||
context 'when the file is present' do
|
||||
before(:each) { File.write(path, {Rails.env => content}.to_yaml) }
|
||||
after(:each) { FileUtils.rm(path) }
|
||||
|
||||
it 'returns the environment-specific content' do
|
||||
expect(read).to eq(content.with_indifferent_access)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the file is not present' do
|
||||
it 'raises an error' do
|
||||
expect { read }.to raise_error(CodeOcean::Config::Error)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user