File Coverage

lib/XML/Loy/GeoRSS.pm
Criterion Covered Total %
statement 77 79 97.4
branch 39 54 72.2
condition 20 29 68.9
subroutine 15 16 93.7
pod 11 11 100.0
total 162 189 85.7


line stmt bran cond sub pod time code
1             package XML::Loy::GeoRSS;
2 1     1   724 use strict;
  1         1  
  1         62  
3 1     1   3 use warnings;
  1         1  
  1         58  
4              
5 1         5 use XML::Loy with => (
6             prefix => 'georss',
7             namespace => 'http://www.georss.org/georss'
8 1     1   4 );
  1         1  
9              
10 1     1   5 use Carp qw/carp/;
  1         1  
  1         89  
11 1     1   5 use Scalar::Util qw/looks_like_number/;
  1         1  
  1         1231  
12              
13             # No constructor
14             sub new {
15 0     0 1 0 carp 'Only use ' . __PACKAGE__ . ' as an extension';
16 0         0 return;
17             };
18              
19              
20             # Add 'point' element
21             sub geo_point {
22 4     4 1 6 my $self = shift;
23              
24             # Get
25 4 100 66     47 if (@_ <= 1) {
    100 66        
26             # Get point object
27 2 50       11 my $point = $self->find('point') or return;
28 2 50 50     54 $point = $point->[ shift // 0 ] or return;
29              
30             # Wrong namespace
31 2 50       13 return if $point->namespace ne __PACKAGE__->_namespace;
32              
33             # Return point
34 2         5 return [ split /\s+/, $point->text ];
35             }
36              
37             # Set
38             elsif (@_ == 2 && looks_like_number($_[0]) && looks_like_number($_[1])) {
39 1         12 return $self->add(point => $_[0] . ' ' . $_[1]);
40             }
41              
42             # Parameterlist has wrong length
43 1         5 return;
44             };
45              
46              
47             # Add 'line' element
48             sub geo_line {
49 14     14 1 14 my $self = shift;
50              
51             # Get
52 14 100       24 if (@_ <= 1) {
53 10 50       25 my $line = $self->find('line') or return;
54 10 50 100     246 $line = $line->[ shift // 0 ] or return;
55              
56             # Wrong namespace
57 10 50       51 return if $line->namespace ne __PACKAGE__->_namespace;
58              
59             # Return line
60 10         14 my @points;
61 10         15 my @v = split /\s+/, $line->text;
62 10   50     305 push @points, [ shift(@v), shift(@v) // 0 ] while @v;
63 10         63 return \@points;
64             };
65              
66             # Parameterlist not even or too small
67 4 100 100     20 return if @_ % 2 || @_ < 4;
68              
69             # Set
70 2         27 return $self->add(line => join(' ',@_) );
71             };
72              
73              
74             # Add 'polygon' element
75             sub geo_polygon {
76 13     13 1 36 my $self = shift;
77              
78             # Get
79 13 100       21 if (@_ <= 1) {
80 8 50       19 my $poly = $self->find('polygon') or return;
81 8 50 50     248 $poly = $poly->[ shift // 0 ] or return;
82              
83             # Wrong namespace
84 8 50       43 return if $poly->namespace ne __PACKAGE__->_namespace;
85              
86             # Return polygon
87 8         52 my @points;
88 8         17 my @v = split /\s+/, $poly->text;
89 8   50     260 push @points, [ shift(@v), shift(@v) // 0 ] while @v;
90 8         53 return \@points;
91             };
92              
93             # Parameterlist not even or too small
94 5 100 100     31 return if @_ % 2 || @_ < 6;
95              
96             # Last pair is not identical to first pair
97 2 100 66     6 if ($_[0] != $_[$#_ - 1] && $_[1] != $_[$#_]) {
98 1         2 push(@_, @_[0..1]);
99             };
100              
101             # Add polygon
102 2         53 return $self->add(polygon => join(' ', @_));
103             };
104              
105              
106             # Add properties
107             sub geo_property {
108 1     1 1 1 my $self = shift;
109              
110 1         4 my %properties = @_;
111              
112             # Add all available properties
113 1         8 foreach my $tag (grep(/^(?:(?:relationship|featuretype)tag|featurename)$/i,
114             keys %properties)) {
115              
116 3         4 my $val = $properties{$tag};
117              
118             # Add as an array, if it is one
119 3 100       6 foreach (ref $val ? @$val : ($val)) {
120 5         15 $self->add( lc($tag) => $_ );
121             };
122             };
123              
124 1         8 return $self;
125             };
126              
127              
128             # Add 'floor' element
129             sub geo_floor {
130 1     1 1 3 shift->add(floor => shift);
131             };
132              
133              
134             # Add 'elev' element
135             sub geo_elev {
136 1     1 1 3 shift->add(elev => shift);
137             };
138              
139              
140             # Add 'radius' element
141             sub geo_radius {
142 1     1 1 4 shift->add(radius => shift);
143             };
144              
145              
146             # Add 'where' element
147             sub geo_where {
148 1     1 1 2 shift->add('where');
149             };
150              
151              
152             # Add 'box' element
153             sub geo_box {
154 8     8 1 10 my $self = shift;
155              
156 8 100       18 if (@_ <= 1) {
157 4 50       11 my $box = $self->find('box') or return;
158 4 50 50     108 $box = $box->[ shift // 0 ] or return;
159              
160             # Wrong namespace
161 4 50       28 return if $box->namespace ne __PACKAGE__->_namespace;
162              
163             # Return box
164 4         6 my @points;
165 4         10 my @v = split /\s+/, $box->text;
166             return [
167 4         136 [$v[0], $v[1]],
168             [$v[2], $v[3]]
169             ];
170             };
171              
172             # Parameterlist has wrong length
173 4 100       17 return unless @_ == 4;
174              
175 1         15 return $self->add(box => join(' ',@_));
176             };
177              
178              
179             # Add 'circle' element
180             sub geo_circle {
181 6     6 1 7 my $self = shift;
182              
183 6 100       13 if (@_ <= 1) {
184 3 50       10 my $circle = $self->find('circle') or return;
185 3 50 50     81 $circle = $circle->[ shift // 0 ] or return;
186              
187             # Wrong namespace
188 3 50       17 return if $circle->namespace ne __PACKAGE__->_namespace;
189              
190             # Return point
191 3         7 my @v = split /\s+/, $circle->text;
192 3         107 return [ [ $v[0], $v[1] ], $v[2] ];
193             };
194              
195             # Parameterlist has wrong length
196 3 100       13 return unless @_ == 3;
197              
198 1         11 return $self->add(circle => join(' ',@_));
199             };
200              
201              
202             1;
203              
204              
205             __END__