Lose some code by not converting to/from path::class objects, and also by flattening...
[catagits/Gitalist.git] / lib / Gitalist / Git / Project.pm
index 5209149..de3f05f 100644 (file)
@@ -3,19 +3,20 @@ use MooseX::Declare;
 class Gitalist::Git::Project {
     # FIXME, use Types::Path::Class and coerce
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+    use MooseX::Types::Moose qw/Str Maybe/;
     use DateTime;
     use Path::Class;
     use Gitalist::Git::Util;
     use aliased 'Gitalist::Git::Object';
 
     our $SHA1RE = qr/[0-9a-fA-F]{40}/;
-    
+
     has name => ( isa => NonEmptySimpleStr,
-                  is => 'ro' );
+                  is => 'ro', required => 1 );
     has path => ( isa => "Path::Class::Dir",
-                  is => 'ro');
+                  is => 'ro', required => 1);
 
-    has description => ( isa => NonEmptySimpleStr,
+    has description => ( isa => Str,
                          is => 'ro',
                          lazy_build => 1,
                      );
@@ -23,7 +24,7 @@ class Gitalist::Git::Project {
                    is => 'ro',
                    lazy_build => 1,
                );
-    has last_change => ( isa => 'DateTime',
+    has last_change => ( isa => Maybe['DateTime'],
                          is => 'ro',
                          lazy_build => 1,
                      );
@@ -33,25 +34,31 @@ class Gitalist::Git::Project {
                    handles => [ 'run_cmd' ],
                );
 
+    method BUILD {
+        $self->$_() for qw/_util last_change owner description/; # Ensure to build early.
+    }
+
     method _build__util {
-        my $util = Gitalist::Git::Util->new(
+        Gitalist::Git::Util->new(
             gitdir => $self->path,
         );
-        return $util;
     }
-    
+
     method _build_description {
-        my $description = $self->path->file('description')->slurp;
-        chomp $description;
+        my $description = "";
+        eval {
+            $description = $self->path->file('description')->slurp;
+            chomp $description;
+        };
         return $description;
     }
 
     method _build_owner {
-        my $owner = (getpwuid $self->path->stat->uid)[6];
-        $owner =~ s/,+$//;
-        return $owner;
+        my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
+        $gecos =~ s/,+$//;
+        return length($gecos) ? $gecos : $name;
     }
-    
+
     method _build_last_change {
         my $last_change;
         my $output = $self->run_cmd(
@@ -106,22 +113,15 @@ The keys for each item will be:
                                     type => $type,
                                     sha1 => $object,
                                     file => $file,
+                                    project => $self,
                                   );
         }
         return @ret;
     }
 
-
-    method project_dir (Path::Class::Dir $project) {
-        my $dir = $project->stringify;
-        $dir .= '/.git'
-            if -f dir($dir)->file('.git/HEAD');
-        return $dir;
-    }
-
     # Compatibility
 
-=head2 project_info
+=head2 info
 
 Returns a hash containing properties of this project. The keys will
 be:
@@ -133,7 +133,7 @@ be:
 
 =cut
 
-    method project_info {
+    method info {
         return {
             name => $self->name,
             description => $self->description,
@@ -141,5 +141,5 @@ be:
             last_change => $self->last_change,
         };
     };
-    
+
 } # end class