Mangle OnlyWhenBuilt funstionality into the role, going back to trivial map_attributes
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Engine / Trait / OnlyWhenBuilt.pm
CommitLineData
a473d69d 1package MooseX::Storage::Engine::Trait::OnlyWhenBuilt;
2use Moose::Role;
3
4eafd265 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.
8around '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};
a473d69d 18
191;
20