Skip to content

struct Athena::Config::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 ACF::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 ACF::AnnotationConfigurations::ConfigurationBase)".id unless annotations.empty? %}
 {% end %}

# ...

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

Todo

Centralize the hash resolution logic once this issue is resolved.

Class methods#

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

View source

Methods#

#[](ann_class : Athena::Routing::Annotations::View.class, index : Int32 = -1) : Athena::Routing::Annotations::ViewConfiguration #

Returns the Athena::Routing::Annotations::View configuration instance for the provided ann_class at the provided index.

Returns the last configuration instance by default.

View source

#[]?(ann_class : Athena::Routing::Annotations::View.class, index : Int32 = -1) : Athena::Routing::Annotations::ViewConfiguration? #

Returns the Athena::Routing::Annotations::View configuration instance for the provided ann_class at the provided index, or nil if no annotations of that type were found.

Returns the last configuration instance by default.

View source

#fetch_all(ann_class : Athena::Routing::Annotations::View.class) : Array(ACF::AnnotationConfigurations::ConfigurationBase) #

Returns an array of Athena::Routing::Annotations::View configuration instances for the provided ann_class.

View source

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

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

View source