| 1 | package Decl::NodalValuator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Decl::Node; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Decl::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Decl::NodalValuator - implements the template valuator used in a nodal environment. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Version 0.01 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 13 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This isn't really an object module; it's just a place to instantiate a template engine. Templates are defined in L<Decl::Template>, and the main | ||||||||||||||||||||||||||||
| 26 | L<Decl> class instantiates a souped-up version of that vanilla engine that can do some fancy Decl-specific stuff. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 instantiate() | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Instantiates a template engine with a nodal valuator. TODO: implement and use an error if a field is not filled in during expression. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 13 | ▶ | sub instantiate { | ||||||||||||||||||||||||||
|
Subroutine
instantiate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 13 | Decl::Template->new( | |||||||||||||||||||||||||||
| 36 | valuator => \&valuator, | ||||||||||||||||||||||||||||
| 37 | leave_misses => 0, | ||||||||||||||||||||||||||||
| 38 | spanners => { foreach => \&do_foreach, | ||||||||||||||||||||||||||||
| 39 | select => \&do_foreach, }, | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 valuator($name, $node) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Like all template valuators, this takes a name and a value context. By default, the value context is a Node (duh), but we can also pass in a | ||||||||||||||||||||||||||||
| 46 | hashref or an arrayref of alternative data sources. This gives us as much flexibility as possible when expressing our data. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub valuator { | ||||||||||||||||||||||||||
|
Subroutine
valuator: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 0 | my ($name, $node) = @_; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | return $node->{$name} if (ref $node eq 'HASH'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return $node->express_value($name) if (UNIVERSAL::can($node, 'isa') and $node->can('express_value')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
UNIVERSAL::can($node, "isa") and $node->can("express_value")
MC/DC0%0/2
UNIVERSAL::can($node, "isa") and $node->can("express_value")
UNIVERSAL::can($node, "isa")
$node->can("express_value")
Truth table0%0/3
A and B
A= UNIVERSAL::can($node, "isa")B= $node->can("express_value")
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if (ref $node eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | foreach (@$node) { | |||||||||||||||||||||||||||
| 56 | 0 | my $v = valuator ($name, $_); | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | return $v if defined $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | undef; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 do_foreach | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | The C<do_foreach> function implements a .foreach spanner in nodal templates that retrieves data from the structure and hands it off to the repeat | ||||||||||||||||||||||||||||
| 66 | loop code for formatting. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub do_foreach { | ||||||||||||||||||||||||||
|
Subroutine
do_foreach: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my ($self, $command, $values, $valuator) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my ($keyword, $target, $source, @vars) = Decl::Semantics::Code::parse_select($$command[1]); # Same syntax as ^foreach in code | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($keyword eq 'error') { # Couldn't parse the foreach | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | return $self->express_repeat ($command, [ | |||||||||||||||||||||||||||
| 77 | { error => "'.foreach/select " . $$command[1] ."' can't be parsed"}, | ||||||||||||||||||||||||||||
| 78 | $values | ||||||||||||||||||||||||||||
| 79 | ], $valuator); | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my @results = (); | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($keyword eq 'foreach') { # a local data access | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | my ($datasource, $type) = $values->find_data($source); # TODO: error handling if source not found. | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if (not @vars and $datasource->is ('data')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @vars and $datasource->is("data")
MC/DC0%0/2
not @vars and $datasource->is("data")
not @vars
$datasource->is("data")
Truth table0%0/3
not(A) and B
A= not @varsB= $datasource->is("data")
| |||||||||||||||||||||||||||||
| 87 | # Take vars from definition of data source. | ||||||||||||||||||||||||||||
| 88 | 0 | push @vars, $datasource->parmlist; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ($type ne 'data') { # We can only iterate over data (for now, anyway). | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | return $self->express_repeat ($command, [ | |||||||||||||||||||||||||||
| 93 | { error => 'source not data'}, | ||||||||||||||||||||||||||||
| 94 | $values | ||||||||||||||||||||||||||||
| 95 | ], $valuator); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | my $iterator = $datasource->iterate; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | while (my $line = $iterator->next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | # http://stackoverflow.com/questions/38345/is-there-an-elegant-zip-to-interleave-two-lists-in-perl-5 | ||||||||||||||||||||||||||||
| 101 | # This is a nice one-liner that takes the names of the variables and their values, in separate lists, | ||||||||||||||||||||||||||||
| 102 | # zips the two lists together, and makes it a hashref suitable for template interpretation. | ||||||||||||||||||||||||||||
| 103 | 0 | push @results, { (@vars, @$line)[ map { $_, $_ + @vars } ( 0 .. $#vars ) ] }; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } else { # a select! | ||||||||||||||||||||||||||||
| 106 | 0 | my $dbh = $values->find_context ('database')->payload; # TODO: error handling | |||||||||||||||||||||||||||
| 107 | 0 | my $sth = $dbh->prepare ('select ' . $target . ' from ' . $source); # TODO: error handling here, too | |||||||||||||||||||||||||||
| 108 | 0 | $sth->execute(); | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | push @results, $row; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | $self->express_repeat ($command, $values, $valuator, @results); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Michael Roberts, C<< <michael at vivtek.com> >> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 BUGS | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 125 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 126 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Copyright 2011 Michael Roberts. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 133 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 134 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 13 | 1; # End of Decl::NodalValuator | |||||||||||||||||||||||||||