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