Mangle OnlyWhenBuilt funstionality into the role, going back to trivial map_attributes
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Engine / Trait / OnlyWhenBuilt.pm
1 package MooseX::Storage::Engine::Trait::OnlyWhenBuilt;
2 use Moose::Role;
3
4 # we should
5 # only serialize the attribute if it's already built. So, go ahead
6 # and check if the attribute has a predicate. If so, check if it's 
7 # set  and then go ahead and look it up.
8 around 'collapse_attribute' => sub {
9     my ($orig, $self, $attr, @args) = @_;
10
11     my $pred = $attr->predicate if $attr->has_predicate;
12     if ($pred) {
13         return () unless $self->object->$pred();
14     }
15
16     return $self->$orig($attr, @args);
17 };
18
19 1;
20