Aion
view release on metacpan or search on metacpan
lib/Aion/Pleroma.pm view on Meta::CPAN
die "Added eon $key twice, with $action ne $action_exists" if $action_exists ne $action;
}
else {
$self->pleroma->{$key} = $action;
}
$self
}
1;
__END__
=encoding utf-8
=head1 NAME
Aion::Pleroma - container of aeons
=head1 SYNOPSIS
use Aion::Pleroma;
my $pleroma = Aion::Pleroma->new;
$pleroma->get('user') # -> undef
$pleroma->resolve('user') # @-> user is'nt eon!
=head1 DESCRIPTION
Implements the dependency container pattern.
An eon is created when requesting from a container via the C<get> or C<resolve> method, or via the C<eon> aspect as a lazy C<default>. Laziness can be canceled via the C<lazy> aspect.
The container can be obtained using C<< Aion-E<gt>pleroma >>.
The configuration for creating eons is obtained from the C<PLEROMA> config and the annotation file (created by the C<Aion::Annotation> package). The annotation file can be replaced via the C<INI> config.
=head1 CONFIG
Module settings that can be set in C<.env>:
=over
=item * AION_PLEROMA_INI â annotation file. Defaults to C<etc/annotation/eon.ann>.
=item * AION_PLEROMA_AUTOWARE â load modules automatically, even if they are not specified in the configuration. Default is C<1>.
=back
=head1 EONS FROM CONFIG
You can add the C<aion.eon> key to C<etc/*.yml> with a description of additional eons. This allows you to assemble eons declaratively: specify constructor arguments (named or ordered), call methods after creation, and pass references to other eons vi...
Let us describe the classes of eons.
File lib/Ex/Eon/Astronomer.pm:
package Ex::Eon::Astronomer;
use strict; use warnings;
sub new { my ($class, $name, $telescope) = @_; bless { name => $name, telescope => $telescope, seen => [] }, $class }
sub name { $_[0]{name} }
sub telescope { $_[0]{telescope} }
sub seen { $_[0]{seen} }
sub observe { my ($self, $body) = @_; push @{ $self->{seen} }, $body; $body }
1;
File lib/Ex/Eon/Planet.pm:
package Ex::Eon::Planet;
use common::sense;
use Aion;
has name => (is => 'ro');
has moons => (is => 'ro', default => 0);
has discoverer => (is => 'ro');
1;
Now the configuration of the eons. The eon-scientist C<Ex::Eon::Galileo> has arguments specified in order (C<arguments> is an array), after creation the C<observe> method is called with a link to another eon (C<@Ex::Eon::Saturn>). For planets, C<argu...
File etc/aion/eon.yml:
aion:
eon:
Ex::Eon::Galileo:
class: 'Ex::Eon::Astronomer'
arguments: [ 'Galileo Galilei', 'refracting telescope' ]
calls:
- [observe, '@Ex::Eon::Saturn']
Ex::Eon::Jupiter:
class: 'Ex::Eon::Planet'
arguments:
name: 'Jupiter'
moons: 95
discoverer: '@Ex::Eon::Galileo'
Ex::Eon::Saturn:
class: 'Ex::Eon::Planet'
arguments:
name: 'Saturn'
moons: 146
Let's load the configuration from C<etc/aion/eon.yml>, create a container and request eons.
use Aion::Pleroma;
use Aion::Env::Etc ();
my $etc = Aion::Env::Etc::parse('etc/aion/eon.yml');
my $pleroma = Aion::Pleroma->new(pleroma => $etc->{aion}{eon});
my $galileo = $pleroma->resolve('Ex::Eon::Galileo');
$galileo->name # => Galileo Galilei
$galileo->telescope # => refracting telescope
ref($galileo->seen->[0]) # => Ex::Eon::Planet
$galileo->seen->[0]->name # => Saturn
my $jupiter = $pleroma->resolve('Ex::Eon::Jupiter');
$jupiter->name # => Jupiter
$jupiter->moons # => 95
$jupiter->discoverer->name # => Galileo Galilei
( run in 0.668 second using v1.01-cache-2.11-cpan-364913b4093 )