Skip to content

Commit

Permalink
Added settings sets functionality to Settings library
Browse files Browse the repository at this point in the history
  • Loading branch information
LocoDelAssembly committed Nov 18, 2014
1 parent dc85749 commit cfa7c1a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion config/environments/development.rb
Expand Up @@ -30,7 +30,7 @@
config.action_mailer.default_url_options = { :host => "localhost:3000" }

# Load local settings
# Settings.load_from_file(config, 'config/local_settings.yml')
Settings.load_from_file(config, 'config/local_settings.yml', :development) if File.exist?('config/local_settings.yml')

require 'taxonworks'
require 'taxonworks_autoload'
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Expand Up @@ -79,7 +79,7 @@
config.log_formatter = ::Logger::Formatter.new

# Load local settings
# Settings.load_from_file(config, 'config/local_settings.yml')
Settings.load_from_file(config, 'config/local_settings.yml', :production) if File.exist?('config/local_settings.yml')

require 'taxonworks'
end
18 changes: 11 additions & 7 deletions config/local_settings.yml.example
@@ -1,7 +1,11 @@
database_dumps_directory: '/home/jdoe/my-dumps'
exception_notification:
email_prefix: "[TW-Error] "
sender_address: "\"notifier\" <notifier@example.com>"
exception_recipients:
- exceptions@example.com
- root@example.com
development:
database_dumps_directory: "/home/jdoe/my-dumps"
production:
database_dumps_directory: "/mnt/backup-media/tw-dumps"
exception_notification:
email_prefix: "[TW-Error] "
sender_address: "\"notifier\" <notifier@example.com>"
exception_recipients:
- exceptions@example.com
- root@example.com
empty:
6 changes: 4 additions & 2 deletions lib/settings.rb
Expand Up @@ -19,8 +19,10 @@ def self.load_from_hash(config, hash)
load_database_dumps_directory(hash[:database_dumps_directory])
end

def self.load_from_file(config, path)
self.load_from_hash(config, symbolize_keys(YAML.load_file(path)))
def self.load_from_file(config, path, set_name)
hash = YAML.load_file(path)
raise "#{set_name} settings set not found" unless hash.keys.include?(set_name.to_s)
self.load_from_hash(config, symbolize_keys(hash[set_name.to_s] || { }))
end

def self.db_dumps_dir
Expand Down
18 changes: 12 additions & 6 deletions spec/files/settings/valid.yml
@@ -1,6 +1,12 @@
database_dumps_directory: 'tmp'
exception_notification:
email_prefix: "[TW-Error] "
sender_address: "\"notifier\" <notifier@example.com>"
exception_recipients:
- exceptions@example.com
foo:
database_dumps_directory: 'foo'
test:
database_dumps_directory: 'tmp'
exception_notification:
email_prefix: "[TW-Error] "
sender_address: "\"notifier\" <notifier@example.com>"
exception_recipients:
- exceptions@example.com
bar:
database_dumps_directory: 'bar'
empty:
20 changes: 14 additions & 6 deletions spec/lib/settings_spec.rb
Expand Up @@ -16,7 +16,6 @@

describe '::load_from_hash' do
let(:valid_config) { { exception_notification: valid_full_config[:exception_notification] } }
let(:empty_config) { { } }

describe 'exception_notification' do

Expand All @@ -29,7 +28,7 @@

it 'does not set up ExceptionNotification middleware when no settings is available' do
expect(rails_config.middleware).not_to receive(:use)
Settings.load_from_hash(rails_config, empty_config)
Settings.load_from_hash(rails_config, { })
end

end
Expand Down Expand Up @@ -107,18 +106,27 @@

describe '::load_from_file' do

it "calls ::load_from_hash with YAML file converted to hash" do
it "calls ::load_from_hash with the supplied settings set in the YAML file converted to hash" do
expect(Settings).to receive(:load_from_hash).with(rails_config, valid_full_config)
Settings.load_from_file(rails_config, 'spec/files/settings/valid.yml')
Settings.load_from_file(rails_config, 'spec/files/settings/valid.yml', :test)
end

it "calls ::load_from_hash with an empty hash when the settings set in the YAML file is empty" do
expect(Settings).to receive(:load_from_hash).with(rails_config, { })
Settings.load_from_file(rails_config, 'spec/files/settings/valid.yml', :empty)
end

it "throws error when file not exists" do
expect { Settings.load_from_file(rails_config, 'not_exists.yml') }.to raise_error
expect { Settings.load_from_file(rails_config, 'not_exists.yml', :test) }.to raise_error Errno::ENOENT
end

it "throws error when settings set is not present" do
expect { Settings.load_from_file(rails_config, 'spec/files/settings/valid.yml', :INVALID_SET_NAME) }.to raise_error(/.*INVALID_SET_NAME.*/)
end

it "throws error on syntax error" do
expect {
Settings.load_from_file(rails_config, 'spec/files/settings/syntax_error.yml')
Settings.load_from_file(rails_config, 'spec/files/settings/syntax_error.yml', :test)
}.to raise_error Psych::SyntaxError
end
end
Expand Down

0 comments on commit cfa7c1a

Please sign in to comment.