dd12c12e1372038523c4f261b14a7fe04e7cb696
[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     method debug_string { 'chosen collection ' . ref($self->chosen_collection) . " " . $self->chosen_collection->debug_string }
49
50     role_type 'Gitalist::Git::CollectionOfRepositories';
51     has chosen_collection => (
52         does => 'Gitalist::Git::CollectionOfRepositories',
53         handles => [qw/
54             _get_repo_from_name
55             _build_repositories
56         /],
57         default => sub {
58             my $self = shift;
59             $self->_get_collection($self->_get_collection_name_for_vhost($self->vhost) || $self->_get_collection_name_for_vhost('_default_'));
60         },
61         lazy => 1,
62     );
63 }                               # end class
64
65 __END__
66
67 =head1 NAME
68
69 Gitalist::Git::CollectionOfRepositories::Vhost
70
71 =head1 SYNOPSIS
72
73     my $repo = Gitalist::Git::CollectionOfRepositories::Vhost->new(
74         vhost_dispatch => {
75             "git.shadowcat.co.uk" => "foo",
76             "git.moose.perl.org" => "bar",
77             "_default_" => "foo",
78         },
79         collections => {
80             foo => { class => Gitalist::Git::CollectionOfRepositories::XXX', %args },
81             bar => { class => Gitalist::Git::CollectionOfRepositories::XXX', %args },
82         }
83     );
84     my $repository_list = $repo->repositories;
85     my $first_repository = $repository_list->[0];
86     my $named_repository = $repo->get_repository('Gitalist');
87
88 =head1 DESCRIPTION
89
90 =head1 SEE ALSO
91
92 L<Gitalist::Git::CollectionOfRepositories>, L<Gitalist::Git::Repository>
93
94 =head1 AUTHORS
95
96 See L<Gitalist> for authors.
97
98 =head1 LICENSE
99
100 See L<Gitalist> for the license.
101
102 =cut