Revert "Merge remote branch 't0m/json' into json"
[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 "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     method _is_git_repo ($dir) {
23         return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
24     }
25     requires qw/
26         _build_repositories
27         _get_path_for_repository_name
28     /;
29
30     around _build_repositories {
31         [sort { $a->name cmp $b->name } $self->$orig->flatten];
32     }
33 }
34
35 1;
36
37 =head1 NAME
38
39 Gitalist::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
65 This role provides an abstraction for a list of Repository directories.
66
67 =head1 ATTRIBUTES
68
69 =head2 repositories
70
71 An array of all L<Gitalist::Git::Repository>s.
72
73 =head1 METHODS
74
75 =head2 get_repository (Str $name)
76
77 Returns a L<Gitalist::Git::Repository> for the given name.
78 If C<$name> is not a valid git repository an exception will be thrown.
79
80 =head1 SEE ALSO
81
82 L<Gitalist::Git::CollectionOfRepositories::FromListOfDirectories>,
83 L<Gitalist::Git::CollectionOfRepositories::FromDirectory>,
84 L<Gitalist::Git::Repository>.
85
86 =head1 AUTHORS
87
88 See L<Gitalist> for authors.
89
90 =head1 LICENSE
91
92 See L<Gitalist> for the license.
93
94 =cut
95