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