File Coverage

blib/lib/Indent.pm
Criterion Covered Total %
statement 49 49 100.0
branch 16 16 100.0
condition n/a
subroutine 10 10 100.0
pod 5 5 100.0
total 80 80 100.0


line stmt bran cond sub pod time code
1             package Indent;
2              
3 7     7   134998 use strict;
  7         7  
  7         184  
4 7     7   19 use warnings;
  7         12  
  7         330  
5              
6 7     7   2472 use Class::Utils qw(set_params);
  7         65144  
  7         118  
7 7     7   458 use Error::Pure qw(err);
  7         20  
  7         210  
8 7     7   22 use Readonly;
  7         10  
  7         2724  
9              
10             # Constants.
11             Readonly::Scalar my $EMPTY_STR => q{};
12              
13             our $VERSION = 0.11;
14              
15             # Constructor.
16             sub new {
17 16     16 1 1050768 my ($class, @params) = @_;
18 16         27 my $self = bless {}, $class;
19              
20             # Default indent.
21 16         50 $self->{'indent'} = $EMPTY_STR;
22              
23             # Every next indent string.
24 16         29 $self->{'next_indent'} = "\t";
25              
26             # Process params.
27 16         67 set_params($self, @params);
28              
29             # Check to 'next_indent' parameter.
30 14 100       118 if (! defined $self->{'next_indent'}) {
31 1         3 err "'next_indent' parameter must be defined.";
32             }
33 13 100       31 if (ref $self->{'next_indent'}) {
34 2         7 err "'next_indent' parameter must be a string.";
35             }
36              
37             # Check to 'indent' parameter.
38 11 100       23 if (! defined $self->{'indent'}) {
39 1         3 err "'indent' parameter must be defined.";
40             }
41 10 100       23 if (ref $self->{'indent'}) {
42 2         5 err "'indent' parameter must be a string.";
43             }
44              
45             # Object.
46 8         25 return $self;
47             }
48              
49             # Add an indent to global indent.
50             sub add {
51 7     7 1 24 my ($self, $indent) = @_;
52 7 100       24 if (! defined $indent) {
53 2         5 $indent = $self->{'next_indent'};
54             }
55 7         18 $self->{'indent'} .= $indent;
56 7         14 return 1;
57             }
58              
59             # Get a indent value.
60             sub get {
61 13     13 1 1091 my $self = shift;
62 13         55 return $self->{'indent'};
63             }
64              
65             # Remove an indent from global indent.
66             sub remove {
67 3     3 1 11 my ($self, $indent) = @_;
68 3 100       4 if (! defined $indent) {
69 1         2 $indent = $self->{'next_indent'};
70             }
71 3         4 my $indent_length = length $indent;
72 3 100       9 if (substr($self->{'indent'}, -$indent_length) ne $indent) {
73 1         5 err "Cannot remove indent '$indent'.";
74             }
75 2         5 $self->{'indent'} = substr $self->{'indent'}, 0, -$indent_length;
76 2         2 return 1;
77             }
78              
79             # Reseting indent.
80             sub reset {
81 2     2 1 5 my ($self, $reset_value) = @_;
82 2 100       5 if (! defined $reset_value) {
83 1         2 $reset_value = $EMPTY_STR;
84             }
85 2         3 $self->{'indent'} = $reset_value;
86 2         2 return 1;
87             }
88              
89             1;
90              
91             __END__