Add a HasUtils role, so that ::Repo can have utils also, and use it in ::Project...
Tomas Doran [Sun, 8 Nov 2009 23:49:42 +0000 (23:49 +0000)]
lib/Gitalist/Git/HasUtils.pm [new file with mode: 0644]
lib/Gitalist/Git/Project.pm
lib/Gitalist/Git/Repo.pm
lib/Gitalist/Git/Util.pm

diff --git a/lib/Gitalist/Git/HasUtils.pm b/lib/Gitalist/Git/HasUtils.pm
new file mode 100644 (file)
index 0000000..9560bf4
--- /dev/null
@@ -0,0 +1,21 @@
+package Gitalist::Git::HasUtils;
+use Moose::Role;
+use Gitalist::Git::Util;
+use namespace::autoclean;
+
+sub BUILD {}
+after BUILD => sub {
+    my $self = shift;
+    # Force value build. A little convoluted as we don't have an accessor :)
+    $self->_util;
+};
+
+has _util => ( isa => 'Gitalist::Git::Util',
+               is => 'ro',
+               lazy_build => 1,
+               handles => [ 'run_cmd', 'get_gpp_object' ],
+           );
+
+sub _build__util { confess(shift() . " cannot build _util") }
+
+1;
index b0d45a8..bfb92fa 100644 (file)
@@ -1,13 +1,12 @@
 use MooseX::Declare;
 
-class Gitalist::Git::Project {
+class Gitalist::Git::Project with Gitalist::Git::HasUtils {
     # FIXME, use Types::Path::Class and coerce
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
     use MooseX::Types::Moose qw/Str Maybe Bool HashRef/;
     use DateTime;
     use MooseX::Types::Path::Class qw/Dir/;
     use List::MoreUtils qw/any zip/;
-    use Gitalist::Git::Util;
     use aliased 'Gitalist::Git::Object';
 
     our $SHA1RE = qr/[0-9a-fA-F]{40}/;
@@ -29,11 +28,6 @@ class Gitalist::Git::Project {
                          is => 'ro',
                          lazy_build => 1,
                      );
-    has _util => ( isa => 'Gitalist::Git::Util',
-                   is => 'ro',
-                   lazy_build => 1,
-                   handles => [ 'run_cmd', 'get_gpp_object' ],
-               );
 
     has project_dir => ( isa => Dir,
         is => 'ro',
@@ -61,7 +55,7 @@ class Gitalist::Git::Project {
     );
 
     method BUILD {
-        $self->$_() for qw/_util last_change owner description/; # Ensure to build early.
+        $self->$_() for qw/last_change owner description/; # Ensure to build early.
     }
 
     method _project_dir {
index 4146575..3348bdd 100644 (file)
@@ -1,10 +1,17 @@
 use MooseX::Declare;
 
-class Gitalist::Git::Repo {
+class Gitalist::Git::Repo with Gitalist::Git::HasUtils {
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
     use MooseX::Types::Path::Class qw/Dir/;
     use MooseX::Types::Moose qw/ArrayRef/;
-    use Gitalist::Git::Project;
+    use aliased 'Gitalist::Git::Project';
+
+    # FIXME - this is nasty as we build the Git::Utils thing without a project name
+    #         should refactor or something?
+    method _build__util {
+        Gitalist::Git::Util->new();
+    }
+
     has repo_dir => (
         isa => Dir,
         is => 'ro',
@@ -13,7 +20,7 @@ class Gitalist::Git::Repo {
     );
 
     method project (NonEmptySimpleStr $project) {
-        return Gitalist::Git::Project->new(
+        return Project->new(
             name => $project,
             path => $self->repo_dir->subdir($project),
         );
@@ -55,10 +62,7 @@ each item will contain the contents of L</project_info>.
             next unless -d $obj;
             next unless $self->_is_git_repo($obj);
 
-            push @ret, Gitalist::Git::Project->new(
-                name => $file,
-                path => $obj,
-            );
+            push @ret, $self->project($file);
         }
 
         return [sort { $a->name cmp $b->name } @ret];
index a38eb70..846b567 100644 (file)
@@ -9,6 +9,7 @@ class Gitalist::Git::Util {
         handles => { gitdir => 'project_dir' },
         is => 'bare', # No accessor
         weak_ref => 1, # Weak, you have to hold onto me.
+        predicate => 'has_project',
     );
     has _git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
     sub _build__git {
@@ -26,11 +27,17 @@ EOR
 
     has _gpp      => (
         isa => 'Git::PurePerl', is => 'ro', lazy => 1,
-        default => sub { Git::PurePerl->new(gitdir => shift->gitdir) },
+        default => sub {
+            my $self = shift;
+            confess("Cannot get gpp without project")
+                unless $self->has_project;
+            Git::PurePerl->new(gitdir => $self->gitdir);
+        },
     );
 
     method run_cmd (@args) {
-        unshift @args, ( '--git-dir' => $self->gitdir );
+        unshift @args, ( '--git-dir' => $self->gitdir )
+            if $self->has_project;
 #        print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
 
         open my $fh, '-|', $self->_git, @args