File Coverage

blib/lib/String/Sections/Result.pm
Criterion Covered Total %
statement 73 76 96.0
branch 9 10 90.0
condition 2 6 33.3
subroutine 21 21 100.0
pod 10 10 100.0
total 115 123 93.5


line stmt bran cond sub pod time code
1 16     16   1374 use strict;
  16         37  
  16         677  
2 16     16   86 use warnings;
  16         34  
  16         764  
3              
4             package String::Sections::Result;
5             BEGIN {
6 16     16   568 $String::Sections::Result::AUTHORITY = 'cpan:KENTNL';
7             }
8             {
9             $String::Sections::Result::VERSION = '0.3.2';
10             }
11              
12             # ABSTRACT: Glorified wrapper around a hash representing a parsed String::Sections result
13             #
14              
15              
16 16     16   1019 use Moo 1.000008;
  16         14429  
  16         178  
17              
18             ## no critic (RequireArgUnpacking)
19              
20 2     2   15 sub _croak { require Carp; goto &Carp::croak; }
  2         342  
21 2     2   13 sub _blessed { require Scalar::Util; goto &Scalar::Util::blessed }
  2         18  
22              
23              
24              
25 16     16   9323 use Types::Standard qw( HashRef ArrayRef ScalarRef Str Maybe );
  16         107217  
  16         262  
26              
27             our $TYPE_SECTION_NAME = Str;
28             our $TYPE_SECTION_NAMES = ArrayRef [Str];
29             our $TYPE_SECTION = ScalarRef [Str];
30             our $TYPE_OPTIONAL_SECTION = Maybe [$TYPE_SECTION];
31             our $TYPE_CURRENT = Maybe [Str];
32             our $TYPE_SECTIONS = HashRef [$TYPE_SECTION];
33              
34             has 'sections' => (
35             is => ro =>,
36             isa => $TYPE_SECTIONS,
37             lazy => 1,
38             builder => sub {
39 14     14   13433 return {};
40             },
41             );
42              
43              
44              
45             has '_current' => (
46             is => ro =>,
47             isa => $TYPE_CURRENT,
48             reader => '_current',
49             writer => 'set_current',
50             predicate => 'has_current',
51             lazy => 1,
52 2     2   3594 builder => sub { return _croak('current never set, but tried to use it') },
53             );
54              
55              
56             has '_section_names' => (
57             is => ro =>,
58             isa => $TYPE_SECTION_NAMES,
59             lazy => 1,
60 17     17   11334 builder => sub { return [] },
61             );
62              
63              
64             sub section {
65 6     6 1 3905 $TYPE_SECTION_NAME->assert_valid( $_[1] );
66 6         254 return $_[0]->sections->{ $_[1] };
67             }
68              
69              
70 24     24 1 7519 sub section_names { return ( my @list = @{ $_[0]->_section_names } ) }
  24         861  
71              
72              
73 1     1 1 3 sub section_names_sorted { return ( my @list = sort @{ $_[0]->_section_names } ) }
  1         33  
74              
75              
76             sub has_section {
77 6     6 1 2921 $TYPE_SECTION_NAME->assert_valid( $_[1] );
78 6         232 return exists $_[0]->sections->{ $_[1] };
79             }
80              
81              
82             sub set_section {
83 1     1 1 2818 $TYPE_SECTION_NAME->assert_valid( $_[1] );
84 1         60 $TYPE_SECTION->assert_valid( $_[2] );
85 1 50       51 if ( not exists $_[0]->sections->{ $_[1] } ) {
86 1         16 push @{ $_[0]->_section_names }, $_[1];
  1         117  
87             }
88 1         31 $_[0]->sections->{ $_[1] } = $_[2];
89 1         10 return;
90             }
91              
92              
93             sub append_data_to_current_section {
94 56     56 1 2628 $TYPE_OPTIONAL_SECTION->assert_valid( $_[1] );
95 56 100       2734 if ( not exists $_[0]->sections->{ $_[0]->_current } ) {
96 21         9414 push @{ $_[0]->_section_names }, $_[0]->_current;
  21         306  
97 21         820 my $blank = q{};
98 21         478 $_[0]->sections->{ $_[0]->_current } = \$blank;
99             }
100 56 100       1795 if ( defined $_[1] ) {
101 36         56 ${ $_[0]->sections->{ $_[0]->_current } } .= ${ $_[1] };
  36         734  
  36         1068  
102             }
103 56         148 return;
104             }
105              
106              
107             sub append_data_to_section {
108 2     2 1 1192 $TYPE_SECTION_NAME->assert_valid( $_[1] );
109 2         19 $TYPE_OPTIONAL_SECTION->assert_valid( $_[2] );
110 2 100       106 if ( not exists $_[0]->sections->{ $_[1] } ) {
111 1         9 push @{ $_[0]->_section_names }, $_[1];
  1         19  
112 1         7 my $blank = q{};
113 1         19 $_[0]->sections->{ $_[1] } = \$blank;
114             }
115 2 100       24 if ( defined $_[2] ) {
116 1         2 ${ $_[0]->sections->{ $_[1] } } .= ${ $_[2] };
  1         24  
  1         9  
117             }
118 2         5 return;
119             }
120              
121              
122             sub shallow_clone {
123 1   33 1 1 547 my $class = _blessed( $_[0] ) || $_[0];
124 1         33 my $instance = $class->new();
125 1         27 for my $name ( $_[0]->section_names ) {
126 0         0 $instance->set_section( $name, $_[0]->sections->{$name} );
127             }
128 1         13 return $instance;
129             }
130              
131              
132             sub shallow_merge {
133 1   33 1 1 646 my $class = _blessed( $_[0] ) || $_[0];
134 1         23 my $instance = $class->new();
135 1         16 for my $name ( $_[0]->section_names ) {
136 0         0 $instance->set_section( $name, $_[0]->sections->{$name} );
137             }
138 1         12 for my $name ( $_[1]->section_names ) {
139 0         0 $instance->set_section( $name, $_[1]->sections->{$name} );
140             }
141 1         21 return $instance;
142             }
143              
144              
145             sub _compose_section {
146 8     8   2617 $TYPE_SECTION_NAME->assert_valid( $_[1] );
147 8         54 return sprintf qq[__[%s]__\n%s], $_[1], ${ $_[0]->sections->{ $_[1] } };
  8         206  
148             }
149              
150              
151             sub to_s {
152 1     1 1 714 my $self = $_[0];
153 1         7 return join qq{\n}, map { $self->_compose_section($_) } $self->section_names_sorted;
  4         55  
154             }
155              
156             1;
157              
158             __END__