Mangle OnlyWhenBuilt funstionality into the role, going back to trivial map_attributes
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Engine / Trait / OnlyWhenBuilt.pm
index 163be53..461c980 100644 (file)
@@ -1,7 +1,20 @@
 package MooseX::Storage::Engine::Trait::OnlyWhenBuilt;
 use Moose::Role;
 
-our $VERSION = 1;
+# we should
+# only serialize the attribute if it's already built. So, go ahead
+# and check if the attribute has a predicate. If so, check if it's 
+# set  and then go ahead and look it up.
+around 'collapse_attribute' => sub {
+    my ($orig, $self, $attr, @args) = @_;
+
+    my $pred = $attr->predicate if $attr->has_predicate;
+    if ($pred) {
+        return () unless $self->object->$pred();
+    }
+
+    return $self->$orig($attr, @args);
+};
 
 1;