Sha256: 2b6733add83659c29a69ccda711984b9d22fbb62531a071d7d8e85dd0fbcde55

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

module Tdc
  module DefinitionResolvers
    #
    # Knows how to resolve the tag value for the specified instance_definition key by replacing it
    # with an object sourced from the current catalog.
    #
    class TagResolver < Tdc::DefinitionResolvers::DefinitionResolver
      attr_reader :key, :source

      def initialize(key:, source:)
        @key = key
        @source = source
      end

      def resolve(instance_definition)
        return unless instance_definition.key?(key)

        # Lookup the source catalog entry in the current_catalog.
        catalog_entry = instance_eval("current_catalog.#{source}", __FILE__, __LINE__)

        # Before resolution the instance definition value is a tag.
        tag = instance_definition[key]

        # Use the tag to source an object from the current catalog.
        sourced_object = catalog_entry.send(tag)

        unresolvable_tag(tag, catalog_entry) unless sourced_object

        # Replace the tag value with the sourced object.
        instance_definition[key] = sourced_object
      end

      private

      def unresolvable_tag(tag, catalog_entry)
        source_tags = catalog_entry.entries.sort.map { |entry| "'#{entry}'" }.to_sentence

        raise Tdc::FatalError, <<~MESSAGE
          Could not resolve key '#{key}' from source '#{source}'.

          Attempted to resolve tag '#{tag}' from these tags: #{source_tags}
        MESSAGE
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tdc-0.6.1 lib/tdc/definition_resolvers/tag_resolver.rb
tdc-0.6.0 lib/tdc/definition_resolvers/tag_resolver.rb
tdc-0.5.0 lib/tdc/definition_resolvers/tag_resolver.rb
tdc-0.4.7 lib/tdc/definition_resolvers/tag_resolver.rb
tdc-0.4.6.1 lib/tdc/definition_resolvers/tag_resolver.rb
tdc-0.4.6 lib/tdc/definition_resolvers/tag_resolver.rb