lib/rudder/dsl/util.rb in rudder-0.1.0 vs lib/rudder/dsl/util.rb in rudder-0.2.0

- old
+ new

@@ -10,35 +10,41 @@ module Util ## # Recursively converts keys and values # of a {Hash} to YAML friendly values # + # @param use_name [Boolean] when true named objects + # are rendered only by name. Otherwise, renders + # to {Hash} (if able), or returns the object itself. # @return [Hash] representation of this class # - def _deep_to_h(hash) + def _deep_to_h(hash, use_name = true) hash.map do |k, v| - k = _convert_h_val(k) - v = _convert_h_val(v) + k = _convert_h_val(k, use_name) + v = _convert_h_val(v, use_name) [k, v] end.to_h end ## # Converts non-collections to YAML safe strings # and collections to collections of YAML safe strings # + # @param use_name [Boolean] when true named objects + # are rendered only by name. Otherwise, renders + # to {Hash} (if able), or returns the object itself. # - def _convert_h_val(value) + def _convert_h_val(value, use_name = true) case value - when Hash - _deep_to_h(value) when Array - value.map { |x| _convert_h_val(x) } + value.map { |x| _convert_h_val(x, use_name) } when Symbol value.to_s else - if value.respond_to? :to_h - _deep_to_h(value.to_h) + if use_name && value.respond_to?(:name) + value.name.to_s + elsif value.respond_to? :to_h + _deep_to_h(value.to_h, use_name) else value end end end