Rename get_project to get_repository
[catagits/Gitalist.git] / lib / Gitalist / Model / GitRepos.pm
CommitLineData
21336a02 1package Gitalist::Model::GitRepos;
2
3use Moose;
cd169152 4use Gitalist::Git::CollectionOfRepositories::FromDirectory;
5use Gitalist::Git::CollectionOfRepositories::FromListOfDirectories;
bd7cb7a1 6use MooseX::Types::Moose qw/Maybe ArrayRef/;
bddfb71e 7use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
a18818fd 8use Moose::Util::TypeConstraints;
a7010acf 9use Moose::Autobox;
21336a02 10use namespace::autoclean;
11
bddfb71e 12extends 'Catalyst::Model';
21336a02 13
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
a18818fd 52sub _build_repo_dir {
53 my $self = shift;
54 $ENV{GITALIST_REPO_DIR} ?
55 $ENV{GITALIST_REPO_DIR}
56 : $self->has_config_repo_dir
57 ? $self->config_repo_dir
58 : '';
59}
60
61after BUILD => sub {
62 my $self = shift;
bd7cb7a1 63 # Explode loudly at app startup time if there is no list of
64 # projects or repos dir, rather than on first hit
65 $self->_repos_count || $self->repo_dir;
a18818fd 66};
67
bddfb71e 68sub build_per_context_instance {
69 my ($self, $app) = @_;
bd7cb7a1 70 if ($self->_repos_count) {
cd169152 71 Gitalist::Git::CollectionOfRepositories::FromListOfDirectories->new(repos => $self->repos);
bd7cb7a1 72 }
73 else {
cd169152 74 Gitalist::Git::CollectionOfRepositories::FromDirectory->new(repo_dir => $self->repo_dir);
bd7cb7a1 75 }
21336a02 76}
77
78__PACKAGE__->meta->make_immutable;
bddfb71e 79
775e96e0 80__END__
81
82=head1 AUTHORS
83
84See L<Gitalist> for authors.
85
86=head1 LICENSE
87
88See L<Gitalist> for the license.
89
90=cut