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