| 1 | package Gapp::App::Role::HasComponents; | ||
| 2 | { | ||
| 3 | $Gapp::App::Role::HasComponents::VERSION = '0.007'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose::Role; | ||
| 7 | use MooseX::SemiAffordanceAccessor; | ||
| 8 | use MooseX::StrictConstructor; | ||
| 9 | |||
| 10 | use Gapp::App::Hook; | ||
| 11 | |||
| 12 | use MooseX::Types::Moose qw( Str Object ); | ||
| 13 | |||
| 14 | has '_components' => ( | ||
| 15 | is => 'ro', | ||
| 16 | isa => 'HashRef', | ||
| 17 | traits => [qw( Hash )], | ||
| 18 | default => sub { { } }, | ||
| 19 | handles => { | ||
| 20 | com => 'get', | ||
| 21 | }, | ||
| 22 | lazy => 1, | ||
| 23 | ); | ||
| 24 | |||
| 25 | |||
| 26 | sub register_component { | ||
| 27 | my ( $self, $name, $com ) = @_; | ||
| 28 | |||
| 29 | $self->meta->throw_error( 'usage $app->register_component( $name, $com )' ) if ! $name || ! $com; | ||
| 30 | |||
| 31 | $com->set_app( $self ); | ||
| 32 | |||
| 33 | $com->register; | ||
| 34 | |||
| 35 | $self->_components->{ $name } = $com; | ||
| 36 | |||
| 37 | return $com; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | __END__ |