summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-03-02 23:07:06 -0800
committerJay Mundrawala <jdmundrawala@gmail.com>2015-03-20 14:38:03 -0700
commitc7dcd7ffaee7e096ea56176fa88993f12debaecc (patch)
tree522cdfd83a4e7d622ed0c215aeeb7a960b1cff1c
parent1c6feca1fd7487ba02effcdff6a30e26ddc4a490 (diff)
downloadchef-c7dcd7ffaee7e096ea56176fa88993f12debaecc.tar.gz
Add type coercion for hash
-rw-r--r--lib/chef/mixin/powershell_type_coercions.rb10
-rw-r--r--spec/unit/mixin/powershell_type_coercions_spec.rb5
2 files changed, 14 insertions, 1 deletions
diff --git a/lib/chef/mixin/powershell_type_coercions.rb b/lib/chef/mixin/powershell_type_coercions.rb
index 14d4a66ee4..580288c3a0 100644
--- a/lib/chef/mixin/powershell_type_coercions.rb
+++ b/lib/chef/mixin/powershell_type_coercions.rb
@@ -25,10 +25,18 @@ class Chef
Fixnum => { :type => lambda { |x| x.to_s }, :single_quoted => false },
Float => { :type => lambda { |x| x.to_s }, :single_quoted => false },
FalseClass => { :type => lambda { |x| '$false' }, :single_quoted => false },
- TrueClass => { :type => lambda { |x| '$true' }, :single_quoted => false }
+ TrueClass => { :type => lambda { |x| '$true' }, :single_quoted => false },
+ Hash => {:type => Proc.new { |x| translate_hash(x)}, :single_quoted => false}
}
end
+ def translate_hash(x)
+ translated = x.inject([]) do |memo, (k,v)|
+ memo << "#{k}=#{translate_type(v)}"
+ end
+ "@{#{translated.join(';')}}"
+ end
+
def translate_type(value)
translation = type_coercions[value.class]
should_quote = true
diff --git a/spec/unit/mixin/powershell_type_coercions_spec.rb b/spec/unit/mixin/powershell_type_coercions_spec.rb
index beb605628d..4cca7e2f1f 100644
--- a/spec/unit/mixin/powershell_type_coercions_spec.rb
+++ b/spec/unit/mixin/powershell_type_coercions_spec.rb
@@ -42,5 +42,10 @@ describe Chef::Mixin::PowershellTypeCoercions do
it 'should return $true when an instance of TrueClass is provided' do
expect(test_class.translate_type(true)).to eq('$true')
end
+
+ it 'should translate all members of a hash and wrap them in @{} separated by ;' do
+ expect(test_class.translate_type({"a" => 1, "b" => 1.2, "c" => false, "d" => true
+ })).to eq("@{a=1;b=1.2;c=$false;d=$true}")
+ end
end
end