Revert "Merge remote branch 't0m/json' into json"
[catagits/Gitalist.git] / lib / Gitalist / Git / CollectionOfRepositories.pm
CommitLineData
6b3c0b76 1use MooseX::Declare;
2
cd169152 3role Gitalist::Git::CollectionOfRepositories {
6b3c0b76 4 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
5 use MooseX::Types::Moose qw/ArrayRef/;
6 use Moose::Autobox;
44a9ed75 7 use aliased 'Gitalist::Git::Repository';
6b3c0b76 8
82bc0f05 9 has repositories => (
6b3c0b76 10 is => 'ro',
44a9ed75 11 isa => ArrayRef['Gitalist::Git::Repository'],
6b3c0b76 12 required => 1,
13 lazy_build => 1,
14 );
b5ce0e6a 15 method get_repository (NonEmptySimpleStr $name) {
82bc0f05 16 my $path = $self->_get_path_for_repository_name($name);
44a9ed75 17 die "Not a valid git repository."
6b3c0b76 18 unless $self->_is_git_repo($path);
44a9ed75 19 return Repository->new( $path );
6b3c0b76 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/
82bc0f05 26 _build_repositories
27 _get_path_for_repository_name
6b3c0b76 28 /;
29
82bc0f05 30 around _build_repositories {
6b3c0b76 31 [sort { $a->name cmp $b->name } $self->$orig->flatten];
32 }
33}
34
351;
271f6b46 36
37=head1 NAME
38
39Gitalist::Git::CollectionOfRepositories - Interface and partial implementation of a collection of git repositories
40
41=head1 SYNOPSIS
42
43 package My::Example::CollectionOfRepositories;
44 use Moose::Role;
45 use namespace::autoclean;
46
47 with 'Gitalist::Git::CollectionOfRepositories';
48
49 sub _build_repositories {
50 my $self = shift;
51 [ $self->get_repository('Gitalist') ];
52 }
53 sub _get_path_for_repository_name {
54 my ($self, $name) = @_;
55 '/var/example/' . $name . '.git';
56 }
57
58 my $collection = My::Example::CollectionOfRepositories->new
59 my $repository_list = $collection->repositories;
60 my $only_repository = $repository_list->[0];
61 my $named_repository = $repo->get_repository('Gitalist');
62
63=head1 DESCRIPTION
64
65This role provides an abstraction for a list of Repository directories.
66
67=head1 ATTRIBUTES
68
69=head2 repositories
70
71An array of all L<Gitalist::Git::Repository>s.
72
73=head1 METHODS
74
75=head2 get_repository (Str $name)
76
77Returns a L<Gitalist::Git::Repository> for the given name.
78If C<$name> is not a valid git repository an exception will be thrown.
79
80=head1 SEE ALSO
81
82L<Gitalist::Git::CollectionOfRepositories::FromListOfDirectories>,
83L<Gitalist::Git::CollectionOfRepositories::FromDirectory>,
84L<Gitalist::Git::Repository>.
85
86=head1 AUTHORS
87
88See L<Gitalist> for authors.
89
90=head1 LICENSE
91
92See L<Gitalist> for the license.
93
94=cut
95