From d36473f78465d476d2dcaff3789a25a787827d58 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 15 Nov 2016 16:20:55 -0800 Subject: simplify some overly complicated powershell code Signed-off-by: Lamont Granquist --- lib/chef/mixin/powershell_type_coercions.rb | 38 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/chef/mixin/powershell_type_coercions.rb b/lib/chef/mixin/powershell_type_coercions.rb index 4ec555ded6..792ec18842 100644 --- a/lib/chef/mixin/powershell_type_coercions.rb +++ b/lib/chef/mixin/powershell_type_coercions.rb @@ -21,31 +21,31 @@ class Chef module Mixin module PowershellTypeCoercions - def type_coercions - @type_coercions ||= { - Integer => { :type => lambda { |x| x.to_s } }, - Float => { :type => lambda { |x| x.to_s } }, - FalseClass => { :type => lambda { |x| "$false" } }, - TrueClass => { :type => lambda { |x| "$true" } }, - Hash => { :type => Proc.new { |x| translate_hash(x) } }, - Array => { :type => Proc.new { |x| translate_array(x) } }, - Chef::Node::ImmutableMash => { :type => Proc.new { |x| translate_hash(x) } }, - Chef::Node::ImmutableArray => { :type => Proc.new { |x| translate_array(x) } }, - } + def type_coercion(value) + case value + when Integer, Float + value.to_s + when FalseClass + "$false" + when TrueClass + "$true" + when Hash, Chef::Node::ImmutableMash + translate_hash(value) + when Array, Chef::Node::ImmutableArray + translate_array(value) + end end - def translate_type(value) - translation = type_coercions[value.class] - - if translation - translation[:type].call(value) - elsif value.respond_to? :to_psobject + def psobject_conversion(value) + if value.respond_to?(:to_psobject) "(#{value.to_psobject})" - else - safe_string(value.to_s) end end + def translate_type(value) + type_coercion(value) || psobject_conversion(value) || safe_string(value.to_s) + end + private def translate_hash(x) -- cgit v1.2.1