And support a list of directories in the Catalyst model as config
Tomas Doran [Tue, 15 Dec 2009 21:42:25 +0000 (21:42 +0000)]
This is currently untested, but it is what I was aiming for, so if other
people are ok with this then I'll rename ::GitRepos to be in line, add docs
etc etc.

lib/Gitalist/Git/CollectionOfProjects/FromListOfDirectories.pm [new file with mode: 0644]
lib/Gitalist/Model/GitRepos.pm

diff --git a/lib/Gitalist/Git/CollectionOfProjects/FromListOfDirectories.pm b/lib/Gitalist/Git/CollectionOfProjects/FromListOfDirectories.pm
new file mode 100644 (file)
index 0000000..464a539
--- /dev/null
@@ -0,0 +1,34 @@
+use MooseX::Declare;
+
+class Gitalist::Git::CollectionOfProjects::FromListOfDirectories with Gitalist::Git::CollectionOfProjects {
+    use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+    use MooseX::Types::Moose qw/ ArrayRef HashRef /;
+    use File::Basename qw/basename/;
+    use Moose::Autobox;
+
+    has repos => (
+        isa => ArrayRef[NonEmptySimpleStr],
+        is => 'ro',
+        required => 1,
+    );
+    has repos_by_name => (
+        isa => HashRef[NonEmptySimpleStr],
+        is => 'ro',
+        lazy_build => 1,
+        traits => ['Hash'],
+        handles => {
+            _get_path_for_project_name => 'get',
+        },
+    );
+
+    method _build_repos_by_name {
+        { map { basename($_) => $_ } $self->repos->flatten };
+    }
+
+    ## Builders
+    method _build_projects {
+        [ map { $self->get_project($_) } $self->repos->flatten ];
+    }
+}                               # end class
+
+1;
index f988d8c..4b9ed85 100644 (file)
@@ -2,6 +2,8 @@ package Gitalist::Model::GitRepos;
 
 use Moose;
 use Gitalist::Git::Repo;
+use Gitalist::Git::CollectionOfProjects::FromListOfDirectories;
+use MooseX::Types::Moose qw/Maybe ArrayRef/;
 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
 use Moose::Util::TypeConstraints;
 use namespace::autoclean;
@@ -22,11 +24,21 @@ has config_repo_dir => (
 );
 
 has repo_dir => (
-    isa => $repo_dir_t,
+    isa => Maybe[$repo_dir_t],
     is => 'ro',
     lazy_build => 1
 );
 
+has repos => (
+    isa => ArrayRef[$repo_dir_t],
+    is => 'ro',
+    default => sub { [] },
+    traits => ['Array'],
+    handles => {
+        _repos_count => 'count',
+    },
+);
+
 sub _build_repo_dir {
     my $self = shift;
     $ENV{GITALIST_REPO_DIR} ?
@@ -38,14 +50,19 @@ sub _build_repo_dir {
 
 after BUILD => sub {
     my $self = shift;
-    $self->repo_dir; # Explode loudly at app startup time if there is no repos
-                     # dir, rather than on first hit
+    # Explode loudly at app startup time if there is no list of
+    # projects or repos dir, rather than on first hit
+    $self->_repos_count || $self->repo_dir;
 };
 
 sub build_per_context_instance {
     my ($self, $app) = @_;
-
-    Gitalist::Git::Repo->new(repo_dir => $self->repo_dir);
+    if ($self->_repos_count) {
+        Gitalist::Git::CollectionOfProjects::FromListOfDirectories->new(repos => $self->repos);
+    }
+    else {
+        Gitalist::Git::Repo->new(repo_dir => $self->repo_dir);
+    }
 }
 
 __PACKAGE__->meta->make_immutable;