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