Database/Role/Object.pm

filter: Statementstmt 90.0% 27 / 30 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 100% 1 / 1 totaltotal 85.4% 41 / 48 SCAR 14.0
CC
4
Cov
86%
_get_object_attributes
3.0
__ANON__
2.1
__ANON__
2.0
CRAP 4.0
« Database/Role/Backend.pm ↑ Summary Database/Role/ObjectReader.pm »
1 package Git::Database::Role::Object;
2 25 $Git::Database::Role::Object::VERSION = '0.012';
3 25 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 requires qw( kind );
8  
9 has backend => (
10 is => 'lazy',
11 builder => sub {
12 0 ▶ require Git::Database::Backend::None;
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 0 return Git::Database::Backend::None->new;
14 },
15 isa => sub {
16 die "$_[0] DOES not Git::Database::Role::Backend"
17 1656 ▶ if !eval { $_[0]->does('Git::Database::Role::Backend') };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if not eval { do { $_[0]->does("Git::Database::Role::Backend") } } 0 1656 1656
18 },
19 25 predicate => 1,
20 );
21  
22 has digest => (
23 is => 'lazy',
24 620 ▶ builder => sub { $_[0]->backend->hash_object( $_[0] ); },
Subroutine _build_digest: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1320 ▶ coerce => sub { lc $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 25 isa =>
27 quote_sub(q{ die "Not a SHA-1 digest" if $_[0] !~ /^[0-9a-f]{40}/; }),
28 predicate => 1,
29 );
30  
31 has size => (
32 is => 'lazy',
33 885 ▶ builder => sub { length $_[0]->content },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 25 predicate => 1,
35 );
36  
37 has content => (
38 is => 'rwp',
39 0 ▶ builder => sub { $_[0]->_get_object_attributes->{content} },
Subroutine _build_content: not called CC=1 SCAR=1.2
40 25 predicate => 1,
41 lazy => 1,
42 );
43  
44 399 ▶sub as_string { $_[0]->content; }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
45  
46 sub _get_object_attributes {
47 392 ▶ my ($self) = @_;
Subroutine _get_object_attributes: called CC=3 SCAR=11.0
48 392 my $backend = $self->backend;
49 392 ▶ die sprintf "%s can't get_object_attributes", $backend
Branch: 1 branch
BranchTrueFalseTotal
unless $backend->can("get_object_attributes") 0 392 392
50 if !$backend->can('get_object_attributes');
51  
52 392 my $attr = $backend->get_object_attributes( $self->digest );
53 392 ▶ die sprintf '%s %s not found in %s', $self->kind, $self->digest, $backend
Branch: 1 branch
BranchTrueFalseTotal
unless $attr 77 315 392
54 if !$attr;
55 315 return $attr;
56 }
57  
58 25 1;
59  
60 __END__
61  
62 =pod
63  
64 =for Pod::Coverage
65 has_backend
66 has_content
67 has_digest
68 has_size
69  
70 =head1 NAME
71  
72 Git::Database::Role::Object - Role for objects from the Git object database
73  
74 =head1 VERSION
75  
76 version 0.012
77  
78 =head1 SYNOPSIS
79  
80 package Git::Database::Object::Blob;
81  
82 use Moo;
83  
84 with 'Git::Database::Role::Object';
85  
86 sub kind { 'blob' }
87  
88 1;
89  
90 =head1 DESCRIPTION
91  
92 Git::Database::Role::Object provides the generic behaviour for all
93 L<Git::Database> objects obtained from or stored into the Git object
94 database.
95  
96 When creating a new object meant to be added to the Git object database
97 (via L<backend>), only the L</content> attribute is actually required.
98  
99 New objects are typically created via L<Git::Database::Role::ObjectReader>'s
100 L<get_object|Git::Database::Role::ObjectReader/get_object> method,
101 rather than by calling C<new> directly. This is when the object data is
102 fetched from the Git object database.
103  
104 =head1 ATTRIBUTES
105  
106 The L<content>, L<size> and L<digest> attribute are lazy, and can be
107 computed from the others: L<size> from L<content>, L<content> from
108 L<digest> (if the object exists in the backend store), and L<digest>
109 from L<content>. All attributes have a predicate method.
110  
111 Additional attributes in some classes may add other ways to compute
112 the content.
113  
114 Creating a new object with inconsistent C<kind>, C<size>,
115 C<content> and C<digest> attributes can only end in
116 tears. This is also true for additional attributes such as
117 L<directory_entries|Git::Database::Object::Tree/directory_entries>,
118 L<commit_info|Git::Database::Object::Commit/commit_info>, and
119 L<tag_info|Git::Database::Object::Tag/tag_info>.
120  
121 For now, as soon as the L</content> of a Git::Database::Role::Object is
122 needed, it is fully loaded in memory.
123  
124 =head2 backend
125  
126 A L<Git::Database::Role::Backend> from which the object comes from
127 (or will be stored into). It is typically used by the attribute builders.
128  
129 If none is provided, a L<Git::Database::Backend::None> is used, which
130 is only able to compute the L<digest>.
131  
132 =head2 content
133  
134 The object's actual content.
135  
136 =head2 size
137  
138 The size (in bytes) of the object content.
139  
140 =head2 digest
141  
142 The SHA-1 digest of the object, as computed by Git.
143  
144 If set at creation time, it is internally converted to lowercase.
145  
146 =head1 METHODS
147  
148 =head2 as_string
149  
150 Return a string representation of the content.
151  
152 By default, this is the same as C<content()>, but some classes may
153 override it.
154  
155 =head1 REQUIRED METHODS
156  
157 =head2 kind
158  
159 Returns the object "kind".
160  
161 In Git, this is one of C<blob>, C<tree>, C<commit>, and C<tag>.
162  
163 =head1 SEE ALSO
164  
165 L<Git::Database::Object::Blob>,
166 L<Git::Database::Object::Tree>,
167 L<Git::Database::Object::Commit>,
168 L<Git::Database::Object::Tag>.
169  
170 =head1 AUTHOR
171  
172 Philippe Bruhat (BooK) <book@cpan.org>.
173  
174 =head1 COPYRIGHT
175  
176 Copyright 2013-2016 Philippe Bruhat (BooK), all rights reserved.
177  
178 =head1 LICENSE
179  
180 This program is free software; you can redistribute it and/or modify it
181 under the same terms as Perl itself.
182  
183 =cut
« Database/Role/Backend.pm ↑ Summary Database/Role/ObjectReader.pm »