| 1 | package WebService::Mattermost::Helper::Table; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | # ABSTRACT: Markdown table generator. | ||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 1 | ▶ | use Types::Standard qw(ArrayRef Enum Str); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||
| 8 | ################################################################################ | ||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | 1 | has alignment => (is => 'ro', isa => ArrayRef[Enum[ qw(l r c) ]], required => 0); | |||||||||||||||||||||||
| 11 | 1 | has values => (is => 'ro', isa => ArrayRef, required => 1); | |||||||||||||||||||||||
| 12 | 1 | has headers => (is => 'ro', isa => ArrayRef, required => 1); | |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||
| 14 | 1 | has _headers => (is => 'ro', isa => Str, lazy => 1, builder => '_build__headers'); | |||||||||||||||||||||||
| 15 | 1 | has table => (is => 'ro', isa => Str, lazy => 1, builder => '_build_table'); | |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||
| 17 | 1 | has divider => (is => 'ro', isa => Str, default => q{|}); | |||||||||||||||||||||||
| 18 | 1 | has align_l => (is => 'ro', isa => Str, default => ':----|'); | |||||||||||||||||||||||
| 19 | 1 | has align_r => (is => 'ro', isa => Str, default => '----:|'); | |||||||||||||||||||||||
| 20 | 1 | has align_c => (is => 'ro', isa => Str, default => ':---:|'); | |||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||
| 22 | ################################################################################ | ||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||
| 24 | sub _build__headers { | ||||||||||||||||||||||||
| 25 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||
|
Subroutine
_build__headers: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||
| 27 | 2 | my $headers = q{}; | |||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||
| 29 | 2 | foreach my $h (@{$self->headers}) { | |||||||||||||||||||||||
| 30 | 6 | $headers .= sprintf '%s %s', $self->divider, $h; | |||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||
| 33 | 2 | $headers .= $self->divider."\n".$self->divider; | |||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||
| 35 | 2 | ▶ | for (my $i = 0; $i < scalar @{$self->headers}; $i++) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 36 | 6 | ▶ | my $al = $self->alignment && $self->alignment->[$i] || 'l'; | ||||||||||||||||||||||
|
Condition80%4/5
$self->alignment && $self->alignment->[$i] || 'l'
$self->alignment and $self->alignment->[$i]
MC/DC50%1/2
$self->alignment && $self->alignment->[$i] || 'l'
$self->alignment
$self->alignment->[$i]
Truth table66%2/3
A and B
A= $self->alignmentB= $self->alignment->[$i]
| |||||||||||||||||||||||||
| 37 | 6 | my $attr = "align_${al}"; | |||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||
| 39 | 6 | $headers .= $self->$attr; | |||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||
| 42 | 2 | return $headers; | |||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||
| 45 | sub _build_table { | ||||||||||||||||||||||||
| 46 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||
|
Subroutine
_build_table: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||
| 48 | 2 | my $table = $self->_headers."\n"; | |||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||
| 50 | 2 | foreach my $v (@{$self->values}) { | |||||||||||||||||||||||
| 51 | 8 | $table .= $self->divider; | |||||||||||||||||||||||
| 52 | 8 | $table .= join $self->divider, @{$v}; | |||||||||||||||||||||||
| 53 | 8 | $table .= $self->divider."\n"; | |||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||
| 56 | 2 | return $table; | |||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||
| 59 | ################################################################################ | ||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | 1 | 1; | |||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||
| 63 | __END__ | ||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||
| 65 | =pod | ||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||
| 67 | =encoding UTF-8 | ||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||
| 71 | WebService::Mattermost::Helper::Table - Markdown table generator. | ||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||
| 73 | =head1 VERSION | ||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||
| 75 | version 0.28 | ||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||
| 77 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | Format text as tables for Mattermost. | ||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | =head2 USAGE | ||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||
| 83 | use WebService::Mattermost::Helper::Table; | ||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||
| 85 | my $helper = WebService::Mattermost::Helper::Table->new({ | ||||||||||||||||||||||||
| 86 | alignment => [ qw(r l l) ], | ||||||||||||||||||||||||
| 87 | headers => [ 'ID', 'Title', 'Date Created' ], | ||||||||||||||||||||||||
| 88 | values => [ | ||||||||||||||||||||||||
| 89 | [ 1, 'First row', '2018-09-16' ], | ||||||||||||||||||||||||
| 90 | [ 2, 'Second row', '2018-09-17' ], | ||||||||||||||||||||||||
| 91 | [ 3, 'Third row', '2018-09-17' ], | ||||||||||||||||||||||||
| 92 | ], | ||||||||||||||||||||||||
| 93 | }); | ||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||
| 95 | print $helper->table; | ||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||
| 97 | =head1 SEE ALSO | ||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||
| 99 | =over 4 | ||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||
| 101 | =item L<Mattermost Markdown|https://docs.mattermost.com/help/messaging/formatting-text.html#tables> | ||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||
| 103 | =back | ||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||
| 105 | =head1 AUTHOR | ||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||
| 107 | Mike Jones <mike@netsplit.org.uk> | ||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||
| 109 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||
| 111 | This software is Copyright (c) 2020 by Mike Jones. | ||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||
| 113 | This is free software, licensed under: | ||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||
| 115 | The MIT (X11) License | ||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||