Add exercises for section 2
Dave Rolsky [Mon, 8 Jun 2009 18:49:13 +0000 (13:49 -0500)]
moose-class/exercises/answers/02-roles/Employee.pm [new file with mode: 0644]
moose-class/exercises/answers/02-roles/HasAccount.pm [new file with mode: 0644]
moose-class/exercises/answers/02-roles/Person.pm [new file with mode: 0644]
moose-class/exercises/answers/02-roles/Printable.pm [new file with mode: 0644]
moose-class/exercises/t/02-roles.t [new file with mode: 0644]

diff --git a/moose-class/exercises/answers/02-roles/Employee.pm b/moose-class/exercises/answers/02-roles/Employee.pm
new file mode 100644 (file)
index 0000000..6c301fc
--- /dev/null
@@ -0,0 +1,21 @@
+package Employee;
+
+use Moose;
+
+extends 'Person';
+
+has position => ( is => 'rw' );
+has salary   => ( is => 'rw' );
+has ssn      => ( is => 'ro' );
+
+override full_name => sub {
+    my $self = shift;
+
+    return super() . q[ (] . $self->position . q[)];
+};
+
+no Moose;
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/moose-class/exercises/answers/02-roles/HasAccount.pm b/moose-class/exercises/answers/02-roles/HasAccount.pm
new file mode 100644 (file)
index 0000000..4a4bd68
--- /dev/null
@@ -0,0 +1,28 @@
+package HasAccount;
+
+use Moose::Role;
+
+has balance => (
+    is => 'rw',
+);
+
+sub deposit {
+    my $self   = shift;
+    my $amount = shift;
+
+    $self->balance( $self->balance + $amount );
+}
+
+sub withdraw {
+    my $self   = shift;
+    my $amount = shift;
+
+    die "Balance cannot be negative"
+        if $self->balance < $amount;
+
+    $self->balance( $self->balance - $amount );
+}
+
+no Moose::Role;
+
+1;
diff --git a/moose-class/exercises/answers/02-roles/Person.pm b/moose-class/exercises/answers/02-roles/Person.pm
new file mode 100644 (file)
index 0000000..e3b718a
--- /dev/null
@@ -0,0 +1,22 @@
+package Person;
+
+use Moose;
+
+with 'Printable', 'HasAccount';
+
+has first_name => ( is => 'rw' );
+has last_name  => ( is => 'rw' );
+
+sub full_name {
+    my $self = shift;
+
+    return join q{ }, $self->first_name, $self->last_name;
+}
+
+sub as_string { $_[0]->full_name }
+
+no Moose;
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/moose-class/exercises/answers/02-roles/Printable.pm b/moose-class/exercises/answers/02-roles/Printable.pm
new file mode 100644 (file)
index 0000000..cb9b58c
--- /dev/null
@@ -0,0 +1,9 @@
+package Printable;
+
+use Moose::Role;
+
+requires 'as_string';
+
+no Moose::Role;
+
+1;
diff --git a/moose-class/exercises/t/02-roles.t b/moose-class/exercises/t/02-roles.t
new file mode 100644 (file)
index 0000000..ebc6ddc
--- /dev/null
@@ -0,0 +1,31 @@
+# Your tasks ...
+#
+# Create a Printable role. This role should simply require that the
+# consuming class implement an "as_string" method.
+#
+# Make the Person class from the last set of exercises consume this
+# role. Use printable_name as the output for the as_string method. The
+# Employee subclass should still override this output.
+#
+# Implement a role HasMoney. This should provide a read-write
+# "balance" attribute. It should also implement "deposit" and
+# "withdraw" methods. Attempting to reduce the cash balance below 0
+# via "withdraw" should die with an error that includes the string:
+#
+#  Balance cannot be negative
+#
+# Make the Person class consumes this role as well.
+#
+# Make sure all roles are free of Moose droppings.
+
+use strict;
+use warnings;
+
+use lib 't/lib';
+
+use MooseClass::Tests;
+
+use Person;
+use Employee;
+
+MooseClass::Tests::tests02();