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