Changed repos_dir to repo_dir in the COR model.
[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->repo_dir) {
39         return 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
40     }
41     else {
42         return "Don't know where to get repositores from. Try a --repo_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 # Simple directory of repositories (for list)
71 has repo_dir => (
72     is => 'ro',
73     isa => DirOrUndef,
74     coerce => 1,
75     builder => '_build_repo_dir',
76     lazy => 1,
77 );
78
79 # Directory containing list of one or more repositories
80 has repos => (
81     is => 'ro',
82     isa => ArrayRefOfDirs,
83     coerce => 1,
84 );
85
86 sub _build_repo_dir {
87     my $self = shift;
88     return $ENV{GITALIST_REPO_DIR};
89 }
90
91 sub build_per_context_instance {
92     my ($self, $ctx) = @_;
93
94     $self->class();
95
96     if ($self->repo_dir) { $self->repo_dir->resolve }
97
98     my %args = (
99         export_ok => $self->export_ok || '',
100         $self->_has_whitelist ? (whitelist => $self->whitelist) : (),
101         repos => $self->repos,
102         repo_dir => $self->repo_dir,
103         vhost => $ctx->request->uri->host,
104         %{ $self->args }
105     );
106
107     my $class = $self->class;
108
109     $ctx->log->debug("Building $class with " . join(", ", map { $_ . " => " . (defined($args{$_}) ? "'" . $args{$_}  . "'" : 'undef') } keys %args))
110         if $ctx->debug;
111     my $model = $class->new(%args);
112
113     $ctx->log->debug("Using class '$class' " . $model->debug_string) if $ctx->debug;
114
115     return $model;
116 }
117
118 __PACKAGE__->meta->make_immutable;
119
120 __END__
121
122 =encoding UTF-8
123
124 =head1 NAME
125
126 Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist
127
128 =head1 DESCRIPTION
129
130 This Model is a factory for an object implementing the L<Gitalist::Git::CollectionOfRepositories>
131 interface.
132
133 The simple options passed on the command line (like C<--repo_dir>), a class will by picked by default 
134 L<Gitalist::Git::CollectionOfRepositories::FromDirectory>.
135
136 This can be overridden from config by explicitly passing in a class name and args for that class
137 in config:
138
139     <Model::CollectionOfRepos>
140         class MyClassName
141         <args>
142             ...
143         </args>
144     </Model::CollectionOfRepos>
145
146 =head1 AUTHORS
147
148 See L<Gitalist> for authors.
149
150 =head1 LICENSE
151
152 See L<Gitalist> for the license.
153
154 =cut