Giddy/Database.pm

filter: Statementstmt 43.2% 32 / 74 Branchbran 4.5% 1 / 22 Conditioncond 2.9% 1 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 57.1% 8 / 14 Podpod 100% 8 / 8 totaltotal 29.2% 50 / 171 CC 26 SCAR 33.8
CC
26
Cov
26%
get_collection
56.0
find
56.0
undo
30.0
CRAP 29.3
« Giddy/Collection.pm ↑ Summary Giddy/Role/DocumentLoader.pm »
1 package Giddy::Database;
2  
3 # ABSTRACT: A Giddy database.
4  
5 2 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Git::Repository::Log::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Giddy::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = "0.013_001";
13 2 $VERSION = eval $VERSION;
14  
15 =head1 NAME
16  
17 Giddy::Database - A Giddy database.
18  
19 =head1 VERSION
20  
21 version 0.013_001
22  
23 =head1 SYNOPSIS
24  
25 my $db = $giddy->get_database('/path/to/database');
26  
27 =head1 DESCRIPTION
28  
29 This class represents Giddy databases. Aside from providing you with the ability
30 to create and get collections from the database, it provides methods which are
31 global to the database, like commit changes, undoing changes, etc.
32  
33 =head1 CONSUMES
34  
35 =over
36  
37 =item * L<Giddy::Role::PathAnalyzer>
38  
39 =item * L<Giddy::Role::PathMaker>
40  
41 =back
42  
43 =head1 ATTRIBUTES
44  
45 =head2 _repo
46  
47 A L<Git::Repository> object, tied to the git repository of the Giddy database.
48 This is a required attribute. Not meant to be used externally, but knock yourself
49 out if you feel the need to run specific Git commands.
50  
51 =head2 _staged
52  
53 A list of paths staged (added) to the next commit job. Automatically created.
54  
55 =cut
56  
57 2 has '_repo' => (is => 'ro', isa => 'Git::Repository', required => 1);
58  
59 1 ▶has '_staged' => (is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, writer => '_set_staged');
Subroutine __ANON__: called CC=1 SCAR=0.0
60  
61 2 with 'Giddy::Role::PathAnalyzer',
62 'Giddy::Role::PathMaker';
63  
64 =head1 OBJECT METHODS
65  
66 =head2 get_collection( [ $path_to_coll ] )
67  
68 Returns a L<Giddy::Collection> object tied to a certain directory in the database.
69 If a path is not provided, the root collection ('') will be used. If the collection
70 does not exist, Giddy will attempt to create it. The path provided has to be relative
71 to the database's full path, with no starting slash.
72  
73 =cut
74  
75 0 ▶sub get_collection {
Subroutine get_collection: not called CC=7 SCAR=40.3
Pod documented
76 0 my ($self, $path) = @_;
77  
78 0 ▶ $path ||= '';
Condition0%0/2
$path ||= ""
l!l
00
MC/DC0%0/1
$path ||= ""
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
79  
80 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
81 0 ▶ croak "Path of collection to get must not start with a slash."
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 0 0 0
82 if $path =~ m!^/!;
83  
84 0 ▶ croak "The collection path exists in the database but is not a collection."
Branch: 1 branch
BranchTrueFalseTotal
if $self->_path_exists($path) and $self->_is_document_dir($path) || $self->_is_static_dir($path) 0 0 0
Condition0%0/6
$self->_path_exists($path) and $self->_is_document_dir($path) || $self->_is_static_dir($path)
!ll&&!rl&&r
000
$self->_is_document_dir($path) or $self->_is_static_dir($path)
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->_path_exists($path) and $self->_is_document_dir($path) || $self->_is_static_dir($path)
$self->_path_exists($path) $self->_is_document_dir($path) $self->_is_static_dir($path)
Truth table0%0/4
A and B or C
A= $self->_path_exists($path)B= $self->_is_document_dir($path)C= $self->_is_static_dir($path)
ABCresult
0XX0
1000
11X1
1011
85 if $self->_path_exists($path) && ($self->_is_document_dir($path) || $self->_is_static_dir($path));
86  
87 # create the collection directory (unless it already exists)
88 0 $self->_create_dir($path);
89 }
90  
91 0 return Giddy::Collection->new(db => $self, path => $path);
92 }
93  
94 =head2 commit( [$commit_msg] )
95  
96 Commits all pending changes with a commit message. If not provided, Giddy will
97 use a default commit message listing the number of changes performed.
98  
99 =cut
100  
101 1 ▶sub commit {
Subroutine commit: called CC=2 SCAR=9.2
Pod documented
102 1 my ($self, $msg) = @_;
103  
104 0 ▶ $msg ||= "Commited ".scalar(@{$self->_staged})." changes";
Condition50%1/2
$msg ||= 'Commited ' . scalar(@{$self->_staged;}) . ' changes'
l!l
10
MC/DC0%0/1
$msg ||= 'Commited ' . scalar(@{$self->_staged;}) . ' changes'
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
105  
106 # commit
107 1 $self->_repo->run('commit', '-m', $msg);
108  
109 # clear marked list
110 0 $self->_set_staged([]);
111  
112 0 return 1;
113 }
114  
115 =head2 stage( @paths )
116  
117 Marks files/directories as to be staged. Mostly called automatically by
118 C<new_collection()>, C<new_document()>, etc., but you can use it if you need to.
119 Paths are relative to database's path and should not contain starting slashes.
120  
121 =cut
122  
123 1 ▶sub stage {
Subroutine stage: called CC=3 SCAR=11.0
Pod documented
124 1 my ($self, @paths) = @_;
125  
126 1 ▶ croak "You must provide the relative path of the file/directory to stage."
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @paths 0 1 1
127 unless scalar @paths;
128  
129 1 foreach (@paths) {
130 # stage the file
131 1 $self->_repo->run('add', $_);
132 }
133  
134 # store files in object
135 1 my $staged = $self->_staged;
136 1 push(@$staged, @paths);
137 1 $self->_set_staged($staged);
138 }
139  
140 =head2 find( [ $path, [\%options] ] )
141  
142 Searches the Giddy repository for documents that match the provided
143 path. The path has to be relative to the repository's root directory, which
144 is considered the empty string. This string will be used if C<$path> is not provided. This is
145 a convenience method for finding documents by path. See L<Giddy::Collection> and
146 L<Giddy::Manual/"FULL PATH FINDING"> for more information.
147  
148 =cut
149  
150 0 ▶sub find {
Subroutine find: not called CC=7 SCAR=40.3
Pod documented
151 0 my ($self, $path, $opts) = @_;
152  
153 0 ▶ croak "find() expected a hash-ref of options, but received ".ref($opts)
Branch: 1 branch
BranchTrueFalseTotal
if $opts and !ref($opts) || ref $opts ne "HASH" 0 0 0
Condition0%0/6
$opts and !ref($opts) || ref $opts ne "HASH"
!ll&&!rl&&r
000
not ref $opts or ref $opts ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts and !ref($opts) || ref $opts ne "HASH"
$opts not ref $opts ref $opts ne "HASH"
Truth table0%0/4
A and not(B) or C
A= $optsB= not ref $optsC= ref $opts ne "HASH"
ABCresult
0XX0
1000
11X1
1011
154 if $opts && (!ref $opts || ref $opts ne 'HASH');
155  
156 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
157  
158 # in which directory are we searching?
159 0 my ($dir, $name) = ('', '');
160 0 ▶ if ($path && $path =~ m!/([^/]+)$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and $path =~ m[/([^/]+)$]) { } 0 0 0
Condition0%0/3
$path and $path =~ m[/([^/]+)$]
!ll&&!rl&&r
000
MC/DC0%0/2
$path and $path =~ m[/([^/]+)$]
$path $path =~ m[/([^/]+)$]
Truth table0%0/3
A and B
A= $pathB= $path =~ m[/([^/]+)$]
ABresult
0X0
100
111
161 0 $name = $1;
162 0 $dir = $`;
163 } else {
164 0 $name = $path;
165 }
166  
167 0 return $self->get_collection($dir)->find($name, $opts);
168 }
169  
170 =head2 find_one( [ $path, \%options ] )
171  
172 Same as calling C<< find($path, $options)->first() >>.
173  
174 =cut
175  
176 0 ▶sub find_one {
Subroutine find_one: not called CC=1 SCAR=6.9
Pod documented
177 0 shift->find(@_)->first;
178 }
179  
180 =head2 undo( [ $num ] )
181  
182 =head2 undo( $commit_checksum )
183  
184 Cancels the C<$num>th latest commit performed (if $num is 0 or not passed, the
185 latest commit is cancelled). Any changes performed by the commit cancelled
186 are forever lost. If a commit numbered C<$num> isn't found, this method will croak.
187 You can also provide a specific commit SHA-1 checksum to cancel.
188  
189 For an alternative that doesn't lose information, see C<revert()>.
190  
191 =cut
192  
193 0 ▶sub undo {
Subroutine undo: not called CC=5 SCAR=34.0
Pod documented
194 0 my ($self, $num) = @_;
195  
196 0 my $commit;
197 0 ▶ if (defined $num && $num !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num and not $num =~ /^\d+$/) { } 0 0 0
Condition0%0/3
defined $num and not $num =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $num and not $num =~ /^\d+$/
defined $num not $num =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= defined $numB= not $num =~ /^\d+$/
ABresult
0X0
100
111
198 # seems we've been provided with a commit hash, not number
199 0 $commit = $num;
200 } else {
201 0 ▶ $num ||= 0;
Condition0%0/2
$num ||= 0
l!l
00
MC/DC0%0/1
$num ||= 0
$num
Truth table0%0/2
A
A= $num
Aresult
11
00
202 0 my $log = $self->log($num+1);
203 0 ▶ croak "Can't find commit number $num." unless $log;
Branch: 1 branch
BranchTrueFalseTotal
unless $log 0 0 0
204 0 $commit = $log->commit;
205 }
206 0 $self->_repo->run('reset', '--hard', $commit);
207 }
208  
209 =head2 revert( [ $num ] )
210  
211 =head2 revert( $commit_checksum )
212  
213 Reverts the database back to the commit just before the commit performed
214 C<$num>th commits ago and creates a new commit out of it. In other words,
215 a snapshot of the database from the source commit (performed C<$num + 1>
216 commits ago) will be taken and used to replace the database's current
217 state. Then, a new commit is performed. Thus, the changes performed
218 between the source commit and the current commit are preserved in the log.
219 This is different than C<undo()>, which completely removes all commits
220 performed between the source commit and the current commit. Actually, a
221 revert can be cancelled by an C<undo()> operation, as it is a commit
222 in itself.
223  
224 C<$num> will be zero by default, in which case the latest commit is
225 reverted.
226  
227 If a commit numbered C<$num> isn't found, this method will croak.
228  
229 You can also provide a specific commit SHA-1 checksum.
230  
231 =cut
232  
233 0 ▶sub revert {
Subroutine revert: not called CC=5 SCAR=34.0
Pod documented
234 0 my ($self, $num) = @_;
235  
236 0 my $commit;
237 0 ▶ if (defined $num && $num !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num and not $num =~ /^\d+$/) { } 0 0 0
Condition0%0/3
defined $num and not $num =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $num and not $num =~ /^\d+$/
defined $num not $num =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= defined $numB= not $num =~ /^\d+$/
ABresult
0X0
100
111
238 # seems we've been provided with a commit hash, not number
239 0 $commit = $num;
240 } else {
241 0 ▶ $num ||= 0;
Condition0%0/2
$num ||= 0
l!l
00
MC/DC0%0/1
$num ||= 0
$num
Truth table0%0/2
A
A= $num
Aresult
11
00
242 0 my $log = $self->log($num);
243 0 ▶ croak "Can't find commit number $num." unless $log;
Branch: 1 branch
BranchTrueFalseTotal
unless $log 0 0 0
244 0 $commit = $log->commit;
245 }
246 0 $self->_repo->run('revert', $commit);
247 }
248  
249 =head2 log( [ $num ] )
250  
251 If C<$num> is provided (can be zero), will return a L<Git::Repository::Log>
252 object of the commit performed C<$num> commits ago. So 0 will return the
253 latest commit.
254  
255 If num isn't provided, will return a L<Git::Repository::Log::Iterator>
256 object starting from the latest commit.
257  
258 =cut
259  
260 0 ▶sub log {
Subroutine log: not called CC=3 SCAR=24.8
Pod documented
261 0 my ($self, $num_ago) = @_;
262  
263 0 ▶ if (defined $num_ago && $num_ago =~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num_ago and $num_ago =~ /^\d+$/) { } 0 0 0
Condition0%0/3
defined $num_ago and $num_ago =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $num_ago and $num_ago =~ /^\d+$/
defined $num_ago $num_ago =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $num_agoB= $num_ago =~ /^\d+$/
ABresult
0X0
100
111
264 0 return Git::Repository::Log::Iterator->new($self->_repo, "HEAD~$num_ago")->next;
265 } else {
266 0 return Git::Repository::Log::Iterator->new($self->_repo, 'HEAD');
267 }
268 }
269  
270 =head1 AUTHOR
271  
272 Ido Perlmuter, C<< <ido at ido50.net> >>
273  
274 =head1 BUGS
275  
276 Please report any bugs or feature requests to C<bug-giddy at rt.cpan.org>, or through
277 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Giddy>. I will be notified, and then you'll
278 automatically be notified of progress on your bug as I make changes.
279  
280 =head1 SUPPORT
281  
282 You can find documentation for this module with the perldoc command.
283  
284 perldoc Giddy::Database
285  
286 You can also look for information at:
287  
288 =over 4
289  
290 =item * RT: CPAN's request tracker
291  
292 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Giddy>
293  
294 =item * AnnoCPAN: Annotated CPAN documentation
295  
296 L<http://annocpan.org/dist/Giddy>
297  
298 =item * CPAN Ratings
299  
300 L<http://cpanratings.perl.org/d/Giddy>
301  
302 =item * Search CPAN
303  
304 L<http://search.cpan.org/dist/Giddy/>
305  
306 =back
307  
308 =head1 LICENSE AND COPYRIGHT
309  
310 Copyright 2011 Ido Perlmuter.
311  
312 This program is free software; you can redistribute it and/or modify it
313 under the terms of either: the GNU General Public License as published
314 by the Free Software Foundation; or the Artistic License.
315  
316 See http://dev.perl.org/licenses/ for more information.
317  
318 =cut
319  
320 2 __PACKAGE__->meta->make_immutable;
« Giddy/Collection.pm ↑ Summary Giddy/Role/DocumentLoader.pm »