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