File Coverage

blib/lib/Indent/Data.pm
Criterion Covered Total %
statement 45 45 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 70 70 100.0


line stmt bran cond sub pod time code
1             package Indent::Data;
2              
3 4     4   83283 use strict;
  4         38  
  4         122  
4 4     4   21 use warnings;
  4         5  
  4         120  
5              
6 4     4   2000 use Class::Utils qw(set_params);
  4         116694  
  4         89  
7 4     4   318 use Error::Pure qw(err);
  4         9  
  4         168  
8 4     4   1702 use Indent::Utils qw(line_size_check string_len);
  4         10  
  4         220  
9 4     4   27 use Readonly;
  4         9  
  4         1689  
10              
11             # Constants.
12             Readonly::Scalar my $EMPTY_STR => q{};
13             Readonly::Scalar my $LINE_SIZE => 79;
14              
15             our $VERSION = 0.07;
16              
17             # Constructor.
18             sub new {
19 11     11 1 12570 my ($class, @params) = @_;
20 11         28 my $self = bless {}, $class;
21              
22             # Options.
23 11         34 $self->{'line_size'} = $LINE_SIZE;
24 11         20 $self->{'next_indent'} = "\t";
25              
26             # Output.
27 11         19 $self->{'output_separator'} = "\n";
28              
29             # Process params.
30 11         42 set_params($self, @params);
31              
32             # 'line_size' check.
33 9         165 line_size_check($self);
34              
35             # Error with 'next_indent' length greater than 'line_size'.
36 9 100       29 if ($self->{'line_size'} <= length $self->{'next_indent'}) {
37 1         8 err "Bad line_size = '$self->{'line_size'}' ".
38             "or length of string '$self->{'next_indent'}'.";
39             }
40              
41             # Object.
42 8         32 return $self;
43             }
44              
45             # Parses tag to indented data.
46             sub indent {
47 10     10 1 2996 my ($self, $data, $act_indent, $non_indent) = @_;
48              
49             # Undef indent.
50 10 100       24 if (! $act_indent) {
51 4         7 $act_indent = $EMPTY_STR;
52             }
53              
54             # If non_indent data, than return.
55 10 100       18 if ($non_indent) {
56 1         5 return $act_indent.$data;
57             }
58              
59             # Check to actual indent maximal length.
60 9 100       25 if (string_len($act_indent) > ($self->{'line_size'}
61             - string_len($self->{'next_indent'}) - 1)) {
62              
63 2         10 err 'Bad actual indent value. Length is greater then '.
64             '(\'line_size\' - \'size of next_indent\' - 1).';
65             }
66              
67             # Splits data.
68 7         10 my $first = undef;
69 7         15 my $second = $act_indent.$data;
70 7         12 my @data;
71 7         13 while (string_len($second) >= $self->{'line_size'}) {
72 30         53 $first = substr($second, 0, $self->{'line_size'});
73             $second = $act_indent.$self->{'next_indent'}.substr($second,
74 30         59 $self->{'line_size'});
75              
76             # Parsed part of data to @data array.
77 30         60 push @data, $first;
78             }
79              
80             # Add other data to @data array.
81 7 100 100     28 if ($second && $second ne $act_indent.$self->{'next_indent'}) {
82 3         7 push @data, $second;
83             }
84              
85             # Return as array or one line with output separator between its.
86 7 100       40 return wantarray ? @data : join($self->{'output_separator'}, @data);
87             }
88              
89             1;
90              
91             __END__