Wrote recipe9, builder and lazy_build.
[gitmo/Moose.git] / lib / Moose / Cookbook / Recipe6.pod
index ed8d572..6381776 100644 (file)
 
 =head1 NAME
 
-Moose::Cookbook::Recipe6 - The Moose::Role example
+Moose::Cookbook::Recipe6 - The augment/inner example
 
 =head1 SYNOPSIS
+    
+  package Document::Page;
+  use Moose;
   
-  package Constraint;
-  use strict;
-  use warnings;
-  use Moose::Role;
-  
-  has 'value' => (isa => 'Int', is => 'ro');
+  has 'body' => (is => 'rw', isa => 'Str', default => sub {''});
   
-  around 'validate' => sub {
-      my $c = shift;
-      my ($self, $field) = @_;
-      if ($c->($self, $self->validation_value($field))) {
-          return undef;
-      } 
-      else {
-          return $self->error_message;
-      }        
-  };
+  sub create {
+      my $self = shift;
+      $self->open_page;
+      inner();
+      $self->close_page;
+  }
   
-  sub validation_value {
-      my ($self, $field) = @_;
-      return $field;
+  sub append_body { 
+      my ($self, $appendage) = @_;
+      $self->body($self->body . $appendage);
   }
   
-  sub error_message { confess "Abstract method!" }
+  sub open_page  { (shift)->append_body('<page>') }
+  sub close_page { (shift)->append_body('</page>') }  
   
-  package Constraint::OnLength;
-  use strict;
-  use warnings;
-  use Moose::Role;
+  package Document::PageWithHeadersAndFooters;
+  use Moose;
   
-  has 'units' => (isa => 'Str', is => 'ro');
+  extends 'Document::Page';
   
-  override 'validation_value' => sub {
-      return length(super());
+  augment 'create' => sub {
+      my $self = shift;
+      $self->create_header;
+      inner();
+      $self->create_footer;
   };
   
-  override 'error_message' => sub {
-      my $self = shift;
-      return super() . ' ' . $self->units;
-  };    
+  sub create_header { (shift)->append_body('<header/>') }
+  sub create_footer { (shift)->append_body('<footer/>') }  
   
-  package Constraint::AtLeast;
-  use strict;
-  use warnings;
+  package TPSReport;
   use Moose;
   
-  with 'Constraint';
-  
-  sub validate {
-      my ($self, $field) = @_;
-      ($field >= $self->value);
-  }
-  
-  sub error_message { 'must be at least ' . (shift)->value; }
-  
-  package Constraint::NoMoreThan;
-  use strict;
-  use warnings;
-  use Moose;
+  extends 'Document::PageWithHeadersAndFooters';
   
-  with 'Constraint';
+  augment 'create' => sub {
+      my $self = shift;
+      $self->create_tps_report;
+  };
   
-  sub validate {
-      my ($self, $field) = @_;
-      ($field <= $self->value);
+  sub create_tps_report {
+     (shift)->append_body('<report type="tps"/>') 
   }
   
-  sub error_message { 'must be no more than ' . (shift)->value; }
-  
-  package Constraint::LengthNoMoreThan;
-  use strict;
-  use warnings;
-  use Moose;
-  
-  extends 'Constraint::NoMoreThan';
-     with 'Constraint::OnLength';
-     
-  package Constraint::LengthAtLeast;
-  use strict;
-  use warnings;
-  use Moose;
-  
-  extends 'Constraint::AtLeast';
-     with 'Constraint::OnLength';
-  
+  print TPSReport->new->create # <page><header/><report type="tps"/><footer/></page>
+
 =head1 DESCRIPTION
 
-Coming Soon. 
+Coming Soon.
+
+=head1 CONCLUSION
+
+=head1 FOOTNOTES
+
+=over 4
 
-(the other 4 recipes kinda burned me out a bit)
+=back
 
 =head1 AUTHOR
 
@@ -104,7 +76,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Infinity Interactive, Inc.
+Copyright 2007 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>
 
@@ -112,4 +84,3 @@ This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
 =cut       
-       
\ No newline at end of file