diff options
author | Jesse Luehrs <doy@tozt.net> | 2011-02-20 02:04:52 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2011-02-20 02:04:52 -0600 |
commit | 8ad579f26a1c74d5685491df55b57e694e54e087 (patch) | |
tree | de3c03114a1932c1ddbb4a72ae5c34c5ceaf19f3 /lib/MooseX/Bread/Board/Role | |
parent | a0cc11a5f9b60226a07054d6041cc45b4ea2221a (diff) | |
download | bread-board-declare-8ad579f26a1c74d5685491df55b57e694e54e087.tar.gz bread-board-declare-8ad579f26a1c74d5685491df55b57e694e54e087.zip |
initial implementation
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; |