File Coverage

blib/lib/Gitosis/Config.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Gitosis::Config;
2             our $VERSION = '0.06';
3 4     4   96206 use Moose;
  4         2223303  
  4         33  
4 4     4   31784 use MooseX::Types::Path::Class qw(File);
  4         723999  
  4         35  
5 4     4   11599 use MooseX::AttributeHelpers;
  0            
  0            
6              
7             use Gitosis::Config::Reader;
8             use Gitosis::Config::Writer;
9             use Gitosis::Config::Group;
10              
11             has file => (
12             isa => File,
13             coerce => 1,
14             is => 'rw',
15             );
16              
17             has [qw(gitweb daemon loglevel repositories)] => (
18             isa => 'Maybe[Str]',
19             is => 'rw',
20             );
21              
22             has groups => (
23             metaclass => 'Collection::Array',
24             isa => 'ArrayRef[Gitosis::Config::Group]',
25             is => 'ro',
26             auto_deref => 1,
27             lazy_build => 1,
28             provides => {
29             push => 'add_group',
30             find => 'find_group',
31             },
32             );
33              
34             sub find_group_by_name {
35             my ( $self, $arg ) = @_;
36             $self->find_group( sub { $_[0]->name eq $arg } );
37             }
38              
39             sub _build_groups { [] }
40              
41             around 'add_group' => sub {
42             my ( $next, $self, $group ) = @_;
43             $group = Gitosis::Config::Group->new($group) unless ( blessed $group);
44             $self->$next($group);
45             };
46              
47             has repos => (
48             metaclass => 'Collection::Array',
49             isa => 'ArrayRef[HashRef]',
50             is => 'ro',
51             auto_deref => 1,
52             lazy_build => 1,
53             provides => { push => 'add_repo', }
54             );
55              
56             sub _build_repos { [] }
57              
58             #
59             # METHODS
60             #
61              
62             sub to_string {
63             Gitosis::Config::Writer->write_string( $_[0] );
64             }
65              
66             sub save {
67             my ($self) = @_;
68             die 'Must have a filename, please set file()' unless $self->file;
69             $self->file->openw->print( $self->to_string ) or die "$!";
70              
71             }
72              
73             #
74             # Constructor Hack
75             #
76              
77             sub BUILD {
78             my ( $self, $args ) = @_;
79              
80             if ( exists $args->{config} ) {
81             return $self->_build_from_config( $args->{config} );
82             }
83              
84             if ( exists $args->{file} ) {
85             $self->file( $args->{file} );
86             my $cfg = Gitosis::Config::Reader->read_file( $args->{file} );
87             return $self->_build_from_config($cfg);
88             }
89             }
90              
91             sub _build_from_config {
92             my ( $self, $cfg ) = @_;
93             for my $attr (qw(gitweb daemon loglevel repositories)) {
94             $self->$attr( $cfg->{gitosis}{$attr} );
95             }
96             for my $name ( grep { $_ =~ /^group/ } keys %$cfg ) {
97             my $group = $cfg->{$name};
98             ( $group->{name} = $name ) =~ s/^group\s+//;
99             $self->add_group($group);
100             }
101              
102             for my $name ( grep { $_ =~ /^repo/ } keys %$cfg ) {
103             my $repo = $cfg->{$name};
104             ( $repo->{name} = $name ) =~ s/^repo\s+//;
105             $self->add_repo($repo);
106             }
107             }
108              
109             no Moose;
110             1;
111             __END__
112              
113              
114             =head1 NAME
115              
116             Gitosis::Config - Parse and Write gitosis config files
117              
118              
119             =head1 VERSION
120              
121             This document describes Gitosis::Config version 0.05
122              
123              
124             =head1 SYNOPSIS
125              
126             use Gitosis::Config;
127             my $conf = Gitosis::Config->new( file => 'gitosis.conf' );
128              
129             =head1 DESCRIPTION
130              
131             Gitosis::Config is an object oriented wrapper around the gitosis config
132             file format. It allows you to programmatically create and modify gitosis
133             config files.
134              
135             =head1 METHODS
136              
137             =head2 new
138              
139             =head2 loglevel, gitweb, daemon, repositories
140              
141             =head2 groups
142              
143             =head2 add_group
144              
145             =head2 repos
146              
147             =head2 add_repo
148              
149             =head2 to_string
150              
151             =head2 save
152              
153             =head1 CONFIGURATION AND ENVIRONMENT
154            
155             Gitosis::Config requires no configuration files or environment variables.
156              
157             =head1 DEPENDENCIES
158              
159             L<Config::INI>, L<Moose>, L<MooseX::AttributeHelpers>,
160             L<MooseX::Types::Path::Class>
161              
162             =head1 INCOMPATIBILITIES
163              
164             None reported.
165              
166             =head1 BUGS AND LIMITATIONS
167              
168             Please report any bugs or feature requests to
169             C<bug-gitosis-config@rt.cpan.org>, or through the web interface at
170             L<http://rt.cpan.org>.
171              
172             =head1 AUTHOR
173              
174             Chris Prather C<< <chris@prather.org> >>
175              
176              
177             =head1 LICENCE AND COPYRIGHT
178              
179             Copyright (c) 2008, Chris Prather C<< <chris@prather.org> >>. Some rights
180             reserved.
181              
182             This module is free software; you can redistribute it and/or
183             modify it under the same terms as Perl itself. See L<perlartistic>.
184              
185              
186             =head1 DISCLAIMER OF WARRANTY
187              
188             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
189             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
190             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
191             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
192             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
193             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
194             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
195             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
196             NECESSARY SERVICING, REPAIR, OR CORRECTION.
197              
198             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
199             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
200             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
201             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
202             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
203             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
204             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
205             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
206             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
207             SUCH DAMAGES.