Allow the model to be user defined.
[catagits/Gitalist.git] / lib / Gitalist / Model / CollectionOfRepos.pm
CommitLineData
7bf1a6f5 1package Gitalist::Model::CollectionOfRepos;
21336a02 2
3use Moose;
afbb1a52 4use Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive;
cd169152 5use Gitalist::Git::CollectionOfRepositories::FromListOfDirectories;
b70462a4 6use Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList;
bd7cb7a1 7use MooseX::Types::Moose qw/Maybe ArrayRef/;
bddfb71e 8use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
a18818fd 9use Moose::Util::TypeConstraints;
a7010acf 10use Moose::Autobox;
21336a02 11use namespace::autoclean;
12
bddfb71e 13extends 'Catalyst::Model';
21336a02 14
bddfb71e 15with 'Catalyst::Component::InstancePerContext';
16
a18818fd 17my $repo_dir_t = subtype NonEmptySimpleStr,
18 where { -d $_ },
19 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' };
20
a7010acf 21my $arrayof_repos_dir_t = subtype ArrayRef[$repo_dir_t],
22 where { 1 },
23 message { 'Cannot find repository directories listed in config - these are invalid directories: ' . join(', ', $_->flatten) };
24
25coerce $arrayof_repos_dir_t,
26 from NonEmptySimpleStr,
27 via { [ $_ ] };
28
a18818fd 29has config_repo_dir => (
bddfb71e 30 isa => NonEmptySimpleStr,
31 is => 'ro',
a18818fd 32 init_arg => 'repo_dir',
33 predicate => 'has_config_repo_dir',
bddfb71e 34);
35
a18818fd 36has repo_dir => (
a7010acf 37 isa => $repo_dir_t,
a18818fd 38 is => 'ro',
39 lazy_build => 1
40);
41
bd7cb7a1 42has repos => (
a7010acf 43 isa => $arrayof_repos_dir_t,
bd7cb7a1 44 is => 'ro',
45 default => sub { [] },
46 traits => ['Array'],
47 handles => {
48 _repos_count => 'count',
49 },
a7010acf 50 coerce => 1,
bd7cb7a1 51);
52
e33993c9 53has class => (
54 isa => NonEmptySimpleStr,
55 is => 'ro',
56);
57
58has args => (
59 isa => 'HashRef',
60 is => 'ro',
61 default => sub { {} },
62);
1891c774 63
64has search_recursively => (
65 is => 'ro',
66 isa => 'Bool',
67 default => 0,
68);
69
1d727634 70has export_ok => (
71 is => 'ro',
72 isa => 'Str',
73);
74
b70462a4 75has whitelist => (
76 is => 'ro',
77 isa => 'Str',
78);
79
a18818fd 80sub _build_repo_dir {
81 my $self = shift;
82 $ENV{GITALIST_REPO_DIR} ?
83 $ENV{GITALIST_REPO_DIR}
84 : $self->has_config_repo_dir
85 ? $self->config_repo_dir
86 : '';
87}
88
89after BUILD => sub {
90 my $self = shift;
bd7cb7a1 91 # Explode loudly at app startup time if there is no list of
82bc0f05 92 # repositories or repos dir, rather than on first hit
bd7cb7a1 93 $self->_repos_count || $self->repo_dir;
a18818fd 94};
95
e33993c9 96sub _default_model_class {
97 my($self) = @_;
1d727634 98
b70462a4 99 if($self->whitelist && -f $self->whitelist) {
e33993c9 100 return 'FromDirectory::WhiteList';
1891c774 101 } elsif ($self->_repos_count && !$self->search_recursively) {
e33993c9 102 return 'FromListOfDirectories';
07ee9dc1 103 } elsif($self->search_recursively) {
e33993c9 104 return 'FromDirectoryRecursive';
bd7cb7a1 105 }
1d727634 106
e33993c9 107 return 'FromDirectory';
108}
109
110sub build_per_context_instance {
111 my ($self, $app) = @_;
112
113 my %args = (
114 export_ok => $self->export_ok || '',
115 %{ $self->args }
116 );
117
118 my $class = $self->class;
119 Class::MOP::load_class($class) if $class;
120
121 my $default = $self->_default_model_class;
122
123 $args{whitelist} = $self->whitelist if $default eq 'FromDirectory::WhiteList';
124 $args{repos} = $self->repos if $default eq 'FromListOfDirectories';
125 $args{repo_dir} = $self->repo_dir if $default =~ /\b(?:WhiteList|FromDirectory(?:Recursive)?)$/;
126
127 $class ||= "Gitalist::Git::CollectionOfRepositories::$default";
128
129 $app->log->debug("Using class '$class'");
130
1d727634 131 return $class->new(%args);
21336a02 132}
133
134__PACKAGE__->meta->make_immutable;
bddfb71e 135
775e96e0 136__END__
137
2298d93f 138=encoding UTF-8
139
140=head1 NAME
141
142Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist
143
775e96e0 144=head1 AUTHORS
145
146See L<Gitalist> for authors.
147
148=head1 LICENSE
149
150See L<Gitalist> for the license.
151
152=cut