File Coverage

blib/lib/Text/vCard/Precisely/V4/Node.pm
Criterion Covered Total %
statement 30 32 93.7
branch 12 20 60.0
condition 5 9 55.5
subroutine 6 6 100.0
pod 0 1 0.0
total 53 68 77.9


line stmt bran cond sub pod time code
1             package Text::vCard::Precisely::V4::Node;
2              
3 14     14   102 use Carp;
  14         33  
  14         920  
4 14     14   88 use Encode qw(decode_utf8);
  14         34  
  14         796  
5              
6 14     14   87 use Moose;
  14         30  
  14         118  
7 14     14   95364 use Moose::Util::TypeConstraints;
  14         35  
  14         123  
8              
9             extends 'Text::vCard::Precisely::V3::Node';
10              
11             enum 'Name' => [
12             qw( FN ORG TITLE ROLE
13             ADR TEL EMAIL PHOTO LOGO URL
14             TZ GEO IMPP LANG XML KEY NOTE
15             SOURCE SOUND FBURL CALADRURI CALURI
16             RELATED X-SOCIALPROFILE
17             )
18             ];
19             has name => ( is => 'rw', required => 1, isa => 'Name' );
20              
21             subtype 'SortAs' => as 'Str' =>
22             where { decode_utf8($_) =~ m|^[\p{ascii}\w\s]+$|s } # Does everything pass?
23             => message {"The SORT-AS you provided, $_, was not supported"};
24             has sort_as => ( is => 'rw', isa => 'Maybe[SortAs]' );
25              
26             subtype 'PIDNum' => as 'Num' => where {m/^\d(?:.\d)?$/s}
27             => message {"The PID you provided, $_, was not supported"};
28             has pid => ( is => 'rw', isa => subtype 'PID' => as 'ArrayRef[PIDNum]' );
29              
30             subtype 'ALTID' => as 'Int' => where { $_ > 0 and $_ <= 100 }
31             => message {"The number you provided, $_, was not supported in 'ALTID'"};
32             has altID => ( is => 'rw', isa => 'ALTID' );
33              
34             subtype 'MediaType' => as 'Str' =>
35             where {m{^(?:application|audio|example|image|message|model|multipart|text|video)/[\w+\-\.]+$}is}
36             => message {"The MediaType you provided, $_, was not supported"};
37             has media_type => ( is => 'rw', isa => 'MediaType' );
38              
39             sub as_string {
40 65     65 0 124 my ($self) = @_;
41 65         112 my @lines;
42 65   33     1858 push @lines, $self->name() || croak "Empty name";
43 65 50       1841 push @lines, 'ALTID=' . $self->altID() if $self->altID();
44 65 50       1688 push @lines, 'PID=' . join ',', @{ $self->pid() } if $self->pid();
  0         0  
45 65 100 66     1767 push @lines, 'TYPE="' . join( ',', map { uc $_ } @{ $self->types() } ) . '"'
  3         13  
  3         74  
46             if ref $self->types() eq 'ARRAY' and $self->types()->[0];
47 65 50       1712 push @lines, 'PREF=' . $self->pref() if $self->pref();
48 65 50       1757 push @lines, 'MEDIATYPE=' . $self->media_type() if $self->media_type();
49 65 50       1771 push @lines, 'LANGUAGE=' . $self->language() if $self->language();
50 65 100 66     1761 push @lines, 'SORT-AS="' . $self->sort_as() . '"'
51             if $self->sort_as()
52             and $self->name() =~ /^(?:FN|ORG)$/;
53              
54 65         1761 my $content = $self->content();
55             my $string
56             = join( ';', @lines ) . ':'
57             . (
58             ref($content) eq 'Array'
59 65 0       1797 ? map { $self->name() eq 'GEO' ? $content : $self->_escape($_) } @$content
  0 100       0  
    50          
60             : $self->name() eq 'GEO' ? $content
61             : $self->_escape($content)
62             );
63 65         189 return $self->fold($string);
64             }
65              
66             __PACKAGE__->meta->make_immutable;
67 14     14   44994 no Moose;
  14         33  
  14         80  
68              
69             1;