Sha256: f0ac3f6154ede48b32a0269998a90a37d79ec17902c598f597be3e4562f28d52

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

# This module defines some methods for walking the reference tree
# of various objects.
module RefCheck
  class SelfReference < StandardError
  end

  class NullReference < StandardError
  end

  # Build up a set of references.
  # rubocop:disable Metrics/PerceivedComplexity
  def build_references(refs = [], origin = nil, method = :all_refs)
    if respond_to?(method)
      send(method).each do |ref|
        raise SelfReference, "#{origin} references itself at #{to_json}" if origin && ref.to_s == origin
        raise NullReference, "#{origin} contains null value reference at #{to_json}" if origin && ref.nil?

        refs << ref
      end
    end

    ref_children.each do |elem|
      # Nulls are not permitted in Cloudformation templates.
      raise NullReference, "#{origin} contains null value reference at #{to_json}" if origin && elem.nil?

      elem.build_references(refs, origin, method) if elem.respond_to?(:build_references)
    end

    refs
  end
  # rubocop:enable Metrics/PerceivedComplexity

  def ref_children
    []
  end
end

# Mixin to Array
class Array
  include RefCheck
  def ref_children
    self
  end
end

# Mixin to Hash
class Hash
  include RefCheck
  def ref_children
    values
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cfndsl-1.1.1 lib/cfndsl/ref_check.rb
cfndsl-1.1.0 lib/cfndsl/ref_check.rb
cfndsl-1.0.6 lib/cfndsl/ref_check.rb
cfndsl-1.0.5 lib/cfndsl/ref_check.rb
cfndsl-1.0.4 lib/cfndsl/ref_check.rb
cfndsl-1.0.3 lib/cfndsl/ref_check.rb
cfndsl-1.0.2 lib/cfndsl/ref_check.rb
cfndsl-1.0.1 lib/cfndsl/ref_check.rb
cfndsl-1.0.0 lib/cfndsl/ref_check.rb
cfndsl-1.0.0.pre.1 lib/cfndsl/ref_check.rb