| 1 | # $Id: /mirror/coderepos/lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM.pm 89522 2008-10-28T01:36:10.445014Z daisuke $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package MooseX::DOM; | ||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use MooseX::DOM::Meta::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our $AUTHORITY = 'cpan:DMAKI'; | |||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.00999'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=13.0
| |||||||||||||||||||||||||||||
| 13 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 14 | 2 | my $caller = caller(); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | my $metaclass = 'MooseX::DOM::Meta::LibXML'; | |||||||||||||||||||||||||||
| 17 | 2 | Class::MOP::load_class($metaclass); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | my $meta = Moose::Util::MetaRole::apply_metaclass_roles( | |||||||||||||||||||||||||||
| 20 | for_class => $caller, | ||||||||||||||||||||||||||||
| 21 | metaclass_roles => [ $metaclass ] | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if (grep { $_ eq 'BUILDARGS' } @_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | $meta->add_method('BUILDARGS' => Moose::Meta::Method->wrap( | ||||||||||||||||||||||||||||
| 26 | package_name => $caller, | ||||||||||||||||||||||||||||
| 27 | name => 'BUILDARGS', | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | body => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 29 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (@_ == 1 && ref $_[0] ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 1 and ref $_[0] ne "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] ne "HASH"
@_ == 1
ref $_[0] ne "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] ne "HASH"
| |||||||||||||||||||||||||||||
| 31 | 0 | unshift @_, 'dom_root'; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 0 | return {@_}; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 0 | )); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | $class->export_keywords($caller); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub unimport { | ||||||||||||||||||||||||||
|
Subroutine
unimport: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 43 | 0 | my $caller = caller(); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | $class->unexport_keywords($caller); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub export_keywords { | ||||||||||||||||||||||||||
|
Subroutine
export_keywords: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | my ($class, $caller) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my $exporter = Sub::Exporter::build_exporter({ | ||||||||||||||||||||||||||||
| 52 | into => $caller, | ||||||||||||||||||||||||||||
| 53 | groups => { default => [ ':all' ] }, | ||||||||||||||||||||||||||||
| 54 | exports => [ | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | dom_nodes => sub { $class->build_dom_nodes($caller) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | dom_fetchnodes => sub { $class->build_dom_fetchnodes($caller) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | dom_to_class => sub { $class->build_dom_to_class($caller) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | dom_value => sub { $class->build_dom_value($caller) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 0 | ] | |||||||||||||||||||||||||||
| 60 | }); | ||||||||||||||||||||||||||||
| 61 | 0 | $exporter->($class); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub unexport_keywords { | ||||||||||||||||||||||||||
|
Subroutine
unexport_keywords: not called CC=3 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | my ($class, $caller) = @_; | |||||||||||||||||||||||||||
| 66 | 0 | my @keywords = qw(dom_nodes dom_fetchnodes dom_to_class dom_value); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | { no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 69 | 0 | foreach my $name (@keywords) { | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ( defined &{ $caller . '::' . $name }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | delete ${ $caller . '::' }{$name}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub build_dom_value { | ||||||||||||||||||||||||||
|
Subroutine
build_dom_value: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | my ($class, $caller) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 81 | 0 | my $name = shift; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $args = { @_ == 1 ? (fetch => {xpath => $_[0]}) : @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | my $fetch = $args->{fetch}; | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $fetch_xpath = $fetch->{xpath} || $name; | ||||||||||||||||||||||||||
|
Condition0%0/3
$fetch->{'xpath'} || $name
MC/DC0%0/2
$fetch->{'xpath'} || $name
$fetch->{'xpath'}
$name
Truth table0%0/3
A || B
A= $fetch->{'xpath'}B= $name
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | Carp::confess "name must not contain special characters '$name'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | if $name !~ /^[\w_]+$/; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | my $meta = $caller->meta; | |||||||||||||||||||||||||||
| 91 | $meta->add_method( | ||||||||||||||||||||||||||||
| 92 | $name, | ||||||||||||||||||||||||||||
| 93 | Moose::Meta::Method->wrap( | ||||||||||||||||||||||||||||
| 94 | package_name => $caller, | ||||||||||||||||||||||||||||
| 95 | name => $name, | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | body => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 97 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | 0 | $self->dom_root->findvalue($fetch_xpath); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | ) | ||||||||||||||||||||||||||||
| 101 | 0 | ); | |||||||||||||||||||||||||||
| 102 | 0 | }; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub build_dom_nodes { | ||||||||||||||||||||||||||
|
Subroutine
build_dom_nodes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 0 | my ($class, $caller) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 109 | 0 | my $name = shift; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $args = { @_ == 1 ? (fetch => $_[0]) : @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | Carp::confess "name must not contain special characters '$name'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | if $name !~ /^[\w_]+$/; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | $args->{into} = $caller; | |||||||||||||||||||||||||||
| 115 | 0 | my @methods = ( | |||||||||||||||||||||||||||
| 116 | $class->build_dom_nodes_accessor($name, $args), | ||||||||||||||||||||||||||||
| 117 | $class->build_dom_nodes_appender($name, $args), | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $meta = $caller->meta; | |||||||||||||||||||||||||||
| 121 | 0 | foreach my $method (@methods) { | |||||||||||||||||||||||||||
| 122 | 0 | $meta->add_method($method->{name}, $method->{code}); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | } | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub build_dom_nodes_appender { | ||||||||||||||||||||||||||
|
Subroutine
build_dom_nodes_appender: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | my ($class, $name, $args) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # I can't figure out this one automatically (I think). | ||||||||||||||||||||||||||||
| 131 | # just expect a code, and if I can't find it, not methods are | ||||||||||||||||||||||||||||
| 132 | # returned to the callee | ||||||||||||||||||||||||||||
| 133 | my $config = ref $args->{append} eq 'HASH' ? $args->{append} : | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | { code => $args->{append} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $method = $config->{name} || "add_$name"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$config->{'name'} || "add_$name"
MC/DC0%0/1
$config->{'name'} || "add_$name"
$config->{'name'}
Truth table0%0/2
A
A= $config->{'name'}
| |||||||||||||||||||||||||||||
| 136 | 0 | my $code = $config->{code}; | |||||||||||||||||||||||||||
| 137 | 0 | my $ret; | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($code) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | $ret = { | ||||||||||||||||||||||||||||
| 140 | $method, | ||||||||||||||||||||||||||||
| 141 | Moose::Meta::Method->wrap( | ||||||||||||||||||||||||||||
| 142 | package_name => $args->{into}, | ||||||||||||||||||||||||||||
| 143 | 0 | name => $method, | |||||||||||||||||||||||||||
| 144 | body => $code | ||||||||||||||||||||||||||||
| 145 | ) | ||||||||||||||||||||||||||||
| 146 | }; | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | return $ret ? $ret : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub build_dom_nodes_accessor { | ||||||||||||||||||||||||||
|
Subroutine
build_dom_nodes_accessor: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 0 | my ($class, $name, $args) = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my $fetch = $args->{fetch} || $name; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{'fetch'} || $name
MC/DC0%0/2
$args->{'fetch'} || $name
$args->{'fetch'}
$name
Truth table0%0/3
A || B
A= $args->{'fetch'}B= $name
| |||||||||||||||||||||||||||||
| 155 | 0 | my $store = $args->{store}; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if (! ref $fetch) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | my $xpath = $fetch; | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | $fetch = sub { shift->dom_root->findnodes($xpath) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | my $code = <<" EOSUB"; | |||||||||||||||||||||||||||
| 163 | sub { | ||||||||||||||||||||||||||||
| 164 | my \$self = shift; | ||||||||||||||||||||||||||||
| 165 | my \@ret = \$fetch->(\$self); | ||||||||||||||||||||||||||||
| 166 | EOSUB | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if ($store) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | $code .= <<" EOSUB"; | |||||||||||||||||||||||||||
| 169 | if (\@_) { | ||||||||||||||||||||||||||||
| 170 | \$store->(\$self, \@_); | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | EOSUB | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | $code .= <<" EOSUB"; | |||||||||||||||||||||||||||
| 176 | return \@ret; | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | EOSUB | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $cv = eval $code; Carp::confess($@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | return { | ||||||||||||||||||||||||||||
| 182 | name => $name, | ||||||||||||||||||||||||||||
| 183 | code => Moose::Meta::Method->wrap( | ||||||||||||||||||||||||||||
| 184 | package_name => $args->{into}, | ||||||||||||||||||||||||||||
| 185 | 0 | name => $name, | |||||||||||||||||||||||||||
| 186 | body => $cv, | ||||||||||||||||||||||||||||
| 187 | ) | ||||||||||||||||||||||||||||
| 188 | }; | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub build_dom_fetchnodes { | ||||||||||||||||||||||||||
|
Subroutine
build_dom_fetchnodes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 0 | my ($class, $caller) = @_; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | my $args = {@_ == 1 ? (xpath => $_[0]) : @_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | my $filter = $args->{filter}; | |||||||||||||||||||||||||||
| 197 | 0 | my $xpath = $args->{xpath}; | |||||||||||||||||||||||||||
| 198 | return ($filter ? | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 200 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 201 | 0 | return $filter->($self->dom_root->findnodes($xpath)); | |||||||||||||||||||||||||||
| 202 | } : | ||||||||||||||||||||||||||||
| 203 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 204 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 205 | 0 | return $self->dom_root->findnodes($xpath); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 0 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | }; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub build_dom_to_class { | ||||||||||||||||||||||||||
|
Subroutine
build_dom_to_class: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 0 | my ($class, $caller) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 215 | 0 | ▶ | my $args = {@_ == 1 ? (to_class => $_[0]) : @_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | my $to_class = $args->{to_class}; | |||||||||||||||||||||||||||
| 217 | 0 | Class::MOP::load_class($to_class); | |||||||||||||||||||||||||||
| 218 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 219 | 0 | map { $to_class->new($_) } @_; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 0 | } | |||||||||||||||||||||||||||
| 222 | 0 | } | |||||||||||||||||||||||||||
| 223 | 2 | 1; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | __END__ | ||||||||||||||||||||||||||||