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