File Coverage

blib/lib/Git/Database/Role/Backend.pm
Criterion Covered Total %
statement 25 26 96.1
branch 3 8 37.5
condition 1 6 16.6
subroutine 8 8 100.0
pod 2 2 100.0
total 39 50 78.0


line stmt bran cond sub pod time code
1             package Git::Database::Role::Backend;
2             $Git::Database::Role::Backend::VERSION = '0.012';
3 8     8   8861 use Digest::SHA;
  8         20536  
  8         444  
4              
5 8     8   63 use Git::Database::Object::Blob;
  8         16  
  8         218  
6 8     8   41 use Git::Database::Object::Tree;
  8         19  
  8         212  
7 8     8   52 use Git::Database::Object::Commit;
  8         16  
  8         197  
8 8     8   76 use Git::Database::Object::Tag;
  8         15  
  8         180  
9              
10 8     8   41 use Moo::Role;
  8         19  
  8         80  
11              
12             has store => (
13             is => 'ro',
14             required => 1,
15             predicate => 1,
16             );
17              
18             sub hash_object {
19 529     529 1 93495 my ( $self, $object ) = @_;
20 529         5225 return Digest::SHA->new->add( $object->kind, ' ', $object->size, "\0",
21             $object->content )->hexdigest;
22             }
23              
24             sub create_object {
25 192     192 1 1143 my $self = shift;
26 192         1041 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 0     3452 ? %{ $_[0] }
  0 50       0  
    50          
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 50 33     2526 if !exists $attr{kind} || !defined $attr{kind};
42              
43             return
44 192         5776 "Git::Database::Object::\u$attr{kind}"->new( %attr, backend => $self );
45             }
46              
47             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