File Coverage

blib/lib/Salus.pm
Criterion Covered Total %
statement 61 62 98.3
branch 14 18 77.7
condition 1 2 50.0
subroutine 11 11 100.0
pod 1 1 100.0
total 88 94 93.6


line stmt bran cond sub pod time code
1             package Salus;
2              
3 3     3   294544 use 5.006;
  3         12  
4 3     3   16 use strict;
  3         6  
  3         110  
5 3     3   14 use warnings;
  3         4  
  3         238  
6             our $VERSION = '0.09';
7              
8 3     3   1523 use Salus::Header;
  3         9  
  3         164  
9 3     3   1428 use Salus::Table;
  3         13  
  3         232  
10              
11             my (%PRO, %META);
12             BEGIN {
13             %PRO = (
14             keyword => sub {
15 3     3   31 no strict 'refs';
  3         5  
  3         549  
16 4         8 my ($caller, $keyword, $cb) = @_;
17 4         5 *{"${caller}::${keyword}"} = $cb;
  4         4137  
18             },
19             clone => sub {
20 28         34 my $obj = shift;
21 28         39 my $ref = ref $obj;
22 28 100       73 return $obj if !$ref;
23 12 100       27 return [ map { $PRO{clone}->($_) } @{$obj} ] if $ref eq 'ARRAY';
  8         16  
  2         5  
24 10 50       25 return { map { $_ => $PRO{clone}->($obj->{$_}) } keys %{$obj} } if $ref eq 'HASH';
  18         59  
  10         19  
25 0         0 return $obj;
26             }
27 3     3   1094 );
28             }
29              
30             sub import {
31 3     3   60 my ($pkg, %import) = @_;
32              
33 3         9 my $caller = caller();
34              
35 3 50       24 if (exists $import{header} ? $import{header} : $import{all}) {
    100          
36 2         5 my ($index, %indexes) = (0, ());
37             $PRO{keyword}($caller, 'header', sub {
38 8     8   377068 my ($name, %options) = @_;
39 8         19 $options{name} = $name;
40 8         10 push @{$META{$caller}{headers}}, \%options;
  8         30  
41 2         12 });
42             }
43              
44 3 50       27 if (exists $import{new} ? $import{new} : $import{all}) {
    100          
45             $PRO{keyword}($caller, 'new', sub {
46 2     2   27 my ($pkg, %options) = @_;
47 2         16 __PACKAGE__->new($META{$caller}, \%options, $pkg);
48 2         8 });
49             }
50             }
51              
52             sub new {
53 2     2 1 7 my ($self, $meta, $options, $caller) = @_;
54            
55 2         11 $meta = $PRO{clone}($meta);
56              
57 2         7 my ($i, @headers, %properties) = (0, (), ());
58              
59             my %indexes = map {
60 8 50       21 $_->{index} ? ($_->{index} => 1) : ()
61 2         6 } @{ $meta->{headers} };
  2         5  
62              
63 2         4 for my $header (@{$meta->{headers}}) {
  2         29  
64 8         4119 while (1) {
65 14 100       43 unless ($indexes{$i}) {
66 8         20 $indexes{$i}++;
67 8         17 $header->{index} = $i;
68 8         13 last;
69             }
70 6         10 ++$i;
71             }
72 8         15 push @headers, Salus::Header->new(%{$header});
  8         66  
73             }
74              
75             return Salus::Table->new(
76 2         38 %{$options},
77             headers => \@headers,
78 2   50     1462 rows => $meta->{rows} || []
79             );
80             }
81              
82             1;
83              
84             __END__;