Deal with the singular case better
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
1 use MooseX::Declare;
2
3 class Gitalist::Git::Repo with Gitalist::Git::CollectionOfProjects {
4     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
5     use MooseX::Types::Path::Class qw/Dir/;
6
7     has repo_dir => (
8         isa => Dir,
9         is => 'ro',
10         required => 1,
11         coerce => 1,
12     );
13
14     method BUILD {
15         # Make sure repo_dir is an absolute path so that
16         # ->contains() works correctly.
17         $self->repo_dir->resolve;
18     }
19
20     method _get_path_for_project_name (NonEmptySimpleStr $name) {
21         my $path = $self->repo_dir->subdir($name)->resolve;
22         die "Directory traversal prohibited"
23             unless $self->repo_dir->contains($path);
24         return $path;
25     }
26
27     ## Builders
28     method _build_projects {
29         my $dh = $self->repo_dir->open || die "Could not open repo_dir";
30         my @ret;
31         while (my $dir_entry = $dh->read) {
32             # try to get a project for each entry in repo_dir
33              eval {
34                  my $p = $self->get_project($dir_entry);
35                  push @ret, $p;
36             };
37          }
38         return \@ret;
39     }
40 }                               # end class
41
42 __END__
43
44 =head1 NAME
45
46 Gitalist::Git::Repo - Model of a repository directory
47
48 =head1 SYNOPSIS
49
50     my $repo = Gitalist::Git::Repo->new( repo_dir => $Dir );
51     my $project_list = $repo->projects;
52     my $first_project = $project_list->[0];
53     my $named_project = $repo->get_project('Gitalist');
54
55 =head1 DESCRIPTION
56
57 This class models a Gitalist Repo, which is a collection of
58 Projects (git repositories).  It is used for creating Project
59 objects to work with.
60
61
62 =head1 ATTRIBUTES
63
64 =head2 repo_dir (C<Path::Class::Dir>)
65
66 The filesystem root of the C<Repo>.
67
68 =head2 projects
69
70 An array of all L<Gitalist::Git::Project>s found in C<repo_dir>.
71
72
73
74 =head1 METHODS
75
76 =head2 get_project (Str $name)
77
78 Returns a L<Gitalist::Git::Project> for the given name.
79 If C<$name> is not a valid git repository under C<$repo_dir>, an exception
80 will be thrown.
81
82
83
84 =head1 SEE ALSO
85
86 L<Gitalist::Git::Project>
87
88
89 =head1 AUTHORS
90
91 See L<Gitalist> for authors.
92
93 =head1 LICENSE
94
95 See L<Gitalist> for the license.
96
97 =cut