+package Moose::Cookbook::Basics::Recipe2;
-=pod
+# ABSTRACT: A simple B<BankAccount> example
+
+__END__
-=head1 NAME
-Moose::Cookbook::Basics::Recipe2 - A simple B<BankAccount> example
+=pod
=head1 SYNOPSIS
other value is provided to the constructor.
The C<deposit> and C<withdraw> methods should be fairly
-self-explanatory, as they are just plain old Perl 5 OO.
+self-explanatory, as they are just plain old Perl 5 OO. (2)
As you know from the first recipe, the keyword C<extends> sets a
class's superclass. Here we see that B<CheckingAccount> C<extends>
C<BankAccount> type constraint is new, and was actually defined the
moment we created the B<BankAccount> class itself. In fact, Moose
creates a corresponding type constraint for every class in your
-program (2).
+program (3).
This means that in the first recipe, constraints for both C<Point> and
C<Point3D> were created. In this recipe, both C<BankAccount> and
C<CheckingAccount> type constraints are created automatically. Moose
does this as a convenience so that your classes and type constraint
can be kept in sync with one another. In short, Moose makes sure that
-it will just DWIM (3).
+it will just DWIM (4).
In B<CheckingAccount>, we see another method modifier, the C<before>
modifier.
protection by first checking if there are available funds in the
checking account. If not (and if there is an overdraft account
available), it transfers the amount needed into the checking
-account (4).
+account (5).
As with the method modifier in the first recipe, we could use
C<SUPER::> to get the same effect:
);
And as with the first recipe, a more in-depth example can be found in
-the F<t/000_recipes/moose_cookbook_basics_recipe2.t> test file.
+the F<t/recipes/moose_cookbook_basics_recipe2.t> test file.
=head1 CONCLUSION
=item (2)
+Note that for simple methods like these, which just manipulate some
+single piece of data, it is often not necessary to write them at all.
+For instance, C<deposit> could be implemented via the C<inc> native
+delegation for counters - see
+L<Moose::Meta::Attribute::Native::Trait::Counter> for more specifics,
+and L<Moose::Meta::Attribute::Native> for a broader overview.
+
+=item (3)
+
In reality, this creation is sensitive to the order in which modules
are loaded. In more complicated cases, you may find that you need to
-explicitly declare a class type before the corresponding is loaded.
+explicitly declare a class type before the corresponding class is
+loaded.
-=item (3)
+=item (4)
Moose does not attempt to encode a class's is-a relationships within
the type constraint hierarchy. Instead, Moose just considers the class
successfully. For more details, please refer to the
L<Moose::Util::TypeConstraints> documentation.
-=item (4)
+=item (5)
If the overdraft account does not have the amount needed, it will
throw an error. Of course, the overdraft account could also have
=back
-=head1 SEE ALSO
-
-=over 4
-
-=item Acknowledgment
+=head1 ACKNOWLEDGMENT
The BankAccount example in this recipe is directly taken from the
examples in this chapter of "Practical Common Lisp":
L<http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html>
-=back
-
-=head1 AUTHORS
-
-Stevan Little E<lt>stevan@iinteractive.comE<gt>
-
-Dave Rolsky E<lt>autarch@urth.orgE<gt>
-
-=head1 COPYRIGHT AND LICENSE
-
-Copyright 2006-2010 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;
isa_ok( $savings_account, 'BankAccount' );
is( $savings_account->balance, 250, '... got the right savings balance' );
- ok ! exception {
- $savings_account->withdraw(50);
- },
- '... withdrew from savings successfully';
+ is(
+ exception {
+ $savings_account->withdraw(50);
+ },
+ undef,
+ '... withdrew from savings successfully'
+ );
is( $savings_account->balance, 200,
- '... got the right savings balance after withdrawl' );
+ '... got the right savings balance after withdrawal' );
$savings_account->deposit(150);
is( $savings_account->balance, 350,
is( $checking_account->balance, 100,
'... got the right checkings balance' );
- ok ! exception {
- $checking_account->withdraw(50);
- },
- '... withdrew from checking successfully';
+ is(
+ exception {
+ $checking_account->withdraw(50);
+ },
+ undef,
+ '... withdrew from checking successfully'
+ );
is( $checking_account->balance, 50,
- '... got the right checkings balance after withdrawl' );
+ '... got the right checkings balance after withdrawal' );
is( $savings_account->balance, 350,
- '... got the right savings balance after checking withdrawl (no overdraft)'
+ '... got the right savings balance after checking withdrawal (no overdraft)'
);
- ok ! exception {
- $checking_account->withdraw(200);
- },
- '... withdrew from checking successfully';
+ is(
+ exception {
+ $checking_account->withdraw(200);
+ },
+ undef,
+ '... withdrew from checking successfully'
+ );
is( $checking_account->balance, 0,
- '... got the right checkings balance after withdrawl' );
+ '... got the right checkings balance after withdrawal' );
is( $savings_account->balance, 200,
- '... got the right savings balance after overdraft withdrawl' );
+ '... got the right savings balance after overdraft withdrawal' );
}
{
is( $checking_account->balance, 100,
'... got the right checkings balance' );
- ok ! exception {
- $checking_account->withdraw(50);
- },
- '... withdrew from checking successfully';
+ is(
+ exception {
+ $checking_account->withdraw(50);
+ },
+ undef,
+ '... withdrew from checking successfully'
+ );
is( $checking_account->balance, 50,
- '... got the right checkings balance after withdrawl' );
-
- ok exception {
- $checking_account->withdraw(200);
- },
- '... withdrawl failed due to attempted overdraft';
+ '... got the right checkings balance after withdrawal' );
+
+ isnt(
+ exception {
+ $checking_account->withdraw(200);
+ },
+ undef,
+ '... withdrawal failed due to attempted overdraft'
+ );
is( $checking_account->balance, 50,
- '... got the right checkings balance after withdrawl failure' );
+ '... got the right checkings balance after withdrawal failure' );
}
=end testing