File Coverage

blib/lib/XML/NewsML_G2/Scheme_Manager.pm
Criterion Covered Total %
statement 35 35 100.0
branch 12 14 85.7
condition 5 6 83.3
subroutine 9 9 100.0
pod 5 5 100.0
total 66 69 95.6


line stmt bran cond sub pod time code
1             package XML::NewsML_G2::Scheme_Manager;
2              
3 18     18   125 use Moose;
  18         42  
  18         116  
4 18     18   111231 use Carp;
  18         45  
  18         1250  
5 18     18   116 use namespace::autoclean;
  18         42  
  18         130  
6              
7             my @attrs = (
8             qw(desk hltype role ind geo org topic crel crol drol svc
9             isbn ean isrol nprov cinat ninat stat sig iso3166_1a2 genre isin medtop
10             rnd vidrnd colsp adc group pgrmod copyright_holder electiondistrict
11             electionprovince facet sportfacet sportfacetvalue storytype gyibt eventid ncostat)
12             );
13              
14             foreach (@attrs) {
15             has $_, isa => 'XML::NewsML_G2::Scheme', is => 'rw';
16             }
17              
18             # public methods
19              
20             sub get_all_schemes {
21 72     72 1 249 my $self = shift;
22              
23 2808         4148 return grep {defined}
24 72         403 map { $self->$_() } sort $self->meta->get_attribute_list();
  2808         79146  
25             }
26              
27             sub build_qcode {
28 2127     2127 1 3861 my ( $self, $name, $value ) = @_;
29 2127 100       4135 return unless $value;
30              
31 2071 50       7313 my $getter = $self->can($name)
32             or croak "No schema named '$name' for value $value!";
33 2071         56458 my $scheme = $getter->($self);
34 2071 100 66     45583 return unless ( $scheme and ( $scheme->uri or $scheme->catalog ) );
      100        
35              
36 1821         43631 return $scheme->alias . ':' . $value;
37             }
38              
39             sub add_qcode_or_literal {
40 1200     1200 1 2673 my ( $self, $elem, $name, $value ) = @_;
41 1200 100       2578 $self->_add_qcode( $elem, $name, $value )
42             or $elem->setAttribute( 'literal', $name . '#' . $value );
43 1200         4471 return 1;
44             }
45              
46             sub add_qcode {
47 393     393 1 959 my ( $self, $elem, $name, $value ) = @_;
48 393 50       896 $self->_add_qcode( $elem, $name, $value )
49             or die "Specifying a '$name' schema with uri or catalog required\n";
50 393         1041 return 1;
51             }
52              
53             sub add_role {
54 350     350 1 930 my ( $self, $elem, $name, $value ) = @_;
55              
56 350         803 my $role = $self->build_qcode( $name, $value );
57 350 100       885 return unless $role;
58              
59 298         1031 $elem->setAttribute( 'role', $role );
60 298         3286 return 1;
61             }
62              
63             # private methods
64              
65             sub _add_qcode {
66 1593     1593   2970 my ( $self, $elem, $name, $value ) = @_;
67              
68 1593         3069 my $qcode = $self->build_qcode( $name, $value );
69 1593 100       4040 return unless $qcode;
70              
71 1395         4562 $elem->setAttribute( 'qcode', $qcode );
72 1395         15049 return 1;
73             }
74              
75             __PACKAGE__->meta->make_immutable;
76              
77             1;
78              
79             __END__
80              
81             =head1 NAME
82              
83             XML::NewsML_G2::Scheme_Manager - hold all L<XML::NewsML_G2::Scheme> instances
84              
85              
86             =for test_synopsis
87             my ($s1, $s2, $s3);
88              
89             =head1 SYNOPSIS
90              
91             my $sm = XML::NewsML_G2::Scheme_Manager->new(desk => $s1, hltype => $s2, svc => $s3);
92              
93             =head1 ATTRIBUTES
94              
95             =over 4
96              
97             =item crel
98              
99             Scheme for company relations
100              
101             =item desk
102              
103             Scheme for editorial desk
104              
105             =item ean
106              
107             Scheme for european/international article number
108              
109             =item geo
110              
111             Scheme for location information
112              
113             =item hltype
114              
115             Scheme for type of headline
116              
117             =item ind
118              
119             Scheme for content indicators
120              
121             =item isbn
122              
123             Scheme for international standard book number
124              
125             =item org
126              
127             Scheme for organisations
128              
129             =item role
130              
131             Scheme for editorial note roles
132              
133             =item svc
134              
135             Scheme for editorial service
136              
137             =item topic
138              
139             Scheme for topics
140              
141             =item isrol
142              
143             Scheme for info source role
144              
145             =item nprov
146              
147             Scheme for news provder
148              
149             =item ninat
150              
151             Scheme for news item nature
152              
153             =item stat
154              
155             Scheme for document state
156              
157             =item sig
158              
159             Scheme for signals
160              
161             =item iso3166_1a2
162              
163             Scheme for country codes
164              
165             =item genre
166              
167             Scheme for genres
168              
169             =item isin
170              
171             Scheme for ISIN codes
172              
173             =item medtop
174              
175             Scheme for media topics
176              
177             =item rnd
178              
179             Scheme for renditions
180              
181             =item colsp
182              
183             Scheme for colorspaces
184              
185             =item adc
186              
187             Scheme for audio channels
188              
189             =item group
190              
191             Scheme for groups within a package
192              
193             =item pgrmod
194              
195             Scheme for package group mode
196              
197             =item copyright_holder
198              
199             Scheme for copyright holder
200              
201             =item electiondistrict
202              
203             Scheme for election districts
204              
205             =item electionprovince
206              
207             Scheme for election provinces
208              
209             =item facet
210              
211             Scheme for facets
212              
213             =item sportfacet
214              
215             Scheme for sport facets
216              
217             =item sportfacetvalue
218              
219             Scheme for sport facet values
220              
221             =item storytype
222              
223             Scheme for storytypes
224              
225             =item gyibt
226              
227             Scheme for creator kinds
228              
229             =item eventid
230              
231             Scheme for events
232              
233             =back
234              
235             =head1 METHODS
236              
237             =over 4
238              
239             =item get_all_schemes
240              
241             Returns a list of all registered L<XML::NewsML_G2::Scheme> instances
242              
243             =item build_qcode
244              
245             Build a qcode of the given scheme
246              
247             $scheme_manager->build_qcode('ninat', 'text');
248              
249             If the schema does not provide a catalog or URI, creating a qcode is
250             not possible, and this method will return undef.
251              
252             =item add_qcode
253              
254             Add a qcode attribute of the given scheme to the XML element:
255              
256             $scheme_manager->add_qcode($element, 'ninat', 'text');
257              
258             If the schema does not provide a catalog or URI, creating a qcode is
259             not possible, and this method will die.
260              
261             =item add_qcode_or_literal
262              
263             Same as C<add_qcode>, but will create a C<literal> attribute if
264             creating a qcode is not possible.
265              
266             =item add_role
267              
268             If the scheme is defined, add a role attribute to the given XML
269             element. Else, do nothing.
270              
271             $scheme_manager->add_role($element, 'isrol', 'originfo');
272              
273             =back
274              
275             =head1 AUTHOR
276              
277             Philipp Gortan C<< <philipp.gortan@apa.at> >>
278              
279             =head1 LICENCE AND COPYRIGHT
280              
281             Copyright (c) 2013-2014, APA-IT. All rights reserved.
282              
283             See L<XML::NewsML_G2> for the license.