indexed by
[dbsrgits/DBIx-Data-Store-old.git] / lib / DBIx / Data / Collection / Set / IndexableBy.pm
CommitLineData
9f2b6cc8 1package DBIx::Data::Collection::Set::IndexableBy;
2
3use Moose;
4use Method::Signatures::Simple;
5
6extends 'DBIx::Data::Collection::Set';
7
8has _indexed_by => (is => 'ro', default => sub { {} });
9
10has _indexable_by => (is => 'ro', required => 1, init_arg => 'indexable_by');
11
12method indexed_by ($by) {
13 if (my $cached = $self->_indexed_by->{$by}) {
14 return $cached
15 }
16 my $index_spec = $self->_indexable_by->{$by};
17 die "${self} not indexable by ${by}" unless $index_spec;
18 my $new = DBIx::Data::Collection::Set->new($index_spec);
50166086 19 $self->_connect_caches($new);
9f2b6cc8 20 $self->_indexed_by->{$by} = $new
21}
22
50166086 23method _connect_caches ($new) {
24 $new->_set_member_cache($self->_member_cache) if $self->_member_cache_built;
25 $new->_add_to_caches($_) for $self->_all_key_cache_members;
26 $new->_setup_observation_of($self);
27 $self->_setup_observation_of($new);
28}
29
9f2b6cc8 30__PACKAGE__->meta->make_immutable;
31
321;