work in progress, listview still broken
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Collection / DBIC / Role / Base.pm
CommitLineData
7adfd53f 1package Reaction::InterfaceModel::Collection::DBIC::Role::Base;
2
3use Reaction::Role;
4use Scalar::Util qw/blessed/;
5use Class::MOP;
6
7# WARNING - DANGER: this is just an RFC, please DO NOT USE YET
8
9role Base, which {
10
11 has '_source_resultset' => (
12 is => 'ro',
13 required => 1,
14 isa => 'DBIx::Class::ResultSet',
15 );
16
c8fbb8ad 17 has 'member_type' => (is => 'ro', isa => 'ClassName', lazy_build => 1);
18
7adfd53f 19
20 #implements BUILD => as {
21 # my $self = shift;
22 # Class::MOP::load_class($self->_im_class);
23 # confess "_im_result_class must be a Reaction::InterfaceModel::Object"
24 # unless $self->_im_class->isa("Reaction::InterfaceModel::Object");
25 # confess "_im_result_class must have an inflate_result method"
26 # unless $self->_im_class->can("inflate_result");
27 #};
28
c8fbb8ad 29
30
7adfd53f 31 #Oh man. I have a bad feeling about this one.
c8fbb8ad 32 implements _build_member_type => as {
7adfd53f 33 my $self = shift;
89939ff9 34 my $class = blessed($self) || $self;
7adfd53f 35 $class =~ s/::Collection$//;
36 return $class;
37 };
38
89939ff9 39 implements _build__collection_store => as {
7adfd53f 40 my $self = shift;
c8fbb8ad 41 [ $self->_source_resultset->search({}, {result_class => $self->member_type})->all ];
7adfd53f 42 };
43
44 implements clone => as {
45 my $self = shift;
46 my $rs = $self->_source_resultset->search_rs({});
47 #should the clone include the arrayref of IM::Objects too?
48 return (blessed $self)->new(
49 _source_resultset => $rs,
c8fbb8ad 50 member_type => $self->member_type, @_
7adfd53f 51 );
52 };
53
54 implements count_members => as {
55 my $self = shift;
56 $self->_source_resultset->count;
57 };
58
59 implements add_member => as {
60 confess "Not yet implemented";
61 };
62
63 implements remove_member => as {
64 confess "Not yet implemented";
65 };
66
f670cfd0 67
68 implements page => as {
69 my $self = shift;
70 my $rs = $self->_source_resultset->page(@_);
71 return (blessed $self)->new(
72 _source_resultset => $rs,
c8fbb8ad 73 member_type => $self->member_type,
f670cfd0 74 );
75 };
76
77 implements pager => as {
78 my $self = shift;
79 return $self->_source_resultset->pager(@_);
80 };
81
7adfd53f 82};
83
841;
85
86
87=head1 NAME
88
89Reaction::InterfaceModel::Collection::DBIC::Role::Base
90
91=head1 DESCRIPTION
92
93Provides methods to allow a collection to be populated by a L<DBIx::Class::ResultSet>
94
95=head1 Attributes
96
97=head2 _source_resultset
98
99Required, Read-only. Contains the L<DBIx::Class::ResultSet> used to populate the
100collection.
101
c8fbb8ad 102=head2 member_type
7adfd53f 103
104Read-only, lazy_build. The name of the IM Object Class that the resultset inside this
c8fbb8ad 105collection will inflate to. Predicate: C<has_member_type>
7adfd53f 106
107=head1 METHODS
108
109=head2 clone
110
111Returns a clone of the current collection, complete with a cloned C<_source_resultset>
112
113=head2 count_members
114
115Returns the number of items found by the ResultSet
116
117=head2 add_member
118
119=head2 remove_member
120
121These will die as they have not been implemented yet.
122
123=head1 PRIVATE METHODS
124
125=head2 _build_im_class
126
127Will attempt to remove the suffix "Collection" from the current class name and return
128that. I.e. C<MyApp::MyIM::Roles::Collection> would return C<MyApp::MyIM::Roles>
129
130=head2 _build_collection_store
131
132Replace the default builder to populate the collection with all results returned by the
133resultset.
134
135=head1 AUTHORS
136
137See L<Reaction::Class> for authors.
138
139=head1 LICENSE
140
141See L<Reaction::Class> for the license.
142
143=cut