Sha256: 88434e41f3eb0dfab4b76edafe228bb7a88a9a0685c75fa64e246cdeaecc2ca6

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Rudder
  module DSL
    ##
    # Utility helper methods for DSL components.
    #
    # This is ntended for internal use and is subject to change.
    #
    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, use_name = true)
        hash.map do |k, 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, use_name = true)
        case value
        when Array
          value.map { |x| _convert_h_val(x, use_name) }
        when Symbol
          value.to_s
        else
          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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rudder-0.3.1 lib/rudder/dsl/util.rb
rudder-0.3.0 lib/rudder/dsl/util.rb
rudder-0.2.2 lib/rudder/dsl/util.rb
rudder-0.2.1 lib/rudder/dsl/util.rb
rudder-0.2.0 lib/rudder/dsl/util.rb