Detergent/Config.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 154 CC 4 SCAR 14.4
CC
4
Cov
0%
add_match
12.0
stylesheet
6.0
stylesheets
2.0
CRAP 4.2
1 package HTML::Detergent::Config;
2  
3 =head1 NAME
4  
5 HTML::Detergent::Config - Configuration object for HTML::Detergent
6  
7 =cut
8  
9 use strict;
10 use warnings FATAL => 'all';
11  
12 use Moose;
13 use namespace::autoclean;
14  
15 use MooseX::Types -declare => [qw(PathMap PathList Metadata ICB Config)];
16  
17 use MooseX::Types::Moose qw(Undef Str ArrayRef HashRef);
18  
19 use XML::LibXML;
20  
21 our $VERSION = '0.02';
22  
23 =head1 METHODS
24  
25 =head2 new
26  
27 =over 4
28  
29 =item match
30  
31 This is an ARRAY reference of XPath expressions to try against the
32 document, in order of preference. Entries optionally may be
33 two-element ARRAY references themselves, the second element being a
34 URL where an XSLT stylesheet may be found.
35  
36 match => [ '/some/xpath/expression',
37 [ '/other/expr', '/url/of/transform.xsl' ],
38 ],
39  
40 =cut
41  
42 subtype PathMap, as HashRef[Str|Undef];
43 coerce PathMap, from ArrayRef, via {
44 my $x = { map { ref $_ ? @{$_}[0,1] : ($_ => undef) } @{$_[0]} };
45 $x;
46 };
47  
48 has _match_map => (
49 is => 'rw',
50 isa => PathMap,
51 lazy => 1,
52 # required => 1,
53 traits => ['Hash'],
54 default => sub { { } },
55 coerce => 1,
56 init_arg => 'match',
57 handles => {
58 # this whines when the key is undef
59 # stylesheet => 'get',
60 # wait, the value can be undef too
61 # stylesheets => 'values',
62 },
63 );
64  
65 =item stylesheet
66  
67 =cut
68  
69 sub stylesheet {
70 my ($self, $xpath) = @_;
71 return unless defined $xpath;
72 $self->_match_map->{$xpath};
73 }
74  
75 =item stylesheets
76  
77 =cut
78  
79 sub stylesheets {
80 my $self = shift;
81 grep { defined $_ } values %{$self->_match_map};
82 }
83  
84 =item add_match
85  
86 =cut
87  
88 sub add_match {
89 my ($self, $xpath, $xslt) = @_;
90 my ($map, $seq) = ($self->_match_map, $self->_match_sequence);
91 if (exists $map->{$xpath}) {
92 $map->{$xpath} = $xslt if defined $xslt;
93 }
94 else {
95 $map->{$xpath} = $xslt;
96 push @$seq, $xpath;
97 }
98 1;
99 }
100  
101 subtype PathList, as ArrayRef[Str];
102 coerce PathList, from ArrayRef[Str|ArrayRef],
103 via { [ map { ref $_ ? @{$_}[0] : $_ } @{$_[0]} ] };
104 coerce PathList, from HashRef,
105 via { [ keys %{$_[0]} ] };
106  
107 has _match_sequence => (
108 is => 'rw',
109 isa => PathList,
110 lazy => 1,
111 traits => ['Array'],
112 default => sub { [ ] },
113 coerce => 1,
114 handles => {
115 matches => 'elements',
116 },
117 );
118  
119 =item link
120  
121 This is a HASH reference where the keys correspond to C<rel>
122 attributes and the values to C<href> attributes of C<E<lt>linkE<gt>>
123 elements. If the values are ARRAY references, they will be processed
124 in document order. C<rel> attributes will be sorted lexically. If a
125 callback is supplied instead, the caller expects a result of the same
126 form.
127  
128 link => { rel1 => 'href1', rel2 => [ qw(href2 href3) ] },
129  
130 # or
131  
132 link => \&_link_cb,
133  
134 =cut
135  
136 subtype Metadata, as HashRef[ArrayRef[Str]];
137 coerce Metadata, from HashRef, via {
138 #require Data::Dumper;
139 #warn Data::Dumper::Dumper(\@_);
140  
141 # for some reason this needed an explicit return. go figure.
142 return {
143 map { $_ => (ref $_[0]{$_} eq 'ARRAY' ? $_[0]{$_} : [$_[0]{$_}] ) }
144 keys %{$_[0]}
145 };
146 };
147  
148 has links => (
149 is => 'rw',
150 isa => Metadata,
151 default => sub { {} },
152 traits => ['Hash'],
153 lazy => 1,
154 coerce => 1,
155 init_arg => 'link',
156 );
157  
158 =item add_link
159  
160 =cut
161  
162 sub add_link {
163 my ($self, $rel, $href) = @_;
164 my $links = $self->links;
165 push @{$links->{$rel} ||= []}, $href;
166 }
167  
168 =item meta
169  
170 This is a HASH reference where the keys correspond to C<name>
171 attributes and the values to C<content> attributes of
172 C<E<lt>metaE<gt>> elements. If the values are ARRAY references, they
173 will be processed in document order. C<name> attributes will be sorted
174 lexically. If a callback is supplied instead, the caller expects a
175 result of the same form.
176  
177 meta => { name1 => 'content1',
178 name2 => [ qw(content2 content3) ] },
179  
180 # or
181  
182 meta => \&_meta_cb,
183  
184 =cut
185  
186 has metadata => (
187 is => 'rw',
188 isa => Metadata,
189 default => sub { {} },
190 traits => ['Hash'],
191 lazy => 1,
192 coerce => 1,
193 init_arg => 'meta',
194 );
195  
196 =item add_meta
197  
198 =cut
199  
200 sub add_meta {
201 my ($self, $name, $content) = @_;
202 my $meta = $self->metadata;
203 push @{$meta->{$name} ||= []}, $content;
204 }
205  
206 =item callback
207  
208 These callbacks will be passed into the internal L<XML::LibXSLT>
209 processor. See L<XML::LibXML::InputCallback> for details.
210  
211 callback => [ \&_match_cb, \&_open_cb, \&_read_cb, \&_close_cb ],
212  
213 # or
214  
215 callback => $icb, # isa XML::LibXML::InputCallback
216  
217 =back
218  
219 =cut
220  
221 class_type ICB, { class => 'XML::LibXML::InputCallback' };
222 coerce ICB, from ArrayRef, via {
223 my $x = XML::LibXML::InputCallback->new;
224 $x->register_callbacks(shift);
225 $x;
226 };
227  
228 has callback => (
229 is => 'rw',
230 isa => ICB,
231 lazy => 1,
232 default => sub { XML::LibXML::InputCallback->new },
233 );
234  
235 class_type Config, { class => __PACKAGE__ };
236 coerce Config, from HashRef,
237 via { HTML::Detergent::Config->new(shift) };
238  
239 around BUILDARGS => sub {
240 my $orig = shift;
241 my $class = shift;
242  
243 my %p = ref $_[0] ? %{$_[0]} : @_;
244 $p{_match_sequence} = $p{match} || [];
245  
246 $class->$orig(%p);
247 };
248  
249 =head2 stylesheets
250  
251 List all stylesheets associated with an XPath expression.
252  
253 =head2 stylesheet
254  
255 Retrieve a stylesheet for a given XPath expression.
256  
257 =cut
258  
259 =head2 merge
260  
261 =cut
262  
263 sub merge {
264 my ($self, $left, $right) = @_;
265 # my ($
266  
267 # $class->new(\%
268  
269 # just return myself for now
270 $left;
271 }
272  
273 __PACKAGE__->meta->make_immutable;
274 no Moose;
275  
276 1;