Database/Role/Backend.pm

filter: Statementstmt 96.5% 28 / 29 Branchbran 37.5% 3 / 8 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 73.6% 42 / 57 SCAR 20.6
CC
7
Cov
74%
create_object
15.2
BEGIN
1.0
BEGIN
1.0
CRAP 7.8
« Database/Backend/Git/Raw/Repository.pm ↑ Summary Database/Role/Object.pm »
1 package Git::Database::Role::Backend;
2 18 $Git::Database::Role::Backend::VERSION = '0.012';
3 18 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 18 ▶use Git::Database::Object::Blob;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Git::Database::Object::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Git::Database::Object::Commit;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Git::Database::Object::Tag;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 has store => (
13 is => 'ro',
14 required => 1,
15 predicate => 1,
16 );
17  
18 sub hash_object {
19 529 ▶ my ( $self, $object ) = @_;
Subroutine hash_object: called CC=1 SCAR=0.0
Pod documented
20 529 return Digest::SHA->new->add( $object->kind, ' ', $object->size, "\0",
21 $object->content )->hexdigest;
22 }
23  
24 sub create_object {
25 192 ▶ my $self = shift;
Subroutine create_object: called CC=7 SCAR=27.2
Pod documented
26 192 my $class = ref $self;
27  
28 # inspired by Moo::Object::BUILDARGS
29 my %attr = @_ == 1
30 ? defined $_[0] && ref $_[0] eq 'HASH'
31 192 ▶ ? %{ $_[0] }
Branch: 3 branches
BranchTrueFalseTotal
@_ == 1 ? : 0 192 192
defined $_[0] && ref $_[0] eq 'HASH' ? : 0 0 0
@_ % 2 ? : 0 192 192
Condition0%0/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
32 : die "Single parameters to create_object() must be a HASH ref"
33 . " data => " . $_[0] . "\n"
34 : @_ % 2
35 ? die "The create_object() method for $class expects a hash reference"
36 . " or a key/value list. You passed an odd number of arguments\n"
37 : @_;
38  
39 # kind is required
40 die "Missing required arguments: kind"
41 192 ▶ if !exists $attr{kind} || !defined $attr{kind};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr{"kind"} and defined $attr{"kind"} 0 192 192
Condition33%1/3
exists $attr{"kind"} and defined $attr{"kind"}
!ll&&!rl&&r
00192
MC/DC0%0/2
exists $attr{"kind"} and defined $attr{"kind"}
exists $attr{"kind"} defined $attr{"kind"}
Truth table33%1/3
A and B
A= exists $attr{"kind"}B= defined $attr{"kind"}
ABresult
0X0
100
111
42  
43 return
44 192 "Git::Database::Object::\u$attr{kind}"->new( %attr, backend => $self );
45 }
46  
47 18 1;
48  
49 __END__
50  
51 =pod
52  
53 =for Pod::Coverage
54 has_store
55  
56 =head1 NAME
57  
58 Git::Database::Role::Backend - Abstract role for a Git database backend
59  
60 =head1 VERSION
61  
62 version 0.012
63  
64 =head1 SYNOPSIS
65  
66 package MyGitBackend;
67  
68 use Moo;
69 use namespace::clean;
70  
71 with 'Git::Database::Role::Backend';
72  
73 1;
74  
75 =head1 DESCRIPTION
76  
77 The C<Git::Database::Role::Backend> role encapsulate code for the user-facing
78 store objects. To be usable as a L<backend|Git::Repository::Tutorial/backend>,
79 a class must at least do this role.
80  
81 =head1 REQUIRED ATTRIBUTES
82  
83 =head2 store
84  
85 The L<store|Git::Database::Tutorial/store> that will store and retrieve
86 data from the Git repository.
87  
88 There is a C<has_store> predicate method for this attribute.
89  
90 =head1 METHODS
91  
92 =head2 hash_object
93  
94 # the empty tree
95 my $tree = Git::Database::Object::Tree->new( content => '' );
96
97 # 4b825dc642cb6eb9a060e54bf8d69288fbee4904
98 my $digest = $backend->hash_object( $tree );
99  
100 Compute and return the SHA-1 digest for the given object.
101  
102 May be called from the L<digest|Git::Database::Role::Object/digest>
103 builder for one of the object classes (L<Git::Database::Object::Blob>,
104 L<Git::Database::Object::Tree>, L<Git::Database::Object::Commit>,
105 L<Git::Database::Object::Tag>), so the implementation should not try to
106 shortcut and call C<< $object->digest >>.
107  
108 The role provides a Perl implementation for it, but most backends will
109 want to override it for performance reasons.
110  
111 =head2 create_object
112  
113 # argument is a HASH reference
114 my $object = $store->create_object( \%attr );
115  
116 # arguments is a list of pairs
117 my $object = $store->create_object( %attr );
118  
119 Return an object instance of an object doing the
120 L<Git::Database::Role::Object> role, or dies if C<kind> is unknown
121 or not provided.
122  
123 This method assumes the provided attribute values are consistent for the
124 object kind. B<Behaviour is undefined if the various attributes are not
125 internally consistent.> (E.g. if the size does not match the content.)
126  
127 Note: this method creates new objects, but does not store them in
128 the underlying Git datatase. Do save them in the database, use
129 L<Git::Database::Role::Object::Writer/put_object>.
130  
131 =head1 AUTHOR
132  
133 Philippe Bruhat (BooK) <book@cpan.org>
134  
135 =head1 COPYRIGHT
136  
137 Copyright 2016 Philippe Bruhat (BooK), all rights reserved.
138  
139 =head1 LICENSE
140  
141 This program is free software; you can redistribute it and/or modify it
142 under the same terms as Perl itself.
143  
144 =cut
« Database/Backend/Git/Raw/Repository.pm ↑ Summary Database/Role/Object.pm »