aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/InterfaceModel/Collection/DBIC/Role/Base.pm
blob: 2da485c1d0d50518a172856a7fd76afdcb693e62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package Reaction::InterfaceModel::Collection::DBIC::Role::Base;

use Reaction::Role;
use Scalar::Util qw/blessed/;
use Class::MOP;

# WARNING - DANGER: this is just an RFC, please DO NOT USE YET

role Base, which {

  has '_source_resultset' => (
                             is => 'ro',
                             required => 1,
                             isa => 'DBIx::Class::ResultSet',
                            );

  has '_im_class' => (
                      is         => 'ro',
                      isa        => 'Str',
                      lazy_build => 1,
                     );

  #implements BUILD => as {
  #  my $self = shift;
  #  Class::MOP::load_class($self->_im_class);
  #  confess "_im_result_class must be a Reaction::InterfaceModel::Object"
  #    unless $self->_im_class->isa("Reaction::InterfaceModel::Object");
  #  confess "_im_result_class must have an inflate_result method"
  #    unless $self->_im_class->can("inflate_result");
  #};

  #Oh man. I have a bad feeling about this one.
  implements _build_im_class => as {
    my $self = shift;
    my $class = blessed $self || $self;
    $class =~ s/::Collection$//;
    return $class;
  };

  implements _build_collection_store => as {
    my $self = shift;
    my $im_class = $self->_im_class;
    [ $self->_source_resultset->search({}, {result_class => $im_class})->all ];
  };

  implements clone => as {
    my $self = shift;
    my $rs = $self->_source_resultset->search_rs({});
    #should the clone include the arrayref of IM::Objects too?
    return (blessed $self)->new(
                                _source_resultset => $rs,
                                _im_class => $self->_im_class, @_
                               );
  };

  implements count_members => as {
    my $self = shift;
    $self->_source_resultset->count;
  };

  implements add_member => as {
    confess "Not yet implemented";
  };

  implements remove_member => as {
    confess "Not yet implemented";
  };

};

1;


=head1 NAME

Reaction::InterfaceModel::Collection::DBIC::Role::Base

=head1 DESCRIPTION

Provides methods to allow a collection to be populated by a L<DBIx::Class::ResultSet>

=head1 Attributes

=head2 _source_resultset

Required, Read-only. Contains the L<DBIx::Class::ResultSet> used to populate the
collection.

=head2 _im_class

Read-only, lazy_build. The name of the IM Object Class that the resultset inside this
collection will inflate to. Predicate: C<_has_im_class>

=head1 METHODS

=head2 clone

Returns a clone of the current collection, complete with a cloned C<_source_resultset>

=head2 count_members

Returns the number of items found by the ResultSet

=head2 add_member

=head2 remove_member

These will die as they have not been implemented yet.

=head1 PRIVATE METHODS

=head2 _build_im_class

Will attempt to remove the suffix "Collection" from the current class name and return
that. I.e. C<MyApp::MyIM::Roles::Collection> would return C<MyApp::MyIM::Roles>

=head2 _build_collection_store

Replace the default builder to populate the collection with all results returned by the
resultset.

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut