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   82673 use strict;
  4         36  
  4         116  
4 4     4   18 use warnings;
  4         9  
  4         112  
5              
6 4     4   1843 use Class::Utils qw(set_params);
  4         112869  
  4         90  
7 4     4   327 use Error::Pure qw(err);
  4         8  
  4         165  
8 4     4   1729 use Indent::Utils qw(line_size_check string_len);
  4         11  
  4         219  
9 4     4   26 use Readonly;
  4         8  
  4         1658  
10              
11             # Constants.
12             Readonly::Scalar my $EMPTY_STR => q{};
13             Readonly::Scalar my $LINE_SIZE => 79;
14              
15             our $VERSION = 0.06;
16              
17             # Constructor.
18             sub new {
19 11     11 1 12518 my ($class, @params) = @_;
20 11         28 my $self = bless {}, $class;
21              
22             # Options.
23 11         36 $self->{'line_size'} = $LINE_SIZE;
24 11         19 $self->{'next_indent'} = "\t";
25              
26             # Output.
27 11         25 $self->{'output_separator'} = "\n";
28              
29             # Process params.
30 11         40 set_params($self, @params);
31              
32             # 'line_size' check.
33 9         166 line_size_check($self);
34              
35             # Error with 'next_indent' length greater than 'line_size'.
36 9 100       27 if ($self->{'line_size'} <= length $self->{'next_indent'}) {
37 1         7 err "Bad line_size = '$self->{'line_size'}' ".
38             "or length of string '$self->{'next_indent'}'.";
39             }
40              
41             # Object.
42 8         30 return $self;
43             }
44              
45             # Parses tag to indented data.
46             sub indent {
47 10     10 1 3030 my ($self, $data, $act_indent, $non_indent) = @_;
48              
49             # Undef indent.
50 10 100       22 if (! $act_indent) {
51 4         8 $act_indent = $EMPTY_STR;
52             }
53              
54             # If non_indent data, than return.
55 10 100       20 if ($non_indent) {
56 1         4 return $act_indent.$data;
57             }
58              
59             # Check to actual indent maximal length.
60 9 100       24 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         13 my $first = undef;
69 7         14 my $second = $act_indent.$data;
70 7         10 my @data;
71 7         15 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         55 $self->{'line_size'});
75              
76             # Parsed part of data to @data array.
77 30         61 push @data, $first;
78             }
79              
80             # Add other data to @data array.
81 7 100 100     29 if ($second && $second ne $act_indent.$self->{'next_indent'}) {
82 3         5 push @data, $second;
83             }
84              
85             # Return as array or one line with output separator between its.
86 7 100       38 return wantarray ? @data : join($self->{'output_separator'}, @data);
87             }
88              
89             1;
90              
91             __END__