blib/lib/Sphinx/XMLpipe2.pm

filter: Statementstmt 80.8% 59 / 73 Branchbran 50.0% 11 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 76.6% 82 / 107 CC 16 SCAR 19.4
CC
16
Cov
74%
_fetch_header
11.4
add_data
9.0
remove_data
3.0
CRAP 7.0
↑ Summary
1 package Sphinx::XMLpipe2;
2  
3 2 ▶use XML::Hash::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = "0.05";
6  
7 4 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
8 4 my ($class, %args) = @_;
9 4 ▶ return undef unless %args;
Branch: 1 branch
BranchTrueFalseTotal
unless %args 1 3 4
10  
11 3 my $self = \%args;
12 3 $self->{'data'} = {};
13  
14 3 $self->{'xmlprocessor'} = XML::Hash::XS->new(
15 xml_decl => 0,
16 indent => 4,
17 use_attr => 1,
18 content => 'content',
19 encoding => 'utf-8',
20 utf8 => 0,
21 );
22  
23 3 bless $self, $class;
24 3 return $self
25 }
26  
27 1 ▶sub fetch {
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
28 1 my ($self, ) = @_;
29  
30 1 my $content1 = $self->_fetch_header();
31 1 my $content2 = $self->_fetch_data();
32 1 return qq~<?xml version="1.0" encoding="utf-8"?>\n<sphinx:docset>$content1$content2</sphinx:docset>~;
33 }
34  
35 2 ▶sub add_data {
Subroutine add_data: called CC=7 SCAR=22.0
Pod documented
36 2 my ($self, $data) = @_;
37 2 ▶ return undef unless exists $data->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{"id"} 1 1 2
38  
39 $self->{'data'}->{'sphinx:document'} = []
40 1 ▶ unless exists $self->{'data'}->{'sphinx:document'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{"sphinx:document"} 1 0 1
41  
42 1 my %params = ();
43 1 my @keys = ();
44  
45 1 ▶ if (ref($self->{'attrs'}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'attrs'} eq 'HASH') { } 1 0 1
elsif (ref $self->{'attrs'} eq 'ARRAY') { } 0 0 0
46 1 @keys = (@{$self->{'fields'}}, keys %{$self->{'attrs'}});
47 1 map { $params{$_} = [$data->{$_}] } @keys;
48 }
49 elsif (ref($self->{'attrs'}) eq 'ARRAY') {
50 0 my @def = ();
51 0 for my $definition (@{$self->{'attrs'}}) {
52 0 push @def, $definition->{'name'};
53 }
54 0 @keys = (@{$self->{'fields'}}, @def);
55 }
56 1 ▶ map { $params{$_} = [$data->{$_}] if exists $data->{$_}; } @keys;
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$_} 4 0 4
57  
58 1 push @{$self->{'data'}->{'sphinx:document'}}, {
59 1 id => $data->{'id'},
60 %params
61 };
62 1 return $self;
63 }
64  
65 2 ▶sub remove_data {
Subroutine remove_data: called CC=3 SCAR=11.0
Pod documented
66 2 my ($self, $data) = @_;
67 2 ▶ return undef unless exists $data->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{"id"} 1 1 2
68  
69 $self->{'data'}->{'sphinx:killlist'} = {'id' => []}
70 1 ▶ unless exists $self->{'data'}->{'sphinx:killlist'}->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{"sphinx:killlist"}{"id"} 1 0 1
71  
72 1 push @{$self->{'data'}->{'sphinx:killlist'}->{'id'}}, [$data->{'id'}];
73 1 return $self;
74 }
75  
76  
77 1 ▶sub _fetch_header {
Subroutine _fetch_header: called CC=7 SCAR=24.3
78 1 my ($self, ) = @_;
79 1 my $header = {
80 'sphinx:schema' => {
81 'sphinx:field' => [],
82 'sphinx:attr' => [],
83 }
84 };
85  
86 1 for my $field (@{$self->{'fields'}}) {
87 3 push @{$header->{'sphinx:schema'}->{'sphinx:field'}}, {'name' => $field};
88 }
89  
90 1 ▶ if (ref($self->{'attrs'}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'attrs'} eq 'HASH') { } 1 0 1
elsif (ref $self->{'attrs'} eq 'ARRAY') { } 0 0 0
91 1 for my $attr (keys %{$self->{'attrs'}}) {
92 1 push @{$header->{'sphinx:schema'}->{'sphinx:attr'}}, {'name' => $attr, 'type' => $self->{'attrs'}->{$attr}};
93 }
94 }
95 elsif (ref($self->{'attrs'}) eq 'ARRAY') {
96 0 for my $definition (@{$self->{'attrs'}}) {
97 0 my $node = {};
98 0 ▶ map {$node->{$_} = $definition->{$_} if exists $definition->{$_};} qw(name type bits default);
Branch: 1 branch
BranchTrueFalseTotal
if exists $definition->{$_} 0 0 0
99 0 push @{$header->{'sphinx:schema'}->{'sphinx:attr'}}, $node;
100 }
101 }
102 0 @{$header->{'sphinx:schema'}->{'sphinx:attr'}} = sort {$a->{name} cmp $b->{name}} @{$header->{'sphinx:schema'}->{'sphinx:attr'}};
103  
104 1 return _pruning_xml($self->{'xmlprocessor'}->hash2xml($header));
105 }
106  
107 1 ▶sub _fetch_data {
Subroutine _fetch_data: called CC=1 SCAR=0.0
108 1 my ($self, ) = @_;
109 1 return _pruning_xml($self->{'xmlprocessor'}->hash2xml($self->{'data'}));
110 }
111  
112 2 ▶sub _pruning_xml {
Subroutine _pruning_xml: called CC=1 SCAR=0.0
113 2 my ($xml) = @_;
114 2 $xml =~ s/^\s*<root>//is;
115 2 $xml =~ s/<\/root>\s*$//is;
116 2 return $xml;
117 }
118  
119 2 1;
120 __END__
↑ Summary