8daf00cb9d308a69c58c9598d6590a89f0a38f29
[catagits/Gitalist.git] / lib / Gitalist / Git / CollectionOfRepositories / Vhost.pm
1 use MooseX::Declare;
2
3 class Gitalist::Git::CollectionOfRepositories::Vhost
4     with Gitalist::Git::CollectionOfRepositories {
5     use MooseX::Types::Moose qw/ HashRef Str /;
6     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7     use MooseX::Types::Path::Class qw/Dir/;
8     use Moose::Util::TypeConstraints;
9
10     sub BUILDARGS { # FIXME - This is fuck ugly!
11         my ($class, @args) = @_;
12         my $args = $class->next::method(@args);
13         my %collections = %{ delete $args->{collections} };
14         foreach my $name (keys %collections) {
15             my %args = %{$collections{$name}};
16             my $class = delete $args{class};
17             Class::MOP::load_class($class);
18             $collections{$name} = $class->new(%args);
19         }
20         my $ret = { %$args, collections => \%collections };
21         return $ret;
22     }
23
24     has vhost_dispatch => (
25         isa => HashRef,
26         traits => ['Hash'],
27         required => 1,
28         handles => {
29             _get_collection_name_for_vhost => 'get',
30         },
31     );
32
33     has collections => (
34         isa => HashRef,
35         traits => ['Hash'],
36         required => 1,
37         handles => {
38             _get_collection => 'get',
39         }
40     );
41
42     has vhost => (
43         is => 'ro',
44         isa => Str,
45         required => 1,
46     );
47
48     role_type 'Gitalist::Git::CollectionOfRepositories';
49     has chosen_collection => (
50         does => 'Gitalist::Git::CollectionOfRepositories',
51         handles => [qw/
52             _get_repo_from_name
53             _build_repositories
54         /],
55         default => sub {
56             my $self = shift;
57             $self->_get_collection($self->_get_collection_name_for_vhost($self->vhost) || $self->_get_collection_name_for_vhost('_default_'));
58         },
59         lazy => 1,
60     );
61 }                               # end class
62
63 __END__
64
65 =head1 NAME
66
67 Gitalist::Git::CollectionOfRepositories::Vhost
68
69 =head1 SYNOPSIS
70
71     my $repo = Gitalist::Git::CollectionOfRepositories::Vhost->new(
72         vhost_dispatch => {
73             "git.shadowcat.co.uk" => "foo",
74             "git.moose.perl.org" => "bar",
75             "_default_" => "foo",
76         },
77         collections => {
78             foo => { class => Gitalist::Git::CollectionOfRepositories::XXX', %args },
79             bar => { class => Gitalist::Git::CollectionOfRepositories::XXX', %args },
80         }
81     );
82     my $repository_list = $repo->repositories;
83     my $first_repository = $repository_list->[0];
84     my $named_repository = $repo->get_repository('Gitalist');
85
86 =head1 DESCRIPTION
87
88 =head1 SEE ALSO
89
90 L<Gitalist::Git::CollectionOfRepositories>, L<Gitalist::Git::Repository>
91
92 =head1 AUTHORS
93
94 See L<Gitalist> for authors.
95
96 =head1 LICENSE
97
98 See L<Gitalist> for the license.
99
100 =cut