Allow the model to be user defined.
[catagits/Gitalist.git] / lib / Gitalist / Model / CollectionOfRepos.pm
index 84e107d..1e709c4 100644 (file)
@@ -3,6 +3,7 @@ package Gitalist::Model::CollectionOfRepos;
 use Moose;
 use Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive;
 use Gitalist::Git::CollectionOfRepositories::FromListOfDirectories;
+use Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList;
 use MooseX::Types::Moose qw/Maybe ArrayRef/;
 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
 use Moose::Util::TypeConstraints;
@@ -49,11 +50,33 @@ has repos => (
     coerce => 1,
 );
 
+has class => (
+    isa => NonEmptySimpleStr,
+    is  => 'ro',
+);
+
+has args => (
+    isa     => 'HashRef',
+    is      => 'ro',
+    default => sub { {} },
+);
+
+has search_recursively => (
+    is      => 'ro',
+    isa     => 'Bool',
+    default => 0,
+);
+
 has export_ok => (
     is  => 'ro',
     isa => 'Str',
 );
 
+has whitelist => (
+    is  => 'ro',
+    isa => 'Str',
+);
+
 sub _build_repo_dir {
     my $self = shift;
     $ENV{GITALIST_REPO_DIR} ?
@@ -70,19 +93,40 @@ after BUILD => sub {
     $self->_repos_count || $self->repo_dir;
 };
 
+sub _default_model_class {
+    my($self) = @_;
+
+    if($self->whitelist && -f $self->whitelist) {
+        return 'FromDirectory::WhiteList';
+    } elsif ($self->_repos_count && !$self->search_recursively) {
+        return 'FromListOfDirectories';
+    } elsif($self->search_recursively) {
+        return 'FromDirectoryRecursive';
+    }
+
+    return 'FromDirectory';
+}
+
 sub build_per_context_instance {
     my ($self, $app) = @_;
 
-    my %args = (export_ok => $self->export_ok || '');
-    my $class;
-    if ($self->_repos_count) {
-        $class = 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
-        $args{repos} = $self->repos;
-    }
-    else {
-        $class = 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
-        $args{repo_dir} = $self->repo_dir;
-    }
+    my %args = (
+        export_ok => $self->export_ok || '',
+        %{ $self->args }
+    );
+
+    my $class = $self->class;
+    Class::MOP::load_class($class) if $class;
+
+    my $default = $self->_default_model_class;
+
+    $args{whitelist} = $self->whitelist if $default eq 'FromDirectory::WhiteList';
+    $args{repos}     = $self->repos     if $default eq 'FromListOfDirectories';
+    $args{repo_dir}  = $self->repo_dir  if $default =~ /\b(?:WhiteList|FromDirectory(?:Recursive)?)$/;
+
+    $class ||= "Gitalist::Git::CollectionOfRepositories::$default";
+
+    $app->log->debug("Using class '$class'");
 
     return $class->new(%args);
 }
@@ -91,6 +135,12 @@ __PACKAGE__->meta->make_immutable;
 
 __END__
 
+=encoding UTF-8
+
+=head1 NAME
+
+Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist
+
 =head1 AUTHORS
 
 See L<Gitalist> for authors.