Better document and clean up the factory for collections of repos
[catagits/Gitalist.git] / lib / Gitalist / Model / CollectionOfRepos.pm
CommitLineData
7bf1a6f5 1package Gitalist::Model::CollectionOfRepos;
21336a02 2
3use Moose;
bd7cb7a1 4use MooseX::Types::Moose qw/Maybe ArrayRef/;
bddfb71e 5use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
eef1fb14 6use MooseX::Types::LoadableClass qw/ LoadableClass /;
a18818fd 7use Moose::Util::TypeConstraints;
a7010acf 8use Moose::Autobox;
21336a02 9use namespace::autoclean;
10
bddfb71e 11extends 'Catalyst::Model';
21336a02 12
5e26dc93 13with 'Catalyst::Component::ApplicationAttribute';
bddfb71e 14with 'Catalyst::Component::InstancePerContext';
15
a18818fd 16my $repo_dir_t = subtype NonEmptySimpleStr,
17 where { -d $_ },
18 message { 'Cannot find repository dir: "' . $_ . '", please set up gitalist.conf, or set GITALIST_REPO_DIR environment or pass the --repo_dir parameter when starting the application' };
19
a7010acf 20my $arrayof_repos_dir_t = subtype ArrayRef[$repo_dir_t],
21 where { 1 },
22 message { 'Cannot find repository directories listed in config - these are invalid directories: ' . join(', ', $_->flatten) };
23
24coerce $arrayof_repos_dir_t,
25 from NonEmptySimpleStr,
26 via { [ $_ ] };
27
a18818fd 28has config_repo_dir => (
bddfb71e 29 isa => NonEmptySimpleStr,
30 is => 'ro',
a18818fd 31 init_arg => 'repo_dir',
32 predicate => 'has_config_repo_dir',
bddfb71e 33);
34
a18818fd 35has repo_dir => (
a7010acf 36 isa => $repo_dir_t,
a18818fd 37 is => 'ro',
38 lazy_build => 1
39);
40
bd7cb7a1 41has repos => (
a7010acf 42 isa => $arrayof_repos_dir_t,
bd7cb7a1 43 is => 'ro',
44 default => sub { [] },
45 traits => ['Array'],
46 handles => {
47 _repos_count => 'count',
48 },
a7010acf 49 coerce => 1,
bd7cb7a1 50);
51
e33993c9 52has class => (
eef1fb14 53 isa => LoadableClass,
e33993c9 54 is => 'ro',
eef1fb14 55 is => 'lazy',
56 builder => '_build_class',
e33993c9 57);
58
eef1fb14 59sub _build_class {
60 my($self) = @_;
61
62 if($self->whitelist && -f $self->whitelist) {
63 return 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
64 } elsif ($self->_repos_count && !$self->search_recursively) {
65 return 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
66 } elsif($self->search_recursively) {
67 return 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
68 }
69
70 return 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
71}
72
e33993c9 73has args => (
74 isa => 'HashRef',
75 is => 'ro',
76 default => sub { {} },
77);
1891c774 78
79has search_recursively => (
80 is => 'ro',
81 isa => 'Bool',
82 default => 0,
83);
84
1d727634 85has export_ok => (
86 is => 'ro',
87 isa => 'Str',
88);
89
b70462a4 90has whitelist => (
91 is => 'ro',
92 isa => 'Str',
93);
94
a18818fd 95sub _build_repo_dir {
96 my $self = shift;
5e26dc93 97 my $repo_dir = $self->_application->run_options->{repo_dir};
98
99 $repo_dir ?
100 $repo_dir
a18818fd 101 : $self->has_config_repo_dir
102 ? $self->config_repo_dir
103 : '';
104}
105
106after BUILD => sub {
107 my $self = shift;
bd7cb7a1 108 # Explode loudly at app startup time if there is no list of
82bc0f05 109 # repositories or repos dir, rather than on first hit
bd7cb7a1 110 $self->_repos_count || $self->repo_dir;
a18818fd 111};
112
e33993c9 113
114sub build_per_context_instance {
115 my ($self, $app) = @_;
116
117 my %args = (
118 export_ok => $self->export_ok || '',
eef1fb14 119 $self->_has_whitelist ? (whitelist => $self->whistlist) : (),
120 $self->_has_repos ? (repos => $self->repos) : ()
121 $self->_has_repo_dir ? (repo_dir => $self->repo_dir) : ()
e33993c9 122 %{ $self->args }
123 );
124
125 my $class = $self->class;
e33993c9 126
127 $app->log->debug("Using class '$class'");
128
1d727634 129 return $class->new(%args);
21336a02 130}
131
132__PACKAGE__->meta->make_immutable;
bddfb71e 133
775e96e0 134__END__
135
2298d93f 136=encoding UTF-8
137
138=head1 NAME
139
140Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist
141
eef1fb14 142=head1 DESCRIPTION
143
144This Model is a factory for an object implementing the L<Gitalist::Git::CollectionOfRepositories>
145interface.
146
147The simple options passed on the command line (like C<--repos_dir>), a class will by picked by default
148L<Gitalist::Git::CollectionOfRepositories::FromDirectory>.
149
150This can be overridden from config by explicitly passing in a class name and args for that class
151in config:
152
153 <Model::CollectionOfRepos>
154 class MyClassName
155 <args>
156 ...
157 </args>
158 </Model::CollectionOfRepos>
159
775e96e0 160=head1 AUTHORS
161
162See L<Gitalist> for authors.
163
164=head1 LICENSE
165
166See L<Gitalist> for the license.
167
168=cut