cea90509bf117160be19aedf90f3c9a518ec77b0
[catagits/Gitalist.git] / lib / Gitalist / Git / CollectionOfRepositories.pm
1 use MooseX::Declare;
2
3 role Gitalist::Git::CollectionOfRepositories {
4     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
5     use MooseX::Types::Moose qw/ArrayRef/;
6     use Moose::Autobox;
7     use aliased 'Gitalist::Git::Repository';
8
9     has repositories => (
10         is => 'ro',
11         isa => ArrayRef['Gitalist::Git::Repository'],
12         required => 1,
13         lazy_build => 1,
14     );
15     method get_repository (NonEmptySimpleStr $name) {
16         my $path = $self->_get_path_for_repository_name($name);
17         die "Couldn't get_repository '$name' - not a valid git repository."
18             unless $self->_is_git_repo($path);
19         return Repository->new( $path );
20     }
21     # Determine whether a given directory is a git repo.
22     # http://www.kernel.org/pub/software/scm/git/docs/gitrepository-layout.html
23     method _is_git_repo ($dir) {
24         return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
25     }
26     requires qw/
27         _build_repositories
28         _get_path_for_repository_name
29     /;
30
31     around _build_repositories {
32         [sort { $a->name cmp $b->name } $self->$orig->flatten];
33     }
34 }
35
36 1;
37
38 =head1 NAME
39
40 Gitalist::Git::CollectionOfRepositories - Interface and partial implementation of a collection of git repositories
41
42 =head1 SYNOPSIS
43
44     package My::Example::CollectionOfRepositories;
45     use Moose::Role;
46     use namespace::autoclean;
47
48     with 'Gitalist::Git::CollectionOfRepositories';
49
50     sub _build_repositories {
51         my $self = shift;
52         [ $self->get_repository('Gitalist') ];
53     }
54     sub _get_path_for_repository_name {
55         my ($self, $name) = @_;
56         '/var/example/' . $name . '.git';
57     }
58
59     my $collection = My::Example::CollectionOfRepositories->new
60     my $repository_list = $collection->repositories;
61     my $only_repository = $repository_list->[0];
62     my $named_repository = $repo->get_repository('Gitalist');
63
64 =head1 DESCRIPTION
65
66 This role provides an abstraction for a list of Repository directories.
67
68 =head1 ATTRIBUTES
69
70 =head2 repositories
71
72 An array of all L<Gitalist::Git::Repository>s.
73
74 =head1 METHODS
75
76 =head2 get_repository (Str $name)
77
78 Returns a L<Gitalist::Git::Repository> for the given name.
79 If C<$name> is not a valid git repository an exception will be thrown.
80
81 =head1 SEE ALSO
82
83 L<Gitalist::Git::CollectionOfRepositories::FromListOfDirectories>,
84 L<Gitalist::Git::CollectionOfRepositories::FromDirectory>,
85 L<Gitalist::Git::Repository>.
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
96