=item The basis of the TypeContraints module was Rob Kinyon's idea
originally, I just ran with it.
-=item Thanks to mst & chansen and the whole #moose poose for all the
+=item Thanks to mst & chansen and the whole #moose posse for all the
early ideas/feature-requests/encouragement/bug-finding.
=item Thanks to David "Theory" Wheeler for meta-discussions and spelling fixes.
BEGIN {
eval "use Regexp::Common; use Locale::US;";
plan skip_all => "Regexp::Common & Locale::US required for this test" if $@;
- plan tests => 81;
+ plan tests => 66;
}
use Test::Exception;
last_name => 'Little',
title => 'Senior Developer',
address => Address->new(city => 'Madison', state => 'CT')
- ),
- Employee->new(
- first_name => 'Rob',
- last_name => 'Kinyon',
- title => 'Developer',
- address => Address->new(city => 'Marysville', state => 'OH')
- ),
+ ),
]
});
} '... created the entire company successfully';
is($ii->address->state, 'NY', '... got the right state');
is($ii->address->zip_code, 11030, '... got the zip code');
-is($ii->get_employee_count, 4, '... got the right employee count');
+is($ii->get_employee_count, 3, '... got the right employee count');
# employee #1
is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
is($ii->employees->[2]->address->state, 'CT', '... got the right state');
-# employee #4
-
-isa_ok($ii->employees->[3], 'Employee');
-isa_ok($ii->employees->[3], 'Person');
-
-is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
-is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
-ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
-is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
-is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name');
-is($ii->employees->[3]->title, 'Developer', '... got the right title');
-is($ii->employees->[3]->company, $ii, '... got the right company');
-ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
-
-isa_ok($ii->employees->[3]->address, 'Address');
-is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
-is($ii->employees->[3]->address->state, 'OH', '... got the right state');
-
# create new company
my $new_company = Company->new(name => 'Infinity Interactive International');
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 9;
+use Test::Exception;
+
+BEGIN {
+ use_ok('Moose');
+}
+
+{
+ package Record;
+ use Moose;
+
+ has 'first_name' => (is => 'ro', isa => 'Str');
+ has 'last_name' => (is => 'ro', isa => 'Str');
+
+ package RecordSet;
+ use Moose;
+
+ has 'data' => (
+ is => 'ro',
+ isa => 'ArrayRef[Record]',
+ default => sub { [] },
+ );
+
+ has 'index' => (
+ is => 'rw',
+ isa => 'Int',
+ default => sub { 0 },
+ );
+
+ sub next {
+ my $self = shift;
+ my $i = $self->index;
+ $self->index($i + 1);
+ return $self->data->[$i];
+ }
+
+ package RecordSetIterator;
+ use Moose;
+
+ has 'record_set' => (
+ is => 'rw',
+ isa => 'RecordSet',
+ );
+
+ # list the fields you want to
+ # fetch from the current record
+ my @fields = Record->meta->get_attribute_list;
+
+ has 'current_record' => (
+ is => 'rw',
+ isa => 'Record',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ $self->record_set->next() # grab the first one
+ },
+ trigger => sub {
+ my $self = shift;
+ # whenever this attribute is
+ # updated, it will clear all
+ # the fields for you.
+ $self->$_() for map { '_clear_' . $_ } @fields;
+ }
+ );
+
+ # define the attributes
+ # for all the fields.
+ for my $field (@fields) {
+ has $field => (
+ is => 'ro',
+ isa => 'Any',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ # fetch the value from
+ # the current record
+ $self->current_record->$field();
+ },
+ # make sure they have a clearer ..
+ clearer => ('_clear_' . $field)
+ );
+ }
+
+ sub get_next_record {
+ my $self = shift;
+ $self->current_record($self->record_set->next());
+ }
+}
+
+my $rs = RecordSet->new(
+ data => [
+ Record->new(first_name => 'Bill', last_name => 'Smith'),
+ Record->new(first_name => 'Bob', last_name => 'Jones'),
+ Record->new(first_name => 'Jim', last_name => 'Johnson'),
+ ]
+);
+isa_ok($rs, 'RecordSet');
+
+my $rsi = RecordSetIterator->new(record_set => $rs);
+isa_ok($rsi, 'RecordSetIterator');
+
+is($rsi->first_name, 'Bill', '... got the right first name');
+is($rsi->last_name, 'Smith', '... got the right last name');
+
+$rsi->get_next_record;
+
+is($rsi->first_name, 'Bob', '... got the right first name');
+is($rsi->last_name, 'Jones', '... got the right last name');
+
+$rsi->get_next_record;
+
+is($rsi->first_name, 'Jim', '... got the right first name');
+is($rsi->last_name, 'Johnson', '... got the right last name');
+
+
+
+
+
+
+
+
+
+
+