| 1 | 15 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 15 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package XML::Rabbit::Trait::XPathObjectList; | ||||||||||
| 5 | { | ||||||||||
| 6 | 15 | $XML::Rabbit::Trait::XPathObjectList::VERSION = '0.4.0'; | |||||||||
| 7 | } | ||||||||||
| 8 | 15 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 15 | with 'XML::Rabbit::Trait::XPath'; | |||||||||
| 11 | |||||||||||
| 12 | # ABSTRACT: Multiple XML DOM object xpath extractor trait | ||||||||||
| 13 | |||||||||||
| 14 | around '_process_options' => sub { | ||||||||||
| 15 | 17 | ▶ | my ($orig, $self, $name, $options, @rest) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 16 | |||||||||||
| 17 | 17 | $self->$orig($name, $options, @rest); | |||||||||
| 18 | |||||||||||
| 19 | # This should really be: | ||||||||||
| 20 | # has '+isa' => ( required => 1 ); | ||||||||||
| 21 | # but for some unknown reason Moose doesn't allow that | ||||||||||
| 22 | 17 | ▶ | confess("isa attribute is required") unless defined( $options->{'isa'} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 15 | }; | |||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | has 'isa_map' => ( | ||||||||||
| 27 | is => 'ro', | ||||||||||
| 28 | isa => 'HashRef[Str]', | ||||||||||
| 29 | lazy => 1, | ||||||||||
| 30 | 7 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 15 | ); | |||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | sub _build_default { | ||||||||||
| 35 | 15 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build_default: called CC=1 SCAR=0.0
| |||||||||||
| 36 | return sub { | ||||||||||
| 37 | 15 | ▶ | my ($parent) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 38 | 15 | my $xpath_query = $self->_resolve_xpath_query( $parent ); | |||||||||
| 39 | 15 | $self->_convert_isa_map( $parent ); | |||||||||
| 40 | 15 | my $class = $self->_resolve_class(); | |||||||||
| 41 | 15 | my @nodes; | |||||||||
| 42 | 15 | foreach my $node ( $self->_find_nodes($parent, $xpath_query ) ) { | |||||||||
| 43 | 459 | push @nodes, $self->_create_instance( $parent, $class, $node ); | |||||||||
| 44 | } | ||||||||||
| 45 | 15 | return \@nodes; | |||||||||
| 46 | 15 | }; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 15 | Moose::Util::meta_attribute_alias('XPathObjectList'); | |||||||||
| 50 | |||||||||||
| 51 | 15 | ▶ | no Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | |||||||||||
| 53 | 15 | 1; | |||||||||
| 54 | |||||||||||
| 55 | __END__ | ||||||||||
| 56 | |||||||||||
| 57 | =pod | ||||||||||
| 58 | |||||||||||
| 59 | =encoding utf-8 | ||||||||||
| 60 | |||||||||||
| 61 | =head1 NAME | ||||||||||
| 62 | |||||||||||
| 63 | XML::Rabbit::Trait::XPathObjectList - Multiple XML DOM object xpath extractor trait | ||||||||||
| 64 | |||||||||||
| 65 | =head1 VERSION | ||||||||||
| 66 | |||||||||||
| 67 | version 0.4.0 | ||||||||||
| 68 | |||||||||||
| 69 | =head1 SYNOPSIS | ||||||||||
| 70 | |||||||||||
| 71 | package MyXMLSyntaxNode; | ||||||||||
| 72 | use Moose; | ||||||||||
| 73 | with 'XML::Rabbit::Node'; | ||||||||||
| 74 | |||||||||||
| 75 | has 'persons' => ( | ||||||||||
| 76 | isa => 'ArrayRef[MyXMLSyntax::Person]', | ||||||||||
| 77 | traits => [qw(XPathObjectList)], | ||||||||||
| 78 | xpath_query => './persons/*', | ||||||||||
| 79 | ); | ||||||||||
| 80 | |||||||||||
| 81 | no Moose; | ||||||||||
| 82 | __PACKAGE__->meta->make_immutable(); | ||||||||||
| 83 | |||||||||||
| 84 | 1; | ||||||||||
| 85 | |||||||||||
| 86 | =head1 DESCRIPTION | ||||||||||
| 87 | |||||||||||
| 88 | This module provides the extraction of multiple complex values (subtrees) | ||||||||||
| 89 | from an XML node based on an XPath query. Each subtree is used as input for | ||||||||||
| 90 | the constructor of the class specified in the isa attribute. All of the | ||||||||||
| 91 | extracted objects are then put into an arrayref which is accessible via the | ||||||||||
| 92 | parent attribute. | ||||||||||
| 93 | |||||||||||
| 94 | See L<XML::Rabbit> for a more complete example. | ||||||||||
| 95 | |||||||||||
| 96 | =head1 ATTRIBUTES | ||||||||||
| 97 | |||||||||||
| 98 | =head2 isa_map | ||||||||||
| 99 | |||||||||||
| 100 | Specifies the prefix:tag to class name mapping used with union xpath | ||||||||||
| 101 | queries. See L<XML::Rabbit> for more detailed information. | ||||||||||
| 102 | |||||||||||
| 103 | =head1 METHODS | ||||||||||
| 104 | |||||||||||
| 105 | =head2 _build_default | ||||||||||
| 106 | |||||||||||
| 107 | Returns a coderef that is run to build the default value of the parent attribute. Read Only. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 AUTHOR | ||||||||||
| 110 | |||||||||||
| 111 | Robin Smidsrød <robin@smidsrod.no> | ||||||||||
| 112 | |||||||||||
| 113 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 114 | |||||||||||
| 115 | This software is copyright (c) 2013 by Robin Smidsrød. | ||||||||||
| 116 | |||||||||||
| 117 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 118 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||