File Coverage

blib/lib/Pinwheel/View/String.pm
Criterion Covered Total %
statement 47 47 100.0
branch 20 20 100.0
condition 2 2 100.0
subroutine 10 10 100.0
pod 0 7 0.0
total 79 86 91.8


line stmt bran cond sub pod time code
1             package Pinwheel::View::String;
2              
3             use overload
4 11         130 '""' => \&to_string,
5 11     11   111419 '.' => \&concat;
  11         40108  
6              
7              
8             sub new
9             {
10 373     373 0 3632 my ($class, $raw, $escape) = @_;
11 373         532 my ($d);
12              
13 373 100       1589 $raw = ref($raw) ? $raw : [[$raw]];
14 373         1088 foreach (@$raw) {
15 700 100       1917 if (ref($_) eq 'Pinwheel::View::String') {
16 8         16 push @$d, @{$_->_export()};
  8         23  
17             } else {
18 692         1952 push @$d, $_;
19             }
20             }
21              
22 373         11748 return bless({d => $d, escape => $escape}, $class);
23             }
24              
25             sub clone
26             {
27 60     60 0 96 my ($self, $d) = @_;
28 60         525 return bless({d => $d, escape => $self->{escape}}, ref($self));
29             }
30              
31             sub concat
32             {
33 273     273 0 915 my ($self, $b, $order) = @_;
34 273         860 my $a = $self->{d};
35              
36 273 100       2418 $b = ref($b) ? $b->_export() : [$b];
37 273 100       807 if (!defined($order)) {
    100          
38             # $a .= $b
39 222         941 push @$a, $_ foreach (@$b);
40             } elsif (!$order) {
41             # $a . $b
42 47         619 $self = $self->clone([@$a, @$b]);
43             } else {
44             # $b . $a
45 4         18 $self = $self->clone([@$b, @$a]);
46             }
47              
48 273         7936 return $self;
49             }
50              
51             sub concat_raw
52             {
53 332     332 0 617 push @{$_[0]->{d}}, [$_[1]];
  332         1986  
54 332         15708 return $_[0];
55             }
56              
57             sub add
58             {
59 2     2 0 17 my ($self, $b) = @_;
60 2 100       60 $b = ref($b) ? $b->_export() : [$b];
61 2         5 return $self->clone([@{$self->{d}}, @$b]);
  2         14  
62             }
63              
64             sub radd
65             {
66 2     2 0 13 my ($self, $b) = @_;
67 2 100       11 $b = ref($b) ? $b->_export() : [$b];
68 2         5 return $self->clone([@$b, @{$self->{d}}]);
  2         12  
69             }
70              
71             sub to_string
72             {
73 334     334 0 4649 my ($self) = @_;
74 334         643 my ($escape, $s);
75              
76 334   100 54   1852 $escape = $self->{escape} || sub { $_[0] };
  54         516  
77 334         634 $s = '';
78 334 100       746 $s .= ref($_) ? $_->[0] : $escape->($_) foreach (@{$self->{d}});
  334         2969  
79 334         2482 $self->{d} = [[$s]];
80              
81 334         2626 return $s;
82             }
83              
84             sub _export
85             {
86 93     93   147 my ($self) = @_;
87              
88 93 100       528 return $self->{d} unless $self->{escape};
89 41 100       74 return [map { ref($_) ? $_ : [$self->{escape}($_)] } @{$self->{d}}];
  137         499  
  41         120  
90             }
91              
92              
93             1;