diff options
Diffstat (limited to 'lib/MooseX/Bread/Board/Role')
-rw-r--r-- | lib/MooseX/Bread/Board/Role/Object.pm | 27 | ||||
-rw-r--r-- | lib/MooseX/Bread/Board/Role/Service.pm | 32 |
2 files changed, 59 insertions, 0 deletions
diff --git a/lib/MooseX/Bread/Board/Role/Object.pm b/lib/MooseX/Bread/Board/Role/Object.pm new file mode 100644 index 0000000..552bc75 --- /dev/null +++ b/lib/MooseX/Bread/Board/Role/Object.pm @@ -0,0 +1,27 @@ +package MooseX::Bread::Board::Role::Object; +use Moose::Role; +use Bread::Board; + +has name => ( + is => 'rw', + isa => 'Str', + lazy => 1, + default => sub { shift->meta->name }, +); + +sub BUILD { } +after BUILD => sub { + my $self = shift; + + my $meta = Class::MOP::class_of($self); + return unless $meta->has_services; + + for my $service ($meta->services) { + $self->add_service($service->clone); + } +}; + +no Bread::Board; +no Moose::Role; + +1; diff --git a/lib/MooseX/Bread/Board/Role/Service.pm b/lib/MooseX/Bread/Board/Role/Service.pm new file mode 100644 index 0000000..8442e3a --- /dev/null +++ b/lib/MooseX/Bread/Board/Role/Service.pm @@ -0,0 +1,32 @@ +package MooseX::Bread::Board::Role::Service; +use Moose::Role; + +has associated_attribute => ( + is => 'ro', + isa => 'Class::MOP::Attribute', + required => 1, + weak_ref => 1, +); + +around get => sub { + my $orig = shift; + my $self = shift; + + my $container = $self; + until (!defined($container) + || ($container->isa('Bread::Board::Container') + && $container->does('MooseX::Bread::Board::Role::Object'))) { + $container = $container->parent; + } + die "Couldn't find associated object!" unless defined $container; + + if ($self->associated_attribute->has_value($container)) { + return $self->associated_attribute->get_value($container); + } + + return $self->$orig(@_); +}; + +no Moose::Role; + +1; |