File Coverage

blib/lib/App/Mowyw/Datasource/XML.pm
Criterion Covered Total %
statement 36 38 94.7
branch 9 12 75.0
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 53 59 89.8


line stmt bran cond sub pod time code
1             package App::Mowyw::Datasource::XML;
2              
3 2     2   4255 use strict;
  2         5  
  2         93  
4 2     2   12 use warnings;
  2         5  
  2         76  
5 2     2   14 use base 'App::Mowyw::Datasource::Array';
  2         4  
  2         1384  
6 2     2   12 use XML::Simple;
  2         4  
  2         26  
7 2     2   139 use Scalar::Util qw(reftype);
  2         4  
  2         94  
8              
9 2     2   11 use Carp qw(confess);
  2         4  
  2         726  
10              
11             sub new {
12 5     5 0 30 my ($class, $opts) = @_;
13 5         26 my $self = bless { OPTIONS => $opts, INDEX => 0 }, $class;
14              
15 5 50       22 my $file = $opts->{file} or confess "Mandatory option 'file' is missing\n";
16 5         14 $opts->{source} = $self->_read_data($file);
17             # print Dumper $opts;
18 4         53 $self = $self->SUPER::new($opts);
19              
20 4         90 return $self;
21             }
22              
23             sub _read_data {
24 5     5   9 my ($self, $file) = @_;
25 5         8 my $data;
26 5 100       34 if (exists $self->{OPTIONS}{root}){
27 2         15 $data = XML::Simple->new->parse_file($file, ForceArray => [ $self->{OPTIONS}{root} ]);
28             } else {
29 3         25 $data = XML::Simple->new->parse_file($file);
30             }
31 5 50       247115 if (reftype $data eq 'ARRAY'){
32 0         0 return $data;
33             } else {
34 5 100       25 if (exists $self->{OPTIONS}{root}){
35 2         14 return $data->{$self->{OPTIONS}{root}};
36             } else {
37 3         10 my @keys = keys %$data;
38 3 100       12 if (@keys > 1){
    50          
39 1         192 confess "More than one root item, and no 'root' option specified";
40             } elsif (@keys == 0){
41 0         0 return [];
42             } else {
43 2         12 return $data->{$keys[0]};
44             }
45             }
46             }
47             }
48              
49             1;