| 1 | package Net::Gnip::BaseStream; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use base qw(Net::Gnip::Base); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | Net::Gnip::BaseStream - represent a list of Gnip objects | ||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | =head1 SYNOPIS | ||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | # Create a new stream | ||||||||||||||||||||||||||||||||||||||
| 14 | my $stream = Net::Gnip::BaseStream->new(); | ||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | # ... or parse from XML | ||||||||||||||||||||||||||||||||||||||
| 17 | my $stream = Net::Gnip::BaseStream->parse($xml); | ||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | # assume that the subclass of BaseStream | ||||||||||||||||||||||||||||||||||||||
| 20 | # has children named foo | ||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | # set the foos | ||||||||||||||||||||||||||||||||||||||
| 23 | $stream->foos(@foos); | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | # get the filters | ||||||||||||||||||||||||||||||||||||||
| 26 | my @foos = $stream->foos; | ||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | # or use an iterator | ||||||||||||||||||||||||||||||||||||||
| 29 | while (my $foo = $stream->next) { | ||||||||||||||||||||||||||||||||||||||
| 30 | print $foo->name; | ||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | $stream->reset; | ||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | # ... now you can use it again | ||||||||||||||||||||||||||||||||||||||
| 36 | while (my $foo = $stream->foo) { | ||||||||||||||||||||||||||||||||||||||
| 37 | print $foo->name; | ||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||
| 41 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | =head2 new | ||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||
| 47 | Create a new, empty stream | ||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 52 | 9 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 53 | 9 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||
| 54 | 9 | ▶ | return bless {%opts}, ref($class) || $class; | ||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | =head2 children [child[ren]] | ||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | Get or set the children | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | 28 | ▶ | sub children { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
children: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 67 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 68 | 28 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 69 | 5 | $self->{children} = [@_]; | |||||||||||||||||||||||||||||||||||||
| 70 | 5 | $self->reset; | |||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||
| 72 | 28 | ▶ | return @{$self->{children}||[]}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'children'} || []
MC/DC100%1/1
$self->{'children'} || []
$self->{'children'}
Truth table100%2/2
A
A= $self->{'children'}
| |||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | =head2 parse <xml> | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | Takes a string of XML, parses it and returns a new, | ||||||||||||||||||||||||||||||||||||||
| 78 | potentially populated FilterStream | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 83 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 84 | 4 | my $xml = shift; | |||||||||||||||||||||||||||||||||||||
| 85 | 4 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||
| 86 | 4 | ▶ | my $no_dt = (ref($class) && $class->{_no_dt}) || $opts{_no_dt}; | ||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
ref $class && $class->{'_no_dt'} || $opts{'_no_dt'}
ref $class and $class->{"_no_dt"}
MC/DC33%1/3
ref $class && $class->{'_no_dt'} || $opts{'_no_dt'}
ref $class
$class->{"_no_dt"}
$opts{'_no_dt'}
Truth table40%2/5
A and B || C
A= ref $classB= $class->{"_no_dt"}C= $opts{'_no_dt'}
| |||||||||||||||||||||||||||||||||||||||
| 87 | 4 | my $parser = $class->parser; | |||||||||||||||||||||||||||||||||||||
| 88 | 4 | my $doc = $parser->parse_string($xml); | |||||||||||||||||||||||||||||||||||||
| 89 | 4 | my $elem = $doc->documentElement(); | |||||||||||||||||||||||||||||||||||||
| 90 | 4 | my @children; | |||||||||||||||||||||||||||||||||||||
| 91 | 4 | my $name = $class->_child_name; | |||||||||||||||||||||||||||||||||||||
| 92 | 4 | my $cclass = "Net::Gnip::".ucfirst($name); | |||||||||||||||||||||||||||||||||||||
| 93 | 4 | my %args; | |||||||||||||||||||||||||||||||||||||
| 94 | 4 | foreach my $attr ($elem->attributes()) { | |||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $name = $attr->name; | |||||||||||||||||||||||||||||||||||||
| 96 | 0 | $args{$name} = $attr->value; | |||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||
| 98 | 4 | foreach my $child ($elem->getChildrenByTagName($name)) { | |||||||||||||||||||||||||||||||||||||
| 99 | 4 | push @{$args{children}}, $cclass->_from_element($child, _no_dt => $no_dt); | |||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||
| 102 | 4 | return $class->new( %args ); | |||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | =head2 next | ||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||
| 108 | Returns the next Child object | ||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | 12 | ▶ | sub next { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 113 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 114 | 12 | return $self->{children}->[$self->{_iter}++]; | |||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | =head2 reset | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | Resets the iterator | ||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | 6 | ▶ | sub reset { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 124 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 125 | 6 | $self->{_iter} = 0; | |||||||||||||||||||||||||||||||||||||
| 126 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | =head2 as_xml | ||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | Return this stream as xml | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | 4 | ▶ | sub as_xml { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
as_xml: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 137 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 138 | 4 | my $as_element = shift; | |||||||||||||||||||||||||||||||||||||
| 139 | 4 | my $name = $self->_child_name; | |||||||||||||||||||||||||||||||||||||
| 140 | 4 | my $elem_name = $self->_elem_name; | |||||||||||||||||||||||||||||||||||||
| 141 | 4 | my $element = XML::LibXML::Element->new($elem_name); | |||||||||||||||||||||||||||||||||||||
| 142 | 4 | foreach my $child (@{delete $self->{children}}) { | |||||||||||||||||||||||||||||||||||||
| 143 | 4 | $element->addChild($child->as_xml(1)); | |||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||
| 146 | 4 | foreach my $key (keys %$self) { | |||||||||||||||||||||||||||||||||||||
| 147 | 5 | ▶ | next if '_' eq substr($key, 0, 1); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my $value = $self->{$key}; | |||||||||||||||||||||||||||||||||||||
| 149 | 0 | $element->setAttribute($key, $value); | |||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | 4 | ▶ | return ($as_element) ? $element : $element->toString(1); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||
| 156 | 10 | 1; | |||||||||||||||||||||||||||||||||||||