| 1 | package Data::Xslate; | ||||||||||||||||||
| 2 | 4 | ▶ | use 5.008001; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 4 | our $VERSION = '0.09'; | |||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 4 | ▶ | use Text::Xslate; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 4 | ▶ | use Carp qw( croak ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 4 | ▶ | use Storable qw( freeze thaw ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | # A tied-hash class used to expose the data as the Xslate | ||||||||||||||||||
| 12 | # vars when processing the data. | ||||||||||||||||||
| 13 | { | ||||||||||||||||||
| 14 | package # NO INDEX | ||||||||||||||||||
| 15 | Data::Xslate::Vars; | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 4 | ▶ | use base 'Tie::Hash'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | 458 | ▶ | sub TIEHASH { | ||||||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 20 | 458 | my ($class, $sub) = @_; | |||||||||||||||||
| 21 | 458 | return bless {sub=>$sub}, $class; | |||||||||||||||||
| 22 | } | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 7 | ▶ | sub FETCH { | ||||||||||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 25 | 7 | my ($self, $key) = @_; | |||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | 7 | return $self->{sub}->( $key ); | |||||||||||||||||
| 28 | } | ||||||||||||||||||
| 29 | } | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | 4 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||
| 32 | 153 | my $class = shift; | |||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 153 | ▶ | die if @_ % 2 != 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | 153 | my $args = { @_ }; | |||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 153 | my $defaults = { | |||||||||||||||||
| 38 | substitution_tag => '=', | ||||||||||||||||||
| 39 | nested_key_tag => '=', | ||||||||||||||||||
| 40 | key_separator => '.', | ||||||||||||||||||
| 41 | }; | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 153 | my $self = bless { %$defaults }, $class; | |||||||||||||||||
| 44 | 153 | foreach my $key (keys %$defaults) { | |||||||||||||||||
| 45 | 459 | ▶ | next if ! exists $args->{$key}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 46 | 450 | $self->{$key} = delete $args->{$key}; | |||||||||||||||||
| 47 | } | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 153 | ▶ | my $function = delete( $args->{function} ) || {}; | ||||||||||||||||
|
Condition50%1/2
delete $args->{'function'} || {}
MC/DC0%0/1
delete $args->{'function'} || {}
delete $args->{'function'}
Truth table50%1/2
A
A= delete $args->{'function'}
| |||||||||||||||||||
| 50 | 153 | ▶ | $function->{node} ||= \&_find_node_for_xslate; | ||||||||||||||||
|
Condition50%1/2
$function->{"node"} ||= \&Data::Xslate::_find_node_for_xslate
MC/DC0%0/1
$function->{"node"} ||= \&Data::Xslate::_find_node_for_xslate
$function->{"node"}
Truth table50%1/2
A
A= $function->{"node"}
| |||||||||||||||||||
| 51 | 153 | $self->{_xslate} = Text::Xslate->new( | |||||||||||||||||
| 52 | type => 'text', | ||||||||||||||||||
| 53 | function => $function, | ||||||||||||||||||
| 54 | %$args, | ||||||||||||||||||
| 55 | ); | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 153 | return $self; | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | # Arguments. | ||||||||||||||||||
| 61 | 458 | ▶ | sub substitution_tag { $_[0]->{substitution_tag} } | ||||||||||||||||
|
Subroutine
substitution_tag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 62 | 458 | ▶ | sub nested_key_tag { $_[0]->{nested_key_tag} } | ||||||||||||||||
|
Subroutine
nested_key_tag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 63 | 458 | ▶ | sub key_separator { $_[0]->{key_separator} } | ||||||||||||||||
|
Subroutine
key_separator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | # Attributes. | ||||||||||||||||||
| 66 | 458 | ▶ | sub _xslate { $_[0]->{_xslate} } | ||||||||||||||||
|
Subroutine
_xslate: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | # State variables, only used during local() calls to maintain | ||||||||||||||||||
| 69 | # state in recursive function calls. | ||||||||||||||||||
| 70 | 4 | our $XSLATE; | |||||||||||||||||
| 71 | 4 | our $VARS; | |||||||||||||||||
| 72 | 4 | our $ROOT; | |||||||||||||||||
| 73 | 4 | our $NODES; | |||||||||||||||||
| 74 | 4 | our $SUBSTITUTION_TAG; | |||||||||||||||||
| 75 | 4 | our $NESTED_KEY_TAG; | |||||||||||||||||
| 76 | 4 | our $KEY_SEPARATOR; | |||||||||||||||||
| 77 | 4 | our $PATH_FOR_XSLATE; | |||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | 458 | ▶ | sub render { | ||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 80 | 458 | my ($self, $data) = @_; | |||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | 458 | $data = thaw( freeze( $data ) ); | |||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | 458 | local $Carp::Internal{ (__PACKAGE__) } = 1; | |||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 458 | local $XSLATE = $self->_xslate(); | |||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 458 | my %vars; | |||||||||||||||||
| 89 | 458 | tie %vars, 'Data::Xslate::Vars', \&_find_node_for_xslate; | |||||||||||||||||
| 90 | 458 | local $VARS = \%vars; | |||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | 458 | local $ROOT = $data; | |||||||||||||||||
| 93 | 458 | local $NODES = {}; | |||||||||||||||||
| 94 | 458 | local $SUBSTITUTION_TAG = $self->substitution_tag(); | |||||||||||||||||
| 95 | 458 | local $NESTED_KEY_TAG = $self->nested_key_tag(); | |||||||||||||||||
| 96 | 458 | local $KEY_SEPARATOR = $self->key_separator(); | |||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | 458 | return _evaluate_node( 'root' => $data ); | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 2632 | ▶ | sub _evaluate_node { | ||||||||||||||||
|
Subroutine
_evaluate_node: called CC=9 SCAR=22.0
| |||||||||||||||||||
| 102 | 2632 | my ($path, $node) = @_; | |||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | 2632 | ▶ | return $NODES->{$path} if exists $NODES->{$path}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | 1847 | ▶ | if (!ref $node) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 107 | 931 | ▶ | if (defined $node) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | 931 | ▶ | if ($node =~ m{^\Q$SUBSTITUTION_TAG\E\s*(.+?)\s*$}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 109 | 157 | $node = _find_node( $1, $path ); | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | else { | ||||||||||||||||||
| 112 | 774 | local $PATH_FOR_XSLATE = $path; | |||||||||||||||||
| 113 | 774 | $node = $XSLATE->render_string( $node, $VARS ); | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | 931 | $NODES->{$path} = $node; | |||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | elsif (ref($node) eq 'HASH') { | ||||||||||||||||||
| 119 | 915 | $NODES->{$path} = $node; | |||||||||||||||||
| 120 | 915 | foreach my $key (sort keys %$node) { | |||||||||||||||||
| 121 | 1386 | ▶ | if ($key =~ m{^(.*)\Q$NESTED_KEY_TAG\E$}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 122 | 152 | my $sub_path = "$path$KEY_SEPARATOR$1"; | |||||||||||||||||
| 123 | 152 | my $value = delete $node->{$key}; | |||||||||||||||||
| 124 | 152 | _set_node( $sub_path, $value ); | |||||||||||||||||
| 125 | } | ||||||||||||||||||
| 126 | else { | ||||||||||||||||||
| 127 | 1234 | my $sub_path = "$path$KEY_SEPARATOR$key"; | |||||||||||||||||
| 128 | 1234 | $node->{$key} = _evaluate_node( $sub_path, $node->{$key} ); | |||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | } | ||||||||||||||||||
| 131 | } | ||||||||||||||||||
| 132 | elsif (ref($node) eq 'ARRAY') { | ||||||||||||||||||
| 133 | 1 | $NODES->{$path} = $node; | |||||||||||||||||
| 134 | @$node = ( | ||||||||||||||||||
| 135 | 1 | map { _evaluate_node( "$path$KEY_SEPARATOR$_" => $node->[$_] ) } | |||||||||||||||||
| 136 | (0..$#$node) | ||||||||||||||||||
| 137 | ); | ||||||||||||||||||
| 138 | } | ||||||||||||||||||
| 139 | else { | ||||||||||||||||||
| 140 | 0 | croak "The config node at $path is neither a hash, array, or scalar"; | |||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | 1847 | return $node; | |||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | 324 | ▶ | sub _load_node { | ||||||||||||||||
|
Subroutine
_load_node: called CC=6 SCAR=19.9
| |||||||||||||||||||
| 147 | 324 | my ($path) = @_; | |||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | 324 | my @parts = split(/\Q$KEY_SEPARATOR\E/, $path); | |||||||||||||||||
| 150 | 324 | my $built_path = shift( @parts ); # root | |||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | 324 | my $node = $ROOT; | |||||||||||||||||
| 153 | 324 | ▶ | while (@parts) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 154 | 640 | my $key = shift( @parts ); | |||||||||||||||||
| 155 | 640 | $built_path .= "$KEY_SEPARATOR$key"; | |||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | 640 | ▶ | if (ref($node) eq 'HASH') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 158 | 640 | ▶ | return undef if !exists $node->{$key}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 159 | 633 | $node = _evaluate_node( $built_path => $node->{$key} ); | |||||||||||||||||
| 160 | } | ||||||||||||||||||
| 161 | elsif (ref($node) eq 'ARRAY') { | ||||||||||||||||||
| 162 | 0 | ▶ | return undef if $key > $#$node; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 163 | 0 | $node = _evaluate_node( $built_path => $node->[$key] ); | |||||||||||||||||
| 164 | } | ||||||||||||||||||
| 165 | else { | ||||||||||||||||||
| 166 | 0 | croak "The config node at $path is neither a hash or array"; | |||||||||||||||||
| 167 | } | ||||||||||||||||||
| 168 | } | ||||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | 317 | return $node; | |||||||||||||||||
| 171 | } | ||||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | 317 | ▶ | sub _find_node { | ||||||||||||||||
|
Subroutine
_find_node: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 174 | 317 | my ($path, $from_path) = @_; | |||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | 317 | ▶ | if ($path =~ m{^\Q$KEY_SEPARATOR\E(.+)}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 177 | 1 | $path = $1; | |||||||||||||||||
| 178 | 1 | $from_path = "root${KEY_SEPARATOR}root_sub_key_that_is_not_used_for_absolute_keys"; | |||||||||||||||||
| 179 | } | ||||||||||||||||||
| 180 | |||||||||||||||||||
| 181 | 317 | my @parts = split(/\Q$KEY_SEPARATOR\E/, $from_path); | |||||||||||||||||
| 182 | 317 | pop( @parts ); | |||||||||||||||||
| 183 | |||||||||||||||||||
| 184 | 317 | ▶ | while (@parts) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 185 | 324 | my $sub_path = join($KEY_SEPARATOR, @parts); | |||||||||||||||||
| 186 | |||||||||||||||||||
| 187 | 324 | my $node = _load_node( "$sub_path$KEY_SEPARATOR$path" ); | |||||||||||||||||
| 188 | 324 | ▶ | return $node if $node; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 189 | |||||||||||||||||||
| 190 | 7 | pop( @parts ); | |||||||||||||||||
| 191 | } | ||||||||||||||||||
| 192 | |||||||||||||||||||
| 193 | 0 | return _load_node( $path ); | |||||||||||||||||
| 194 | } | ||||||||||||||||||
| 195 | |||||||||||||||||||
| 196 | 160 | ▶ | sub _find_node_for_xslate { | ||||||||||||||||
|
Subroutine
_find_node_for_xslate: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 197 | 160 | my ($path) = @_; | |||||||||||||||||
| 198 | 160 | return _find_node( $path, $PATH_FOR_XSLATE ); | |||||||||||||||||
| 199 | } | ||||||||||||||||||
| 200 | |||||||||||||||||||
| 201 | 152 | ▶ | sub _set_node { | ||||||||||||||||
|
Subroutine
_set_node: called CC=8 SCAR=24.5
| |||||||||||||||||||
| 202 | 152 | my ($path, $value) = @_; | |||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | 152 | my @parts = split(/\Q$KEY_SEPARATOR\E/, $path); | |||||||||||||||||
| 205 | 152 | my $built_path = shift( @parts ); # root | |||||||||||||||||
| 206 | 152 | my $last_part = pop( @parts ); | |||||||||||||||||
| 207 | |||||||||||||||||||
| 208 | 152 | my $node = $ROOT; | |||||||||||||||||
| 209 | 152 | ▶ | while (@parts) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 210 | 152 | my $key = shift( @parts ); | |||||||||||||||||
| 211 | 152 | $built_path .= "$KEY_SEPARATOR$key"; | |||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | 152 | ▶ | if (ref($node) eq 'HASH') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 214 | 152 | ▶ | return 0 if !exists $node->{$key}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 215 | 152 | $node = _evaluate_node( $built_path => $node->{$key} ); | |||||||||||||||||
| 216 | } | ||||||||||||||||||
| 217 | elsif (ref($node) eq 'ARRAY') { | ||||||||||||||||||
| 218 | 0 | ▶ | return 0 if $key > $#$node; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 219 | 0 | $node = _evaluate_node( $built_path => $node->[$key] ); | |||||||||||||||||
| 220 | } | ||||||||||||||||||
| 221 | else { | ||||||||||||||||||
| 222 | 0 | croak "The config node at $path is neither a hash or array"; | |||||||||||||||||
| 223 | } | ||||||||||||||||||
| 224 | } | ||||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | 152 | delete $NODES->{$path}; | |||||||||||||||||
| 227 | 152 | $value = _evaluate_node( $path => $value ); | |||||||||||||||||
| 228 | |||||||||||||||||||
| 229 | 152 | ▶ | if (ref($node) eq 'HASH') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 230 | 152 | $node->{$last_part} = $value; | |||||||||||||||||
| 231 | } | ||||||||||||||||||
| 232 | elsif (ref($node) eq 'ARRAY') { | ||||||||||||||||||
| 233 | 0 | $node->[$last_part] = $value; | |||||||||||||||||
| 234 | } | ||||||||||||||||||
| 235 | |||||||||||||||||||
| 236 | 152 | return 1; | |||||||||||||||||
| 237 | } | ||||||||||||||||||
| 238 | |||||||||||||||||||
| 239 | 4 | 1; | |||||||||||||||||
| 240 | __END__ | ||||||||||||||||||