This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
217 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'shenanigans/hash/to_ostruct' | ||
require 'deep_merge' | ||
|
||
module Shanty | ||
# Public: Configuration class for shanty | ||
class Config | ||
CONFIG_FILE = '.shanty.yml' | ||
|
||
def initialize(root, environment) | ||
@root = root | ||
@environment = environment | ||
end | ||
|
||
def merge!(new_config) | ||
@config = config_hash.clone.deep_merge(new_config) | ||
end | ||
|
||
def [](key) | ||
to_ostruct[key] | ||
end | ||
|
||
def method_missing(m) | ||
os_resp = to_ostruct.send(m) | ||
return os_resp unless os_resp.nil? | ||
OpenStruct.new | ||
end | ||
|
||
def respond_to?(method_sym, include_private = false) | ||
to_ostruct.send.respond_to?(method_sym, include_private) | ||
end | ||
|
||
def to_ostruct | ||
config_hash.to_ostruct | ||
end | ||
|
||
private | ||
|
||
def config_hash | ||
return @config unless @config.nil? | ||
return @config = {} unless File.exist?(config_path) | ||
config = YAML.load_file(config_path) || {} | ||
@config = config[@environment] || {} | ||
rescue RuntimeError | ||
{} | ||
end | ||
|
||
def config_path | ||
"#{@root}/#{CONFIG_FILE}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'shanty/config' | ||
require 'shenanigans/hash/to_ostruct' | ||
require 'deep_merge' | ||
|
||
# All classes referenced belong to the shanty project | ||
module Shanty | ||
RSpec.describe(Config) do | ||
subject { Config.new('nic', 'test') } | ||
|
||
let(:subconfig) { { 'kim' => 'cage' } } | ||
let(:config) { { 'nic' => subconfig } } | ||
let(:env_config) { { 'test' => config } } | ||
|
||
before do | ||
allow(File).to receive(:exist?) { true } | ||
allow(YAML).to receive(:load_file) { env_config } | ||
end | ||
|
||
describe(Config) do | ||
it('can respond like an OpenStruct') do | ||
expect(subject.nic).to eql(subconfig.to_ostruct) | ||
end | ||
|
||
it('handles no config file existing') do | ||
allow(File).to receive(:exist?) { false } | ||
expect(subject.nic).to eql(OpenStruct.new) | ||
end | ||
|
||
it('handles no data in the config file') do | ||
allow(YAML).to receive(:load_file) { false } | ||
expect(subject.nic).to eql(OpenStruct.new) | ||
end | ||
|
||
it('loads config from a file') do | ||
allow(File).to receive(:exist?).and_call_original | ||
allow(YAML).to receive(:load_file).and_call_original | ||
Dir.mktmpdir('shanty-tests') do |tmp_path| | ||
Dir.chdir(tmp_path) { FileUtils.touch('.shanty.yml') } | ||
expect(Config.new(tmp_path, 'test').nic).to eql(OpenStruct.new) | ||
end | ||
end | ||
|
||
it('returns nothing if .shanty.yml does not exist') do | ||
allow(File).to receive(:exist?).and_call_original | ||
allow(YAML).to receive(:load_file).and_call_original | ||
expect(subject.nic).to eql(OpenStruct.new) | ||
end | ||
end | ||
|
||
describe('#[]') do | ||
it('can respond like a hash') do | ||
expect(subject['nic']).to eql(subconfig.to_ostruct) | ||
end | ||
end | ||
|
||
describe('merge!') do | ||
it('can merge in new config') do | ||
added_config = { 'copolla' => 'cage' } | ||
subject.merge!('nic' => added_config) | ||
|
||
expect(subject['nic']).to eql(subconfig.deep_merge(added_config).to_ostruct) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.