File Coverage

blib/lib/Git/Database/Role/Object.pm
Criterion Covered Total %
statement 18 18 100.0
branch 3 4 75.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 30 31 96.7


line stmt bran cond sub pod time code
1             package Git::Database::Role::Object;
2             $Git::Database::Role::Object::VERSION = '0.011';
3 7     7   70047 use Sub::Quote;
  7         34030  
  7         421  
4              
5 7     7   53 use Moo::Role;
  7         14  
  7         47  
6              
7             requires qw( kind );
8              
9             has backend => (
10             is => 'lazy',
11             builder => sub {
12 244     244   6104 require Git::Database::Backend::None;
13 244         9122 return Git::Database::Backend::None->new;
14             },
15             isa => sub {
16             die "$_[0] DOES not Git::Database::Role::Backend"
17             if !eval { $_[0]->does('Git::Database::Role::Backend') };
18             },
19             predicate => 1,
20             );
21              
22             has digest => (
23             is => 'lazy',
24 620     620   402769 builder => sub { $_[0]->backend->hash_object( $_[0] ); },
25             coerce => sub { lc $_[0] },
26             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     885   78100 builder => sub { length $_[0]->content },
34             predicate => 1,
35             );
36              
37             has content => (
38             is => 'rwp',
39 77     77   1888 builder => sub { $_[0]->_get_object_attributes->{content} },
40             predicate => 1,
41             lazy => 1,
42             );
43              
44 399     399 1 602662 sub as_string { $_[0]->content; }
45              
46             sub _get_object_attributes {
47 392     392   1571 my ($self) = @_;
48 392         9688 my $backend = $self->backend;
49 392 50       5377 die sprintf "%s can't get_object_attributes", $backend
50             if !$backend->can('get_object_attributes');
51              
52 392         8486 my $attr = $backend->get_object_attributes( $self->digest );
53 392 100       3117 die sprintf '%s %s not found in %s', $self->kind, $self->digest, $backend
54             if !$attr;
55 315         3890 return $attr;
56             }
57              
58             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.011
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