r20898@agaton (orig r755): wreis | 2008-07-24 00:32:56 +0100
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Collection.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Collection;
2
3use Reaction::Class;
4use Scalar::Util qw/blessed/;
5use aliased 'Reaction::InterfaceModel::Collection' => 'IM_Collection';
6use aliased 'Reaction::UI::ViewPort::Object';
7
8class Collection is 'Reaction::UI::ViewPort', which {
9
10 has members => (is => 'rw', isa => 'ArrayRef', lazy_build => 1);
11
12 has collection => (is => 'ro', isa => IM_Collection, required => 1);
13 has current_collection => (is => 'rw', isa => IM_Collection, lazy_build => 1);
14
15 has member_args => ( is => 'rw', isa => 'HashRef', lazy_build => 1);
16 has member_class => ( is => 'ro', isa => 'Str', lazy_build => 1);
17
18 implements BUILD => as {
19 my ($self, $args) = @_;
504e2c44 20 if( my $member_args = delete $args->{Member} ){
21 $self->member_args( $member_args );
22 }
ddccc6a2 23 };
24
c8fbb8ad 25 implements _build_member_args => as{ {} };
26
ddccc6a2 27 implements _build_member_class => as{ Object };
28
29 after clear_current_collection => sub{
c8fbb8ad 30 shift->clear_members; #clear the members the current collection changes, duh
ddccc6a2 31 };
32
33 implements _build_current_collection => as {
6f39e2d4 34 return $_[0]->collection;
ddccc6a2 35 };
36
c8fbb8ad 37 #I'm not really sure why this is here all of a sudden.
38 implements model => as { shift->current_collection };
ddccc6a2 39
40 implements _build_members => as {
41 my ($self) = @_;
42 my (@members, $i);
43 my $args = $self->member_args;
44 my $builders = {};
45 my $ctx = $self->ctx;
46 my $loc = join('-', $self->location, 'member');
47 my $class = $self->member_class;
48
49 #replace $i with a real unique identifier so that we don't run a risk of
50 # events being passed down to the wrong viewport. for now i disabled event
51 # passing until i fix this (groditi)
52 for my $obj ( $self->current_collection->members ) {
53 my $type = blessed $obj;
54 my $builder_cache = $builders->{$type} ||= {};
55 my $member = $class->new(
56 ctx => $ctx,
c8fbb8ad 57 model => $obj,
ddccc6a2 58 location => join('-', $loc, $i++),
59 builder_cache => $builder_cache,
60 %$args
61 );
62 push(@members, $member);
63 }
64 return \@members;
65 };
66
67};
68
2dba7201 691;
ddccc6a2 70
2dba7201 71__END__;
ddccc6a2 72
2dba7201 73=head1 NAME
74
75Reaction::UI::ViewPort::Collection
76
77=head1 DESCRIPTION
78
79Creates, from an InterfaceModel::Collection, a list of viewports representing each
80member of the collection.
81
82=head1 ATTRIBUTES
83
84=head2 collection
85
86=head2 current_collection
87
88=head2 member_args
89
90=head2 member_class
91
92=head1
93
94=head1 INTERNAL METHODS
95
96These methods, although stable, are subject to change without notice. These are meant
97to be used only by developers. End users should refrain from using these methods to
98avoid potential breakages.
99
100=head2 BUILD
101
102=head2 get_builder_for
103
104=head2 model
105
106=head1 AUTHORS
107
108See L<Reaction::Class> for authors.
109
110=head1 LICENSE
111
112See L<Reaction::Class> for the license.
113
114=cut