DEATH TO ALL zionist ELLIPSES
[gitmo/Moose.git] / lib / Moose / Cookbook / Basics / Recipe2.pod
index 56d2909..d180a38 100644 (file)
@@ -9,33 +9,33 @@ Moose::Cookbook::Basics::Recipe2 - A simple B<BankAccount> example
 
   package BankAccount;
   use Moose;
-  
-  has 'balance' => (isa => 'Int', is => 'rw', default => 0);
-  
+
+  has 'balance' => ( isa => 'Int', is => 'rw', default => 0 );
+
   sub deposit {
-      my ($self, $amount) = @_;
-      $self->balance($self->balance + $amount);
+      my ( $self, $amount ) = @_;
+      $self->balance( $self->balance + $amount );
   }
-  
+
   sub withdraw {
-      my ($self, $amount) = @_;
+      my ( $self, $amount ) = @_;
       my $current_balance = $self->balance();
-      ($current_balance >= $amount)
+      ( $current_balance >= $amount )
           || confess "Account overdrawn";
-      $self->balance($current_balance - $amount);
+      $self->balance( $current_balance - $amount );
   }
-  
+
   package CheckingAccount;
   use Moose;
-  
+
   extends 'BankAccount';
-  
-  has 'overdraft_account' => (isa => 'BankAccount', is => 'rw');       
-  
+
+  has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );
+
   before 'withdraw' => sub {
-      my ($self, $amount) = @_;
+      my ( $self, $amount ) = @_;
       my $overdraft_amount = $amount - $self->balance();
-      if ($self->overdraft_account && $overdraft_amount > 0) {
+      if ( $self->overdraft_account && $overdraft_amount > 0 ) {
           $self->overdraft_account->withdraw($overdraft_amount);
           $self->deposit($overdraft_amount);
       }
@@ -45,11 +45,11 @@ Moose::Cookbook::Basics::Recipe2 - A simple B<BankAccount> example
 
 The first recipe demonstrated how to build very basic Moose classes,
 focusing on creating and manipulating attributes. The objects in that
-recipe very data-oriented, and did not have much in the way of
+recipe were very data-oriented, and did not have much in the way of
 behavior (i.e. methods). In this recipe, we expand upon the concepts
 from the first recipe to include some real behavior. In particular, we
-should how you can use a method modifier to implement new behavior for
-a method.
+show how you can use a method modifier to implement new behavior for a
+method.
 
 The classes in the SYNOPSIS show two kinds of bank account. A simple
 bank account has one attribute, the balance, and two behaviors,
@@ -65,7 +65,7 @@ account. (1)
 The first class, B<BankAccount>, introduces a new attribute feature, a
 default value:
 
-  has 'balance' => (isa => 'Int', is => 'rw', default => 0);
+  has 'balance' => ( isa => 'Int', is => 'rw', default => 0 );
 
 This says that a B<BankAccount> has a C<balance> attribute, which has
 a C<Int> type constraint, a read/write accessor, and a default value
@@ -81,7 +81,7 @@ class's superclass. Here we see that B<CheckingAccount> C<extends>
 B<BankAccount>. The next line introduces yet another new attribute
 feature, class-based type constraints:
 
-  has 'overdraft_account' => (isa => 'BankAccount', is => 'rw');
+  has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );
 
 Up until now, we have only seen the C<Int> type constraint, which (as
 we saw in the first recipe) is a builtin type constraint. The
@@ -101,9 +101,9 @@ In B<CheckingAccount>, we see another method modifier, the C<before>
 modifier.
 
   before 'withdraw' => sub {
-      my ($self, $amount) = @_;
+      my ( $self, $amount ) = @_;
       my $overdraft_amount = $amount - $self->balance();
-      if ($self->overdraft_account && $overdraft_amount > 0) {
+      if ( $self->overdraft_account && $overdraft_amount > 0 ) {
           $self->overdraft_account->withdraw($overdraft_amount);
           $self->deposit($overdraft_amount);
       }
@@ -124,9 +124,9 @@ As with the method modifier in the first recipe, we could use
 C<SUPER::> to get the same effect:
 
   sub withdraw {
-      my ($self, $amount) = @_;
+      my ( $self, $amount ) = @_;
       my $overdraft_amount = $amount - $self->balance();
-      if ($self->overdraft_account && $overdraft_amount > 0) {
+      if ( $self->overdraft_account && $overdraft_amount > 0 ) {
           $self->overdraft_account->withdraw($overdraft_amount);
           $self->deposit($overdraft_amount);
       }
@@ -157,14 +157,12 @@ method, which accepts named parameters.
   );
 
 And as with the first recipe, a more in-depth example can be found in
-the F<t/000_recipes/basics/002_recipe.t> test file.
+the F<t/000_recipes/moose_cookbook_basics_recipe2.t> test file.
 
 =head1 CONCLUSION
 
-The aim of this recipe was to take the knowledge gained in the first
-recipe and expand upon it with a more realistic use case. The next
-recipe will expand on Moose attributes to create a behaviorally
-sophisticated class defined almost entirely by its attributes.
+This recipe expanded on the basic concepts from the first recipe with
+a more "real world" use case.
 
 =head1 FOOTNOTES
 
@@ -214,17 +212,106 @@ L<http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html>
 
 =back
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
+Dave Rolsky E<lt>autarch@urth.orgE<gt>
+
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2008 by Infinity Interactive, Inc.
+Copyright 2006-2009 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
+=begin testing
+
+my $savings_account;
+
+{
+    $savings_account = BankAccount->new( balance => 250 );
+    isa_ok( $savings_account, 'BankAccount' );
+
+    is( $savings_account->balance, 250, 'got the right savings balance' );
+    lives_ok {
+        $savings_account->withdraw(50);
+    }
+    'withdrew from savings successfully';
+    is( $savings_account->balance, 200,
+        'got the right savings balance after withdrawl' );
+
+    $savings_account->deposit(150);
+    is( $savings_account->balance, 350,
+        'got the right savings balance after deposit' );
+}
+
+{
+    my $checking_account = CheckingAccount->new(
+        balance           => 100,
+        overdraft_account => $savings_account
+    );
+    isa_ok( $checking_account, 'CheckingAccount' );
+    isa_ok( $checking_account, 'BankAccount' );
+
+    is( $checking_account->overdraft_account, $savings_account,
+        'got the right overdraft account' );
+
+    is( $checking_account->balance, 100,
+        'got the right checkings balance' );
+
+    lives_ok {
+        $checking_account->withdraw(50);
+    }
+    'withdrew from checking successfully';
+    is( $checking_account->balance, 50,
+        'got the right checkings balance after withdrawl' );
+    is( $savings_account->balance, 350,
+        'got the right savings balance after checking withdrawl (no overdraft)'
+    );
+
+    lives_ok {
+        $checking_account->withdraw(200);
+    }
+    'withdrew from checking successfully';
+    is( $checking_account->balance, 0,
+        'got the right checkings balance after withdrawl' );
+    is( $savings_account->balance, 200,
+        'got the right savings balance after overdraft withdrawl' );
+}
+
+{
+    my $checking_account = CheckingAccount->new(
+        balance => 100
+
+            # no overdraft account
+    );
+    isa_ok( $checking_account, 'CheckingAccount' );
+    isa_ok( $checking_account, 'BankAccount' );
+
+    is( $checking_account->overdraft_account, undef,
+        'no overdraft account' );
+
+    is( $checking_account->balance, 100,
+        'got the right checkings balance' );
+
+    lives_ok {
+        $checking_account->withdraw(50);
+    }
+    'withdrew from checking successfully';
+    is( $checking_account->balance, 50,
+        'got the right checkings balance after withdrawl' );
+
+    dies_ok {
+        $checking_account->withdraw(200);
+    }
+    'withdrawl failed due to attempted overdraft';
+    is( $checking_account->balance, 50,
+        'got the right checkings balance after withdrawl failure' );
+}
+
+=end testing
+
 =cut