Align Vhost code with reality.
[catagits/Gitalist.git] / lib / Gitalist / Model / CollectionOfRepos.pm
CommitLineData
7bf1a6f5 1package Gitalist::Model::CollectionOfRepos;
21336a02 2
3use Moose;
ea772511 4use MooseX::Types::Moose qw/Undef Maybe ArrayRef Str/;
bddfb71e 5use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
eef1fb14 6use MooseX::Types::LoadableClass qw/ LoadableClass /;
ea772511 7use Gitalist::Git::Types qw/ ArrayRefOfDirs Dir DirOrUndef /;
a18818fd 8use Moose::Util::TypeConstraints;
a7010acf 9use Moose::Autobox;
ea772511 10use Path::Class qw/ dir /;
21336a02 11use namespace::autoclean;
4d334b53 12use Carp qw/croak/;
21336a02 13
bddfb71e 14extends 'Catalyst::Model';
21336a02 15
e33993c9 16has class => (
eef1fb14 17 isa => LoadableClass,
e33993c9 18 is => 'ro',
ea772511 19 lazy => 1,
20 coerce => 1,
eef1fb14 21 builder => '_build_class',
e33993c9 22);
23
eef1fb14 24sub _build_class {
ea772511 25 my ($self) = @_;
eef1fb14 26
27 if($self->whitelist && -f $self->whitelist) {
28 return 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
ea772511 29 }
30 elsif($self->search_recursively) {
eef1fb14 31 return 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
32 }
ea772511 33 elsif ($self->repos) {
34 return 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
35 }
f4184925 36 elsif ($self->repo_dir) {
ea772511 37 return 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
38 }
39 else {
4d334b53 40 croak "Don't know where to get repositores from. Try a --repo_dir option, or setting up config";
ea772511 41 }
eef1fb14 42}
43
e33993c9 44has args => (
45 isa => 'HashRef',
46 is => 'ro',
47 default => sub { {} },
48);
1891c774 49
50has search_recursively => (
51 is => 'ro',
52 isa => 'Bool',
53 default => 0,
54);
55
ea772511 56## XX What is this for?
1d727634 57has export_ok => (
58 is => 'ro',
59 isa => 'Str',
60);
61
b70462a4 62has whitelist => (
63 is => 'ro',
64 isa => 'Str',
ea772511 65 predicate => '_has_whitelist',
b70462a4 66);
67
ea772511 68# Simple directory of repositories (for list)
f4184925 69has repo_dir => (
ea772511 70 is => 'ro',
71 isa => DirOrUndef,
72 coerce => 1,
f4184925 73 builder => '_build_repo_dir',
ea772511 74 lazy => 1,
75);
5e26dc93 76
ea772511 77# Directory containing list of one or more repositories
78has repos => (
79 is => 'ro',
80 isa => ArrayRefOfDirs,
81 coerce => 1,
82);
a18818fd 83
f4184925 84sub _build_repo_dir {
a18818fd 85 my $self = shift;
6537d25e 86 return $ENV{GITALIST_REPO_DIR};
ea772511 87}
a18818fd 88
4d334b53 89sub BUILD {
90 my($self) = @_;
e33993c9 91
ca5cfe83 92 $self->class();
93
f4184925 94 if ($self->repo_dir) { $self->repo_dir->resolve }
4d334b53 95}
96
97sub COMPONENT {
98 my($class, $ctx, @args) = @_;
99
100 my $self = $class->new($ctx, @args);
ca5cfe83 101
e33993c9 102 my %args = (
103 export_ok => $self->export_ok || '',
4d334b53 104 repos => $self->repos,
105 repo_dir => $self->repo_dir,
ea772511 106 $self->_has_whitelist ? (whitelist => $self->whitelist) : (),
e33993c9 107 %{ $self->args }
108 );
109
4d334b53 110 my $model_class = $self->class;
2a285433 111
4d334b53 112 $ctx->log->debug("Building $model_class with " . join(", ", map { $_ . " => " . (defined($args{$_}) ? "'" . $args{$_} . "'" : 'undef') } keys %args))
6537d25e 113 if $ctx->debug;
2a285433 114
4d334b53 115 my $model = $model_class->new(\%args);
116
117 $ctx->log->debug("Using class '$model_class' " . $model->debug_string) if $ctx->debug;
13c42902 118
119 return $model;
21336a02 120}
121
122__PACKAGE__->meta->make_immutable;
bddfb71e 123
775e96e0 124__END__
125
2298d93f 126=encoding UTF-8
127
128=head1 NAME
129
130Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist
131
eef1fb14 132=head1 DESCRIPTION
133
134This Model is a factory for an object implementing the L<Gitalist::Git::CollectionOfRepositories>
135interface.
136
f4184925 137The simple options passed on the command line (like C<--repo_dir>), a class will by picked by default
eef1fb14 138L<Gitalist::Git::CollectionOfRepositories::FromDirectory>.
139
140This can be overridden from config by explicitly passing in a class name and args for that class
141in config:
142
143 <Model::CollectionOfRepos>
144 class MyClassName
145 <args>
146 ...
147 </args>
148 </Model::CollectionOfRepos>
149
775e96e0 150=head1 AUTHORS
151
152See L<Gitalist> for authors.
153
154=head1 LICENSE
155
156See L<Gitalist> for the license.
157
158=cut