Initial refactoring to pull a collectionofprojects role out of ::Git::Repo.
Tomas Doran [Tue, 15 Dec 2009 21:14:16 +0000 (21:14 +0000)]
This now allows ::Git::CollectionOfProjects::FromList to be written which is
what Khisanth wants, and also means that the class whos name we hated
(Gitalist::Git::Repo) naturally gets renamed ::Git::CollectionOfProjects::FromDir
which sounds and feels much better to me. It also means that the config logic
for 'are we working with a dir or list of dirs' becomes the problem of
the Catalyst model code, which feels like a better place for that to be
to me.

lib/Gitalist/Git/CollectionOfProjects.pm [new file with mode: 0644]
lib/Gitalist/Git/Repo.pm

diff --git a/lib/Gitalist/Git/CollectionOfProjects.pm b/lib/Gitalist/Git/CollectionOfProjects.pm
new file mode 100644 (file)
index 0000000..5b111ac
--- /dev/null
@@ -0,0 +1,35 @@
+use MooseX::Declare;
+
+role Gitalist::Git::CollectionOfProjects {
+    use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+    use MooseX::Types::Moose qw/ArrayRef/;
+    use Moose::Autobox;
+    use aliased 'Gitalist::Git::Project';
+
+    has projects => (
+        is => 'ro',
+        isa => ArrayRef['Gitalist::Git::Project'],
+        required => 1,
+        lazy_build => 1,
+    );
+    method get_project (NonEmptySimpleStr $name) {
+        my $path = $self->_get_path_for_project_name($name);
+        die "Not a valid Project"
+            unless $self->_is_git_repo($path);
+        return Project->new( $path );
+    }
+    # Determine whether a given directory is a git repo.
+    method _is_git_repo ($dir) {
+        return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
+    }
+    requires qw/
+        _build_projects
+        _get_path_for_project_name
+    /;
+
+    around _build_projects {
+        [sort { $a->name cmp $b->name } $self->$orig->flatten];
+    }
+}
+
+1;
index 4ea803e..42f4c1d 100644 (file)
@@ -1,10 +1,8 @@
 use MooseX::Declare;
 
-class Gitalist::Git::Repo {
+class Gitalist::Git::Repo with Gitalist::Git::CollectionOfProjects {
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
     use MooseX::Types::Path::Class qw/Dir/;
-    use MooseX::Types::Moose qw/ArrayRef/;
-    use aliased 'Gitalist::Git::Project';
 
     has repo_dir => (
         isa => Dir,
@@ -13,27 +11,17 @@ class Gitalist::Git::Repo {
         coerce => 1,
     );
 
-    has projects => (
-        is => 'ro',
-        isa => ArrayRef['Gitalist::Git::Project'],
-        required => 1,
-        lazy_build => 1,
-    );
-
     method BUILD {
         # Make sure repo_dir is an absolute path so that
         # ->contains() works correctly.
         $self->repo_dir->resolve;
     }
 
-    ## Public methods
-    method get_project (NonEmptySimpleStr $name) {
+    method _get_path_for_project_name (NonEmptySimpleStr $name) {
         my $path = $self->repo_dir->subdir($name)->resolve;
         die "Directory traversal prohibited"
             unless $self->repo_dir->contains($path);
-        die "Not a valid Project"
-            unless $self->_is_git_repo($path);
-        return Project->new( $path );
+        return $path;
     }
 
     ## Builders
@@ -47,14 +35,7 @@ class Gitalist::Git::Repo {
                  push @ret, $p;
             };
          }
-
-        return [sort { $a->name cmp $b->name } @ret];
-    }
-
-    ## Private methods
-    # Determine whether a given directory is a git repo.
-    method _is_git_repo ($dir) {
-        return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
+        return \@ret;
     }
 }                               # end class