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