| 1 | # $Id: /mirror/coderepos/lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM/LibXML.pm 89522 2008-10-28T01:36:10.445014Z daisuke $ | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package MooseX::DOM::LibXML; | ||||||||||||||||||||
| 4 | # I want to use MooseX::Singleton, but it generates some warnings, | ||||||||||||||||||||
| 5 | # so this is a plain moose class with package level global | ||||||||||||||||||||
| 6 | 2 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use MooseX::DOM::LibXML::XPathContext; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | has 'dom_root_attr' => ( | |||||||||||||||||||
| 10 | is => 'rw', | ||||||||||||||||||||
| 11 | isa => 'Moose::Meta::Attribute', | ||||||||||||||||||||
| 12 | lazy => 1, | ||||||||||||||||||||
| 13 | builder => 'build_dom_root_attr' | ||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | my @METHODS = qw(parse_file parse_string dom_findvalue dom_findnodes); | |||||||||||||||||||
| 17 | 2 | foreach my $method (@METHODS) { | |||||||||||||||||||
| 18 | 8 | my $attr = "${method}_method"; | |||||||||||||||||||
| 19 | 8 | my $builder = "build_${method}_method"; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 8 | has $attr=> ( | |||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||
| 23 | isa => 'Moose::Meta::Method', | ||||||||||||||||||||
| 24 | lazy => 1, | ||||||||||||||||||||
| 25 | builder => $builder | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 2 | ▶ | no Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 2 | our $INSTANCE; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | sub instance { | ||||||||||||||||||
|
Subroutine
instance: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 36 | 0 | my $class = shift; | |||||||||||||||||||
| 37 | 0 | ▶ | return $INSTANCE ||= $class->new(); | ||||||||||||||||||
|
Condition0%0/3
$MooseX::DOM::LibXML::INSTANCE ||= $class->new
MC/DC0%0/2
$MooseX::DOM::LibXML::INSTANCE ||= $class->new
$MooseX::DOM::LibXML::INSTANCE
$class->new
Truth table0%0/3
A ||= B
A= $MooseX::DOM::LibXML::INSTANCEB= $class->new
| |||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | sub setup { | ||||||||||||||||||
|
Subroutine
setup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 41 | 0 | my ($self, $meta) = @_; | |||||||||||||||||||
| 42 | 0 | $self->add_attributes($meta); | |||||||||||||||||||
| 43 | 0 | $self->add_methods($meta); | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | ▶ | sub add_attributes { | ||||||||||||||||||
|
Subroutine
add_attributes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 47 | 0 | my ($self, $meta) = @_; | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | $meta->add_attribute( $self->dom_root_attr ); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | sub add_methods { | ||||||||||||||||||
|
Subroutine
add_methods: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 53 | 0 | my ($self, $meta) = @_; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | foreach my $method (@METHODS) { | |||||||||||||||||||
| 56 | 0 | my $attr = "${method}_method"; | |||||||||||||||||||
| 57 | 0 | $meta->add_method($method => $self->$attr); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | ▶ | sub build_dom_root_attr { | ||||||||||||||||||
|
Subroutine
build_dom_root_attr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 62 | 0 | Moose::Meta::Attribute->new( | |||||||||||||||||||
| 63 | dom_root => ( | ||||||||||||||||||||
| 64 | is => 'rw', | ||||||||||||||||||||
| 65 | isa => 'MooseX::DOM::LibXML::XPathContext', | ||||||||||||||||||||
| 66 | coerce => 1, | ||||||||||||||||||||
| 67 | ) | ||||||||||||||||||||
| 68 | ) | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | ▶ | sub build_parse_file_method { | ||||||||||||||||||
|
Subroutine
build_parse_file_method: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||||||||||
| 73 | return Moose::Meta::Method->wrap( | ||||||||||||||||||||
| 74 | package_name => Scalar::Util::blessed $self, | ||||||||||||||||||||
| 75 | name => 'parse_file', | ||||||||||||||||||||
| 76 | 0 | ▶ | body => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 77 | 0 | my ($class, $file) = @_; | |||||||||||||||||||
| 78 | 0 | $class->new(dom_root => XML::LibXML->new->parse_file($file)); | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | 0 | ); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | ▶ | sub build_parse_string_method { | ||||||||||||||||||
|
Subroutine
build_parse_string_method: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 84 | 0 | my $self = shift; | |||||||||||||||||||
| 85 | return Moose::Meta::Method->wrap( | ||||||||||||||||||||
| 86 | package_name => Scalar::Util::blessed $self, | ||||||||||||||||||||
| 87 | name => 'parse_string', | ||||||||||||||||||||
| 88 | 0 | ▶ | body => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 89 | 0 | my ($class, $string) = @_; | |||||||||||||||||||
| 90 | 0 | $class->new(dom_root => XML::LibXML->new->parse_string($string)); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | 0 | ); | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ▶ | sub build_dom_findvalue_method { | ||||||||||||||||||
|
Subroutine
build_dom_findvalue_method: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 96 | 0 | my $self = shift; | |||||||||||||||||||
| 97 | return Moose::Meta::Method->wrap( | ||||||||||||||||||||
| 98 | package_name => Scalar::Util::blessed $self, | ||||||||||||||||||||
| 99 | name => 'dom_findvalue', | ||||||||||||||||||||
| 100 | 0 | ▶ | body => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 101 | 0 | my ($self, $args) = @_; | |||||||||||||||||||
| 102 | 0 | ▶ | if (! ref $args) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | $args = { xpath => $args }; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | 0 | $self->dom_root->findvalue($args->{xpath}); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | 0 | ); | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | ▶ | sub build_dom_findnodes_method { | ||||||||||||||||||
|
Subroutine
build_dom_findnodes_method: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||
| 112 | return Moose::Meta::Method->wrap( | ||||||||||||||||||||
| 113 | package_name => Scalar::Util::blessed $self, | ||||||||||||||||||||
| 114 | name => 'dom_findnodes', | ||||||||||||||||||||
| 115 | 0 | ▶ | body => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 116 | 0 | my ($self, $args) = @_; | |||||||||||||||||||
| 117 | 0 | ▶ | if (! ref $args) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 0 | $args = { xpath => $args }; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | 0 | $self->dom_root->findnodes($args->{xpath}); | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | 0 | ); | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 2 | 1; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||