Skip to content

Commit

Permalink
MergedConfig: Support both string & symbol for []
Browse files Browse the repository at this point in the history
Signed-off-by: Julien 'Lta' BALLET <[email protected]>
  • Loading branch information
elthariel committed Dec 10, 2016
1 parent ecdbd01 commit dcbe0b4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/cheffish/merged_config.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'chef/mash'

module Cheffish
class MergedConfig
def initialize(*configs)
@configs = configs
@merge_arrays = {}
@configs = configs.map { |config| Chef::Mash.from_hash config }
@merge_arrays = Chef::Mash.new
end

include Enumerable
Expand Down
42 changes: 41 additions & 1 deletion spec/functional/merged_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
Cheffish::MergedConfig.new({ :test => "val" })
end

let(:collision) do
c1 = { :test1 => 'c1.1', 'test2' => 'c1.2' }
c2 = { 'test1' => 'c2.1', 'test3' => 'c2.3' }
Cheffish::MergedConfig.new(c1, c2)
end

let(:config_mismatch) do
c1 = { :test => { :test => 'val' } }
c2 = { :test => [2, 3, 4] }
Cheffish::MergedConfig.new(c1, c2)
end

let(:config_hashes) do
c1 = { :test => { :test => 'val' } }
c2 = { :test => { :test2 => 'val2' } }
Cheffish::MergedConfig.new(c1, c2)
end

it "returns value in config" do
expect(config.test).to eq("val")
end
Expand All @@ -15,6 +33,28 @@
end

it "has an informative string representation" do
expect("#{config}").to eq("{:test=>\"val\"}")
expect("#{config}").to eq("{\"test\"=>\"val\"}")
end

it "has indifferent str/sym access" do
expect(config['test']).to eq('val')
end

it "respects precedence between the different configs" do
expect(collision['test1']).to eq('c1.1')
expect(collision[:test1]).to eq('c1.1')
end

it "merges the configs" do
expect(collision[:test2]).to eq('c1.2')
expect(collision[:test3]).to eq('c2.3')
end

it "handle merged value type mismatch" do
expect(config_mismatch[:test]).to eq('test' => 'val')
end

it "merges values when they're hashes" do
expect(config_hashes[:test].keys).to eq ['test', 'test2']
end
end

0 comments on commit dcbe0b4

Please sign in to comment.