File Coverage

blib/lib/SVN/Dump/Record.pm
Criterion Covered Total %
statement 76 76 100.0
branch 26 26 100.0
condition 12 18 66.6
subroutine 25 25 100.0
pod 17 17 100.0
total 156 162 96.3


line stmt bran cond sub pod time code
1             package SVN::Dump::Record;
2             $SVN::Dump::Record::VERSION = '0.07';
3 11     11   71759 use strict;
  11         29  
  11         375  
4 11     11   55 use warnings;
  11         18  
  11         279  
5              
6 11     11   555 use SVN::Dump::Headers;
  11         20  
  11         334  
7 11     11   455 use SVN::Dump::Property;
  11         28  
  11         212  
8 11     11   443 use SVN::Dump::Text;
  11         18  
  11         985  
9              
10             my $NL = "\012";
11              
12             sub new {
13 746     746 1 2627 my ($class, @args) = @_;
14 746         1724 return bless {}, $class
15             }
16              
17             for my $attr (qw( headers_block property_block text_block included_record )) {
18 11     11   74 no strict 'refs';
  11         50  
  11         11278  
19 1268     1268   2747 *{"set_$attr"} = sub { $_[0]->{$attr} = $_[1]; };
20 5384     5384   12143 *{"get_$attr"} = sub { $_[0]->{$attr} };
21             }
22              
23             sub type {
24 1374     1374 1 2419 my ($self) = @_;
25 1374 100       3381 return $self->{headers_block} ? $self->{headers_block}->type() : '';
26             }
27              
28 861     861 1 1405 sub has_text { return defined $_[0]->get_text_block(); }
29 861     861 1 1939 sub has_prop { return defined $_[0]->get_property_block(); }
30             sub has_prop_only {
31 2   66 2 1 14 return defined $_[0]->get_property_block()
32             && !defined $_[0]->get_text_block();
33             }
34             sub has_prop_or_text {
35 453   100 453 1 704 return defined $_[0]->get_property_block()
36             || defined $_[0]->get_text_block();
37             }
38              
39             # length methods
40             sub property_length {
41 36     36 1 57 my ($self) = @_;
42 36         52 my $prop = $self->get_property_block();
43 36 100       93 return defined $prop ? length( $prop->as_string() ) : 0;
44             }
45              
46             sub text_length {
47 38     38 1 610 my ($self) = @_;
48 38         66 my $text = $self->get_text();
49 38 100       86 return defined $text ? length($text) : 0;
50             }
51              
52             sub as_string {
53 858     858 1 2752 my ($self) = @_;
54 858         1431 my $headers_block = $self->get_headers_block();
55              
56             # the headers block
57 858         1751 my $string = $headers_block->as_string();
58              
59             # the properties
60 858 100       1603 $string .= $self->get_property_block()->as_string()
61             if $self->has_prop();
62              
63             # the text
64 858 100       1649 $string .= $self->get_text_block()->as_string()
65             if $self->has_text();
66              
67             # is there an included record?
68 858 100       1505 if( my $included = $self->get_included_record() ) {
69 1         5 $string .= $included->as_string() . $NL;
70             }
71              
72             # add a record separator if needed
73 858         1374 my $type = $self->type();
74 858 100       2710 return $string if $type =~ /\A(?:format|uuid)\z/;
75              
76 762 100       1615 $string .= $type eq 'revision' ? $NL
    100          
77             : $self->has_prop_or_text() ? $NL x 2
78             : $NL ;
79              
80 762         3921 return $string;
81             }
82              
83             sub update_headers {
84 35     35 1 57 my ($self) = @_;
85 35         61 my $proplen = $self->property_length();
86 35         68 my $textlen = $self->text_length();
87              
88 35 100       56 $self->set_header( 'Text-content-length' => $textlen )
89             if defined $self->get_text_block();
90 35 100       91 $self->set_header( 'Prop-content-length', $proplen )
91             if $proplen;
92 35         68 $self->set_header( 'Content-length' => $proplen + $textlen );
93             }
94              
95             # access methods to the inner blocks
96             sub set_header {
97 109     109 1 187 my ($self, $h, $v) = @_;
98 109   66     167 my $headers = $self->get_headers_block()
99             || $self->set_headers_block( SVN::Dump::Headers->new() );
100 109         221 $headers->set( $h, $v );
101             }
102              
103             sub get_header {
104 176     176 1 1662 my ($self, $h) = @_;
105 176         279 return $self->get_headers_block()->get($h);
106             }
107              
108             sub set_property {
109 6     6 1 17 my ( $self, $k, $v ) = @_;
110 6   66     10 my $prop = $self->get_property_block()
111             || $self->set_property_block( SVN::Dump::Property->new() );
112 6         19 $prop->set( $k, $v );
113 6         12 $self->update_headers();
114 6         11 return $v;
115             }
116              
117             sub get_property {
118 3     3 1 1532 my ($self, $k) = @_;
119 3         11 return $self->get_property_block()->get($k);
120             }
121              
122             sub delete_property {
123 3     3 1 1088 my ( $self, @keys ) = @_;
124 3   33     6 my $prop = $self->get_property_block()
125             || $self->set_property_block( SVN::Dump::Property->new() );
126 3         11 my @result = $prop->delete(@keys);
127 3         9 $self->update_headers();
128 3 100       11 return wantarray ? @result : pop @result; # behave like delete()
129             }
130              
131             sub set_text {
132 26     26 1 81 my ($self, $t) = @_;
133 26   66     48 my $text_block = $self->get_text_block()
134             || $self->set_text_block( SVN::Dump::Text->new() );
135              
136 26         67 $text_block->set( $t );
137 26         52 $self->update_headers();
138 26         48 return $t;
139             }
140              
141             sub get_text {
142 206     206 1 1105 my ($self) = @_;
143 206         326 my $text_block = $self->get_text_block();
144 206 100       462 return defined $text_block ? $text_block->get() : undef;
145             }
146              
147             1;
148              
149             __END__