class Tdc::DefinitionResolvers::TagResolver

Knows how to resolve the tag value for the specified instance_definition key by replacing it with an object sourced from the current catalog.

Attributes

key[R]
source[R]

Public Class Methods

new(key:, source:) click to toggle source
# File lib/tdc/definition_resolvers/tag_resolver.rb, line 10
def initialize(key:, source:)
  @key = key
  @source = source
end

Public Instance Methods

resolve(instance_definition) click to toggle source
# File lib/tdc/definition_resolvers/tag_resolver.rb, line 15
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 Instance Methods

unresolvable_tag(tag, catalog_entry) click to toggle source
# File lib/tdc/definition_resolvers/tag_resolver.rb, line 35
      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