File Coverage

blib/lib/RDF/Trine/Iterator/Bindings/Materialized.pm
Criterion Covered Total %
statement 52 52 100.0
branch 8 10 80.0
condition 2 4 50.0
subroutine 12 12 100.0
pod 4 4 100.0
total 78 82 95.1


line stmt bran cond sub pod time code
1             # RDF::Trine::Iterator::Bindings::Materialized
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Trine::Iterator::Bindings::Materialized - Materialized bindings class
7              
8             =head1 VERSION
9              
10             This document describes RDF::Trine::Iterator::Bindings::Materialized version 1.018
11              
12             =head1 SYNOPSIS
13              
14             use RDF::Trine::Iterator;
15            
16             my $iterator = RDF::Trine::Iterator::Bindings::Materialized->new( \@data, \@names );
17             while (my $row = $iterator->next) {
18             my @vars = keys %$row;
19             # do something with @vars
20             }
21              
22             my $iterator = RDF::Trine::Iterator::Bindings->new( \&code, \@names );
23             my $miter = $iterator->materialize;
24             while (my $row = $miter->next) {
25             my @vars = keys %$row;
26             # do something with @vars
27             }
28             $miter->reset; # start the iteration again
29             while (my $row = $miter->next) {
30             # ...
31             }
32              
33             =head1 METHODS
34              
35             Beyond the methods documented below, this class inherits methods from the
36             L<RDF::Trine::Iterator::Bindings> class.
37              
38             =over 4
39              
40             =cut
41              
42             package RDF::Trine::Iterator::Bindings::Materialized;
43              
44 68     68   425 use strict;
  68         202  
  68         1751  
45 68     68   369 use warnings;
  68         160  
  68         1668  
46 68     68   366 no warnings 'redefine';
  68         181  
  68         1970  
47 68     68   347 use Data::Dumper;
  68         144  
  68         3071  
48 68     68   427 use base qw(RDF::Trine::Iterator::Bindings);
  68         148  
  68         5578  
49              
50 68     68   411 use Scalar::Util qw(blessed reftype);
  68         160  
  68         4092  
51              
52             our ($VERSION);
53             BEGIN {
54 68     68   18358 $VERSION = '1.018';
55             }
56              
57             =item C<< new ( \@results, \@names, %args ) >>
58              
59             Returns a new materialized bindings interator. Results must be a reference to an
60             array containing individual results.
61              
62             =cut
63              
64             sub new {
65 4     4 1 37 my $class = shift;
66 4   50     30 my $data = shift || [];
67 4   50     18 my $names = shift || [];
68 4 50       19 Carp::confess unless (scalar(@_) % 2 == 0);
69 4         13 my %args = @_;
70            
71 4 100       26 if (reftype($data) eq 'CODE') {
72 1         3 my @rows;
73 1         4 while (my $row = $data->()) {
74 4         21 push(@rows, $row);
75             }
76 1         5 $data = \@rows;
77             }
78            
79 4 50       20 Carp::confess "not an ARRAY: " . Dumper($data) unless (reftype($data) eq 'ARRAY');
80            
81 4         11 my $type = 'bindings';
82 4         8 my $index = 0;
83             my $stream = sub {
84 25     25   50 my $data = $data->[ $index++ ];
85 25 100       66 unless (defined($data)) {
86 5         12 $index = 0;
87             }
88 25         50 return $data;
89 4         20 };
90 4         36 my $self = $class->SUPER::new( $stream, $names, %args );
91 4         13 $self->{_data} = $data;
92 4         11 $self->{_index} = \$index;
93            
94 4         19 return $self;
95             }
96              
97             =item C<< reset >>
98              
99             Returns the iterator to its starting position.
100              
101             =cut
102              
103             sub reset {
104 3     3 1 611 my $self = shift;
105 3         7 ${ $self->{_index} } = 0;
  3         12  
106             }
107              
108             =item C<< next >>
109              
110             Returns the next item in the iterator.
111              
112             =cut
113              
114             sub next {
115 25     25 1 3656 my $self = shift;
116 25         93 my $data = $self->SUPER::next;
117 25 100       63 unless (defined($data)) {
118 5         13 $self->{_finished} = 0;
119             }
120 25         62 return $data;
121             }
122              
123             =item C<< length >>
124              
125             Returns the number of elements in the iterator.
126              
127             =cut
128              
129             sub length {
130 2     2 1 1333 my $self = shift;
131 2         5 return scalar(@{ $self->{ _data } });
  2         12  
132             }
133              
134             1;
135              
136             __END__
137              
138             =back
139              
140             =head1 BUGS
141              
142             Please report any bugs or feature requests to through the GitHub web interface
143             at L<https://github.com/kasei/perlrdf/issues>.
144              
145             =head1 AUTHOR
146              
147             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
148              
149             =head1 COPYRIGHT
150              
151             Copyright (c) 2006-2012 Gregory Todd Williams. This
152             program is free software; you can redistribute it and/or modify it under
153             the same terms as Perl itself.
154              
155             =cut