Skip to content

struct Athena::DependencyInjection::AnnotationConfigurations
inherits Struct #

Wraps a hash of configuration annotations applied to a given type, method, or instance variable. Provides the logic to access each annotation's configuration in a type safe manner.

Implementations using this type must define the logic to provide the annotation hash manually; this would most likely just be something like:

# Define a hash to store the configurations.
{% custom_configurations = {} of Nil => Nil %}

# Iterate over the stored annotation classes.
{% for ann_class in ADI::CUSTOM_ANNOTATIONS %}
   {% ann_class = ann_class.resolve %}

   # Define an array to store the annotation configurations of this type.
   {% annotations = [] of Nil %}

   # Iterate over each annotation of this type on the given type, method, or instance variable.
   {% for ann in type_method_instance_variable.annotations ann_class %}
     # Add a new instance of the annotations configuration to the array.
     # Add the annotation's positional arguments first, if any, then named arguments.
     {% annotations << "#{ann_class}Configuration.new(#{ann.args.empty? ? "".id : "#{ann.args.splat},".id}#{ann.named_args.double_splat})".id %}
   {% end %}

   # Update the configuration hash with the annotation class and configuration objects, but only if there was at least one.
   {% custom_configurations[ann_class] = "(#{annotations} of ADI::AnnotationConfigurations::ConfigurationBase)".id unless annotations.empty? %}
 {% end %}

# ...

# Use the built hash to instantiate a new `ADI::AnnotationConfigurations` instance.
ADI::AnnotationConfigurations.new({{custom_configurations}} of ADI::AnnotationConfigurations::Classes => Array(ADI::AnnotationConfigurations::ConfigurationBase)),

Todo

Centralize the hash resolution logic once this issue is resolved.

Constructors#

.new(annotation_hash : AnnotationHash = AnnotationHash.new)#

View source

Methods#

#has?(ann_class : ADI::AnnotationConfigurations::Classes) : Bool#

Returns true if there are annotations of the provided ann_class, otherwise false.

View source