Initial refactoring to pull a collectionofprojects role out of ::Git::Repo.
[catagits/Gitalist.git] / lib / Gitalist / Git / CollectionOfProjects.pm
CommitLineData
6b3c0b76 1use MooseX::Declare;
2
3role Gitalist::Git::CollectionOfProjects {
4 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
5 use MooseX::Types::Moose qw/ArrayRef/;
6 use Moose::Autobox;
7 use aliased 'Gitalist::Git::Project';
8
9 has projects => (
10 is => 'ro',
11 isa => ArrayRef['Gitalist::Git::Project'],
12 required => 1,
13 lazy_build => 1,
14 );
15 method get_project (NonEmptySimpleStr $name) {
16 my $path = $self->_get_path_for_project_name($name);
17 die "Not a valid Project"
18 unless $self->_is_git_repo($path);
19 return Project->new( $path );
20 }
21 # Determine whether a given directory is a git repo.
22 method _is_git_repo ($dir) {
23 return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
24 }
25 requires qw/
26 _build_projects
27 _get_path_for_project_name
28 /;
29
30 around _build_projects {
31 [sort { $a->name cmp $b->name } $self->$orig->flatten];
32 }
33}
34
351;