Simplified Repository BUILDARGS logic.
[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
16     has export_ok => (
17         is => 'ro',
18         isa => 'Str',
19     );
20
21     method get_repository (NonEmptySimpleStr $name) {
22         my $repo = $self->_get_repo_from_name($name);
23         confess("Couldn't get_repository '$name' - not a valid git repository.")
24             unless $self->_is_git_repo($repo->path);
25         return $repo;
26     }
27
28     # Determine whether a given directory is a git repo.
29     # http://www.kernel.org/pub/software/scm/git/docs/gitrepository-layout.html
30     method _is_git_repo ($dir) {
31         my $has_head   = -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
32         my $eok_file   = $self->export_ok
33              or return $has_head;
34         my $is_visible = $eok_file
35              && (-f $dir->file($eok_file) || -f $dir->file('.git', $eok_file));
36
37         return $has_head && $is_visible;
38     }
39     requires qw/
40         _build_repositories
41         _get_repo_from_name
42     /;
43
44     around _build_repositories {
45         [sort { $a->name cmp $b->name } $self->$orig->flatten];
46     }
47 }
48
49 1;
50
51 =head1 NAME
52
53 Gitalist::Git::CollectionOfRepositories - Interface and partial implementation of a collection of git repositories
54
55 =head1 SYNOPSIS
56
57     package My::Example::CollectionOfRepositories;
58     use Moose::Role;
59     use namespace::autoclean;
60
61     with 'Gitalist::Git::CollectionOfRepositories';
62
63     sub _build_repositories {
64         my $self = shift;
65         [ $self->get_repository('Gitalist') ];
66     }
67     sub _get_path_for_repository_name {
68         my ($self, $name) = @_;
69         '/var/example/' . $name . '.git';
70     }
71
72     my $collection = My::Example::CollectionOfRepositories->new
73     my $repository_list = $collection->repositories;
74     my $only_repository = $repository_list->[0];
75     my $named_repository = $repo->get_repository('Gitalist');
76
77 =head1 DESCRIPTION
78
79 This role provides an abstraction for a list of Repository directories.
80
81 =head1 ATTRIBUTES
82
83 =head2 repositories
84
85 An array of all L<Gitalist::Git::Repository>s.
86
87 =head1 METHODS
88
89 =head2 get_repository (Str $name)
90
91 Returns a L<Gitalist::Git::Repository> for the given name.
92 If C<$name> is not a valid git repository an exception will be thrown.
93
94 =head1 SEE ALSO
95
96 L<Gitalist::Git::CollectionOfRepositories::FromListOfDirectories>,
97 L<Gitalist::Git::CollectionOfRepositories::FromDirectory>,
98 L<Gitalist::Git::Repository>.
99
100 =head1 AUTHORS
101
102 See L<Gitalist> for authors.
103
104 =head1 LICENSE
105
106 See L<Gitalist> for the license.
107
108 =cut
109