package DBIx::Data::Collection::Set::IndexableBy;

use Moose;
use Method::Signatures::Simple;

extends 'DBIx::Data::Collection::Set';

has _indexed_by => (is => 'ro', default => sub { {} });

has _indexable_by => (is => 'ro', required => 1, init_arg => 'indexable_by');

method indexed_by ($by) {
  if (my $cached = $self->_indexed_by->{$by}) {
    return $cached
  }
  my $index_spec = $self->_indexable_by->{$by};
  die "${self} not indexable by ${by}" unless $index_spec;
  my $new = DBIx::Data::Collection::Set->new($index_spec);
  $self->_indexed_by->{$by} = $new
}

__PACKAGE__->meta->make_immutable;

1;
