Don't blow up so much, but still not working
[catagits/Gitalist.git] / lib / Gitalist / Model / CollectionOfRepos.pm
1 package Gitalist::Model::CollectionOfRepos;
2
3 use Moose;
4 use MooseX::Types::Moose qw/Undef Maybe ArrayRef Str/;
5 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
6 use MooseX::Types::LoadableClass qw/ LoadableClass /;
7 use Gitalist::Git::Types qw/ ArrayRefOfDirs Dir DirOrUndef /;
8 use Moose::Util::TypeConstraints;
9 use Moose::Autobox;
10 use Path::Class qw/ dir /;
11 use namespace::autoclean;
12
13 extends 'Catalyst::Model';
14
15 with 'Catalyst::Component::ApplicationAttribute';
16 with 'Catalyst::Component::InstancePerContext';
17
18 has class => (
19     isa => LoadableClass,
20     is  => 'ro',
21     lazy => 1,
22     coerce => 1,
23     builder => '_build_class',
24 );
25
26 sub _build_class {
27     my ($self) = @_;
28
29     if($self->whitelist && -f $self->whitelist) {
30         return 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
31     }
32     elsif($self->search_recursively) {
33         return 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
34     }
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     }
44 }
45
46 has args => (
47     isa     => 'HashRef',
48     is      => 'ro',
49     default => sub { {} },
50 );
51
52 has search_recursively => (
53     is      => 'ro',
54     isa     => 'Bool',
55     default => 0,
56 );
57
58 ## XX What is this for?
59 has export_ok => (
60     is  => 'ro',
61     isa => 'Str',
62 );
63
64 has whitelist => (
65     is  => 'ro',
66     isa => 'Str',
67     predicate => '_has_whitelist',
68 );
69
70 has repo_dir => (
71     is         => 'ro',
72     isa        => DirOrUndef,
73     coerce     => 1,
74     predicate => '_has_repo_dir',
75 );
76
77 # Simple directory of repositories (for list)
78 has repos_dir => (
79     is => 'ro',
80     isa => DirOrUndef,
81     coerce => 1,
82     builder => '_build_repos_dir',
83     lazy => 1,
84 );
85
86 # Directory containing list of one or more repositories
87 has repos => (
88     is => 'ro',
89     isa => ArrayRefOfDirs,
90     coerce => 1,
91 );
92
93 sub _build_repos_dir {
94     my $self = shift;
95     my $opts = $self->_application->run_options || {};
96     return $self->_has_repo_dir && $self->repo_dir
97         || $opts->{repos_dir} || $ENV{GITALIST_REPO_DIR} || undef;
98 }
99
100 sub build_per_context_instance {
101     my ($self, $ctx) = @_;
102
103     $self->class();
104
105     if ($self->repos_dir) { $self->repos_dir->resolve }
106
107     my %args = (
108         export_ok => $self->export_ok || '',
109         $self->_has_whitelist ? (whitelist => $self->whitelist) : (),
110         repos => $self->repos,
111         repo_dir => $self->repos_dir,
112         vhost => $ctx->request->uri->host,
113         %{ $self->args }
114     );
115
116     my $class = $self->class;
117
118     $ctx->log->debug("Using class '$class'") if $ctx->debug;
119
120     return $class->new(%args);
121 }
122
123 __PACKAGE__->meta->make_immutable;
124
125 __END__
126
127 =encoding UTF-8
128
129 =head1 NAME
130
131 Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist
132
133 =head1 DESCRIPTION
134
135 This Model is a factory for an object implementing the L<Gitalist::Git::CollectionOfRepositories>
136 interface.
137
138 The simple options passed on the command line (like C<--repos_dir>), a class will by picked by default 
139 L<Gitalist::Git::CollectionOfRepositories::FromDirectory>.
140
141 This can be overridden from config by explicitly passing in a class name and args for that class
142 in config:
143
144     <Model::CollectionOfRepos>
145         class MyClassName
146         <args>
147             ...
148         </args>
149     </Model::CollectionOfRepos>
150
151 =head1 AUTHORS
152
153 See L<Gitalist> for authors.
154
155 =head1 LICENSE
156
157 See L<Gitalist> for the license.
158
159 =cut