DEATH TO ALL zionist ELLIPSES
[gitmo/Moose.git] / t / 200_examples / 008_record_set_iterator.t
CommitLineData
638585e1 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
7ff56534 6use Test::More tests => 8;
638585e1 7use Test::Exception;
8
7ff56534 9
638585e1 10
11{
12 package Record;
13 use Moose;
d03bd989 14
638585e1 15 has 'first_name' => (is => 'ro', isa => 'Str');
d03bd989 16 has 'last_name' => (is => 'ro', isa => 'Str');
17
638585e1 18 package RecordSet;
19 use Moose;
d03bd989 20
638585e1 21 has 'data' => (
22 is => 'ro',
d03bd989 23 isa => 'ArrayRef[Record]',
638585e1 24 default => sub { [] },
25 );
d03bd989 26
638585e1 27 has 'index' => (
28 is => 'rw',
d03bd989 29 isa => 'Int',
638585e1 30 default => sub { 0 },
31 );
d03bd989 32
638585e1 33 sub next {
34 my $self = shift;
35 my $i = $self->index;
36 $self->index($i + 1);
37 return $self->data->[$i];
38 }
d03bd989 39
638585e1 40 package RecordSetIterator;
41 use Moose;
42
43 has 'record_set' => (
44 is => 'rw',
45 isa => 'RecordSet',
46 );
47
d03bd989 48 # list the fields you want to
638585e1 49 # fetch from the current record
50 my @fields = Record->meta->get_attribute_list;
51
52 has 'current_record' => (
53 is => 'rw',
d03bd989 54 isa => 'Record',
638585e1 55 lazy => 1,
56 default => sub {
57 my $self = shift;
58 $self->record_set->next() # grab the first one
59 },
60 trigger => sub {
61 my $self = shift;
d03bd989 62 # whenever this attribute is
63 # updated, it will clear all
638585e1 64 # the fields for you.
65 $self->$_() for map { '_clear_' . $_ } @fields;
66 }
67 );
68
d03bd989 69 # define the attributes
638585e1 70 # for all the fields.
71 for my $field (@fields) {
72 has $field => (
73 is => 'ro',
d03bd989 74 isa => 'Any',
638585e1 75 lazy => 1,
d03bd989 76 default => sub {
638585e1 77 my $self = shift;
d03bd989 78 # fetch the value from
638585e1 79 # the current record
80 $self->current_record->$field();
81 },
82 # make sure they have a clearer ..
83 clearer => ('_clear_' . $field)
84 );
85 }
86
87 sub get_next_record {
88 my $self = shift;
89 $self->current_record($self->record_set->next());
90 }
91}
92
93my $rs = RecordSet->new(
94 data => [
95 Record->new(first_name => 'Bill', last_name => 'Smith'),
d03bd989 96 Record->new(first_name => 'Bob', last_name => 'Jones'),
97 Record->new(first_name => 'Jim', last_name => 'Johnson'),
638585e1 98 ]
99);
100isa_ok($rs, 'RecordSet');
101
102my $rsi = RecordSetIterator->new(record_set => $rs);
103isa_ok($rsi, 'RecordSetIterator');
104
1808c2da 105is($rsi->first_name, 'Bill', 'got the right first name');
106is($rsi->last_name, 'Smith', 'got the right last name');
638585e1 107
108$rsi->get_next_record;
109
1808c2da 110is($rsi->first_name, 'Bob', 'got the right first name');
111is($rsi->last_name, 'Jones', 'got the right last name');
638585e1 112
113$rsi->get_next_record;
114
1808c2da 115is($rsi->first_name, 'Jim', 'got the right first name');
116is($rsi->last_name, 'Johnson', 'got the right last name');
638585e1 117
118
119
120
121
122
123
124
125
126
127