Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add namespaces to YAML sources per #288 #301

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions lib/config/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ def empty?
marshal_dump.empty?
end

def add_source!(source)
def add_source!(source, namespace = nil)
# handle yaml file paths
source = (Sources::YAMLSource.new(source)) if source.is_a?(String)
source = (Sources::HashSource.new(source)) if source.is_a?(Hash)
source = (Sources::YAMLSource.new(source, namespace)) if source.is_a?(String)
source = (Sources::HashSource.new(source, namespace)) if source.is_a?(Hash)

@config_sources ||= []
@config_sources << source
end

def prepend_source!(source)
source = (Sources::YAMLSource.new(source)) if source.is_a?(String)
source = (Sources::HashSource.new(source)) if source.is_a?(Hash)
def prepend_source!(source, namespace = nil)
source = (Sources::YAMLSource.new(source, namespace)) if source.is_a?(String)
source = (Sources::HashSource.new(source, namespace)) if source.is_a?(Hash)

@config_sources ||= []
@config_sources.unshift(source)
Expand Down
12 changes: 10 additions & 2 deletions lib/config/sources/hash_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ module Sources
class HashSource
attr_accessor :hash

def initialize(hash)
def initialize(hash, namespace = nil)
@hash = hash

# Make sure @namespace is an array if it exists at all
if namespace
@namespace = namespace
@namespace = [@namespace] unless @namespace.is_a?(Array)
end
end

# returns hash that was passed in to initialize
def load
hash.is_a?(Hash) ? hash : {}
return {} unless hash.is_a?(Hash)
return hash unless @namespace
return @namespace.reverse.inject(hash) { |a, n| { n => a } }
end
end
end
Expand Down
17 changes: 14 additions & 3 deletions lib/config/sources/yaml_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ module Sources
class YAMLSource
attr_accessor :path

def initialize(path)
def initialize(path, namespace = nil)
@path = path.to_s

# Make sure @namespace is an array if it exists at all
if namespace
@namespace = namespace
@namespace = [@namespace] unless @namespace.is_a?(Array)
end
end

# returns a config hash from the YML file
def load
result = YAML.load(ERB.new(IO.read(@path)).result) if @path and File.exist?(@path)

result || {}
return {} unless result
return result unless @namespace

# Resolves namespacing multiple layers deep
# i.e. ['layer1', 'layer2'] comes out to {'layer1' => {'layer2' => content}}
return @namespace.reverse.inject(result) { |a, n| { n => a } }


rescue Psych::SyntaxError => e
raise "YAML syntax error occurred while parsing #{@path}. " \
Expand Down
56 changes: 54 additions & 2 deletions spec/sources/hash_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ module Config::Sources

context "basic hash" do
let(:source) do
HashSource.new(
example_hash =
{
"size" => 2,
"section" => {
"size" => 3,
"servers" => [ {"name" => "yahoo.com"}, {"name" => "amazon.com"} ]
}
}
)
HashSource.new(example_hash)
end

it "should properly read the settings" do
Expand All @@ -33,6 +33,58 @@ module Config::Sources
end
end

context "basic hash with single namespace" do
let(:source) do
example_hash =
{
"size" => 2,
"section" => {
"size" => 3,
"servers" => [ {"name" => "yahoo.com"}, {"name" => "amazon.com"} ]
}
}
HashSource.new(example_hash, 'test_namespace')
end

it "should properly read the settings" do
results = source.load
expect(results['test_namespace']["size"]).to eq(2)
end

it "should properly read nested settings" do
results = source.load
expect(results['test_namespace']["section"]["size"]).to eq(3)
expect(results['test_namespace']["section"]["servers"]).to be_instance_of(Array)
expect(results['test_namespace']["section"]["servers"].size).to eq(2)
end
end

context "basic hash with nested namespace" do
let(:source) do
example_hash =
{
"size" => 2,
"section" => {
"size" => 3,
"servers" => [ {"name" => "yahoo.com"}, {"name" => "amazon.com"} ]
}
}
HashSource.new(example_hash, ['test_namespace', 'test_layer_2'])
end

it "should properly read the settings" do
results = source.load
expect(results['test_namespace']['test_layer_2']["size"]).to eq(2)
end

it "should properly read nested settings" do
results = source.load
expect(results['test_namespace']['test_layer_2']["section"]["size"]).to eq(3)
expect(results['test_namespace']['test_layer_2']["section"]["servers"]).to be_instance_of(Array)
expect(results['test_namespace']['test_layer_2']["section"]["servers"].size).to eq(2)
end
end

context "parameter is not a hash" do
let(:source) do
HashSource.new "hello world"
Expand Down
36 changes: 36 additions & 0 deletions spec/sources/yaml_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,42 @@ module Config::Sources
end
end

context "basic yml file with single namespace" do
let(:source) do
YAMLSource.new "#{fixture_path}/development.yml", 'test_namespace'
end

it "should properly read the settings" do
results = source.load
expect(results['test_namespace']["size"]).to eq(2)
end

it "should properly read nested settings" do
results = source.load
expect(results['test_namespace']["section"]["size"]).to eq(3)
expect(results['test_namespace']["section"]["servers"]).to be_instance_of(Array)
expect(results['test_namespace']["section"]["servers"].size).to eq(2)
end
end

context "basic yml file with nested namespace" do
let(:source) do
YAMLSource.new "#{fixture_path}/development.yml", ['test_namespace', 'test_layer_2']
end

it "should properly read the settings" do
results = source.load
expect(results['test_namespace']['test_layer_2']["size"]).to eq(2)
end

it "should properly read nested settings" do
results = source.load
expect(results['test_namespace']['test_layer_2']["section"]["size"]).to eq(3)
expect(results['test_namespace']['test_layer_2']["section"]["servers"]).to be_instance_of(Array)
expect(results['test_namespace']['test_layer_2']["section"]["servers"].size).to eq(2)
end
end

context "yml file with erb tags" do
let(:source) do
YAMLSource.new "#{fixture_path}/with_erb.yml"
Expand Down