| 1 | # | ||||||||||||||||||||
| 2 | # This file is part of MooseX-Attribute-Deflator | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # This software is Copyright (c) 2012 by Moritz Onken. | ||||||||||||||||||||
| 5 | # | ||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||
| 7 | # | ||||||||||||||||||||
| 8 | # The (three-clause) BSD License | ||||||||||||||||||||
| 9 | # | ||||||||||||||||||||
| 10 | package MooseX::Attribute::Deflator::Structured; | ||||||||||||||||||||
| 11 | { | ||||||||||||||||||||
| 12 | 2 | $MooseX::Attribute::Deflator::Structured::VERSION = '2.2.2'; | |||||||||||||||||||
| 13 | } | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # ABSTRACT: Deflators for MooseX::Types::Structured | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | ▶ | use MooseX::Attribute::Deflator; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 0 | ▶ | deflate 'MooseX::Types::Structured::Optional[]', via { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 20 | 0 | my ( $attr, $constraint, $deflate ) = @_; | |||||||||||||||||||
| 21 | 0 | return $deflate->( $_, $constraint->type_parameter ); | |||||||||||||||||||
| 22 | 5 | ▶ | }, inline_as { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 5 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||||||||||||
| 24 | 5 | return $deflators->( $constraint->type_parameter ); | |||||||||||||||||||
| 25 | 2 | }; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 0 | ▶ | inflate 'MooseX::Types::Structured::Optional[]', via { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 28 | 0 | my ( $attr, $constraint, $inflate ) = @_; | |||||||||||||||||||
| 29 | 0 | return $inflate->( $_, $constraint->type_parameter ); | |||||||||||||||||||
| 30 | 5 | ▶ | }, inline_as { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 5 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||||||||||||
| 32 | 5 | return $deflators->( $constraint->type_parameter ); | |||||||||||||||||||
| 33 | 2 | }; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | deflate 'MooseX::Types::Structured::Map[]', via { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 36 | 0 | my ( $attr, $constraint, $deflate ) = @_; | |||||||||||||||||||
| 37 | 0 | my $value = {%$_}; | |||||||||||||||||||
| 38 | 0 | my $constraints = $constraint->type_constraints; | |||||||||||||||||||
| 39 | 0 | ▶ | while ( my ( $k, $v ) = each %$value ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 0 | $value->{$k} = $deflate->( $value->{$k}, $constraints->[1] ); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | 0 | return $deflate->( $value, $constraint->parent ); | |||||||||||||||||||
| 43 | 1 | ▶ | }, inline_as { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | 1 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||||||||||||
| 45 | return ( | ||||||||||||||||||||
| 46 | 1 | '$value = {%$value};', | |||||||||||||||||||
| 47 | 'while ( my ( $k, $v ) = each %$value ) {', | ||||||||||||||||||||
| 48 | '$value->{$k} = do {', | ||||||||||||||||||||
| 49 | ' my $value = $value->{$k};', | ||||||||||||||||||||
| 50 | ' $value = do {', | ||||||||||||||||||||
| 51 | $deflators->( $constraint->type_constraints->[1] ), | ||||||||||||||||||||
| 52 | ' };', | ||||||||||||||||||||
| 53 | ' };', | ||||||||||||||||||||
| 54 | '}', | ||||||||||||||||||||
| 55 | $deflators->( $constraint->parent ), | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | 2 | }; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | ▶ | inflate 'MooseX::Types::Structured::Map[]', via { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 60 | 0 | my ( $attr, $constraint, $inflate ) = @_; | |||||||||||||||||||
| 61 | 0 | my $value = $inflate->( $_, $constraint->parent ); | |||||||||||||||||||
| 62 | 0 | my $constraints = $constraint->type_constraints; | |||||||||||||||||||
| 63 | 0 | ▶ | while ( my ( $k, $v ) = each %$value ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | $value->{$k} = $inflate->( $value->{$k}, $constraints->[1] ); | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | 0 | return $value; | |||||||||||||||||||
| 67 | 1 | ▶ | }, inline_as { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 68 | 1 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||||||||||||
| 69 | return ( | ||||||||||||||||||||
| 70 | 1 | '$value = do {', | |||||||||||||||||||
| 71 | $deflators->( $constraint->parent ), | ||||||||||||||||||||
| 72 | ' };', | ||||||||||||||||||||
| 73 | 'while ( my ( $k, $v ) = each %$value ) {', | ||||||||||||||||||||
| 74 | ' $value->{$k} = do {', | ||||||||||||||||||||
| 75 | ' my $value = $value->{$k};', | ||||||||||||||||||||
| 76 | ' $value = do {', | ||||||||||||||||||||
| 77 | $deflators->( $constraint->type_constraints->[1] ), | ||||||||||||||||||||
| 78 | ' };', | ||||||||||||||||||||
| 79 | ' };', | ||||||||||||||||||||
| 80 | '}', | ||||||||||||||||||||
| 81 | '$value', | ||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||
| 83 | 2 | }; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | ▶ | deflate 'MooseX::Types::Structured::Dict[]', via { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 86 | 0 | my ( $attr, $constraint, $deflate ) = @_; | |||||||||||||||||||
| 87 | 0 | my %constraints = @{ $constraint->type_constraints }; | |||||||||||||||||||
| 88 | 0 | my $value = {%$_}; | |||||||||||||||||||
| 89 | 0 | ▶ | while ( my ( $k, $v ) = each %$value ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 0 | $value->{$k} = $deflate->( $value->{$k}, $constraints{$k} ); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | 0 | return $deflate->( $value, $constraint->parent ); | |||||||||||||||||||
| 93 | 5 | ▶ | }, inline_as { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 94 | 5 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||||||||||||
| 95 | 5 | my %constraints = @{ $constraint->type_constraints }; | |||||||||||||||||||
| 96 | 5 | my @map = 'my $dict;'; | |||||||||||||||||||
| 97 | 5 | ▶ | while ( my ( $k, $v ) = each %constraints ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 13 | push( @map, | |||||||||||||||||||
| 99 | '$dict->{"' . quotemeta($k) . '"} = sub { ', | ||||||||||||||||||||
| 100 | 'my $value = shift;', | ||||||||||||||||||||
| 101 | $deflators->($v), ' };' ); | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | return ( | ||||||||||||||||||||
| 104 | 5 | @map, | |||||||||||||||||||
| 105 | '$value = {%$value};', | ||||||||||||||||||||
| 106 | 'while ( my ( $k, $v ) = each %$value ) {', | ||||||||||||||||||||
| 107 | '$value->{$k} = do {', | ||||||||||||||||||||
| 108 | ' my $value = $value->{$k};', | ||||||||||||||||||||
| 109 | ' $value = $dict->{$k}->($value);', | ||||||||||||||||||||
| 110 | ' };', | ||||||||||||||||||||
| 111 | '}', | ||||||||||||||||||||
| 112 | $deflators->( $constraint->parent ), | ||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||
| 114 | 2 | }; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | ▶ | inflate 'MooseX::Types::Structured::Dict[]', via { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 117 | 0 | my ( $attr, $constraint, $inflate ) = @_; | |||||||||||||||||||
| 118 | 0 | my %constraints = @{ $constraint->type_constraints }; | |||||||||||||||||||
| 119 | 0 | my $value = $inflate->( $_, $constraint->parent ); | |||||||||||||||||||
| 120 | 0 | ▶ | while ( my ( $k, $v ) = each %$value ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | $value->{$k} = $inflate->( $value->{$k}, $constraints{$k} ); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | 0 | return $value; | |||||||||||||||||||
| 124 | 5 | ▶ | }, inline_as { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 125 | 5 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||||||||||||
| 126 | 5 | my %constraints = @{ $constraint->type_constraints }; | |||||||||||||||||||
| 127 | 5 | my @map = 'my $dict;'; | |||||||||||||||||||
| 128 | 5 | ▶ | while ( my ( $k, $v ) = each %constraints ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 13 | push( @map, | |||||||||||||||||||
| 130 | '$dict->{"' . quotemeta($k) . '"} = sub { ', | ||||||||||||||||||||
| 131 | 'my $value = shift;', | ||||||||||||||||||||
| 132 | $deflators->($v), ' };' ); | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | return ( | ||||||||||||||||||||
| 135 | 5 | @map, | |||||||||||||||||||
| 136 | '$value = do {', | ||||||||||||||||||||
| 137 | $deflators->( $constraint->parent ), | ||||||||||||||||||||
| 138 | ' };', | ||||||||||||||||||||
| 139 | 'while ( my ( $k, $v ) = each %$value ) {', | ||||||||||||||||||||
| 140 | '$value->{$k} = do {', | ||||||||||||||||||||
| 141 | ' my $value = $value->{$k};', | ||||||||||||||||||||
| 142 | ' $value = $dict->{$k}->($value);', | ||||||||||||||||||||
| 143 | ' };', | ||||||||||||||||||||
| 144 | '}', | ||||||||||||||||||||
| 145 | '$value', | ||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||
| 147 | 2 | }; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 0 | ▶ | deflate 'MooseX::Types::Structured::Tuple[]', via { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 150 | 0 | my ( $attr, $constraint, $deflate ) = @_; | |||||||||||||||||||
| 151 | 0 | my @constraints = @{ $constraint->type_constraints }; | |||||||||||||||||||
| 152 | 0 | my $value = [@$_]; | |||||||||||||||||||
| 153 | 0 | ▶ | for ( my $i = 0; $i < @$value; $i++ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | ▶ | $value->[$i] = $deflate->( $value->[$i], | ||||||||||||||||||
|
Condition0%0/3
$constraints[$i] || $constraints[-1]
MC/DC0%0/2
$constraints[$i] || $constraints[-1]
$constraints[$i]
$constraints[-1]
Truth table0%0/3
A || B
A= $constraints[$i]B= $constraints[-1]
| |||||||||||||||||||||
| 155 | $constraints[$i] || $constraints[-1] ); | ||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | 0 | return $deflate->( $value, $constraint->parent ); | |||||||||||||||||||
| 158 | 1 | ▶ | }, inline_as { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 159 | 1 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||||||||||||
| 160 | 1 | my @constraints = @{ $constraint->type_constraints }; | |||||||||||||||||||
| 161 | 1 | my @map = 'my $tuple = [];'; | |||||||||||||||||||
| 162 | 1 | foreach my $tc (@constraints) { | |||||||||||||||||||
| 163 | 4 | push( @map, | |||||||||||||||||||
| 164 | 'push(@$tuple, sub {', | ||||||||||||||||||||
| 165 | 'my $value = shift;', | ||||||||||||||||||||
| 166 | $deflators->($tc), ' });' ); | ||||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | return ( | ||||||||||||||||||||
| 169 | 1 | @map, | |||||||||||||||||||
| 170 | '$value = [@$value];', | ||||||||||||||||||||
| 171 | 'for ( my $i = 0; $i < @$value; $i++ ) {', | ||||||||||||||||||||
| 172 | '$value->[$i] = do {', | ||||||||||||||||||||
| 173 | ' my $value = $value->[$i];', | ||||||||||||||||||||
| 174 | ' $value = ($tuple->[$i] || $tuple->[-1])->($value);', | ||||||||||||||||||||
| 175 | ' };', | ||||||||||||||||||||
| 176 | '}', | ||||||||||||||||||||
| 177 | $deflators->( $constraint->parent ), | ||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||
| 179 | 2 | }; | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | ▶ | inflate 'MooseX::Types::Structured::Tuple[]', via { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 182 | 0 | my ( $attr, $constraint, $inflate ) = @_; | |||||||||||||||||||
| 183 | 0 | my @constraints = @{ $constraint->type_constraints }; | |||||||||||||||||||
| 184 | 0 | my $value = $inflate->( $_, $constraint->parent ); | |||||||||||||||||||
| 185 | 0 | ▶ | for ( my $i = 0; $i < @$value; $i++ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | 0 | ▶ | $value->[$i] = $inflate->( $value->[$i], | ||||||||||||||||||
|
Condition0%0/3
$constraints[$i] || $constraints[-1]
MC/DC0%0/2
$constraints[$i] || $constraints[-1]
$constraints[$i]
$constraints[-1]
Truth table0%0/3
A || B
A= $constraints[$i]B= $constraints[-1]
| |||||||||||||||||||||
| 187 | $constraints[$i] || $constraints[-1] ); | ||||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | 0 | return $value; | |||||||||||||||||||
| 190 | 1 | ▶ | }, inline_as { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 191 | 1 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||||||||||||
| 192 | 1 | my @constraints = @{ $constraint->type_constraints }; | |||||||||||||||||||
| 193 | 1 | my @map = 'my $tuple = [];'; | |||||||||||||||||||
| 194 | 1 | foreach my $tc (@constraints) { | |||||||||||||||||||
| 195 | 4 | push( @map, | |||||||||||||||||||
| 196 | 'push(@$tuple, sub {', | ||||||||||||||||||||
| 197 | 'my $value = shift;', | ||||||||||||||||||||
| 198 | $deflators->($tc), ' });' ); | ||||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | return ( | ||||||||||||||||||||
| 201 | 1 | @map, | |||||||||||||||||||
| 202 | '$value = do {', | ||||||||||||||||||||
| 203 | $deflators->( $constraint->parent ), | ||||||||||||||||||||
| 204 | ' };', | ||||||||||||||||||||
| 205 | 'for ( my $i = 0; $i < @$value; $i++ ) {', | ||||||||||||||||||||
| 206 | '$value->[$i] = do {', | ||||||||||||||||||||
| 207 | ' my $value = $value->[$i];', | ||||||||||||||||||||
| 208 | ' $value = ($tuple->[$i] || $tuple->[-1])->($value);', | ||||||||||||||||||||
| 209 | ' };', | ||||||||||||||||||||
| 210 | '}', | ||||||||||||||||||||
| 211 | '$value', | ||||||||||||||||||||
| 212 | ); | ||||||||||||||||||||
| 213 | 2 | }; | |||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 2 | 1; | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =pod | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =head1 NAME | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | MooseX::Attribute::Deflator::Structured - Deflators for MooseX::Types::Structured | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head1 VERSION | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | version 2.2.2 | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =head1 SYNOPSIS | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | use MooseX::Attribute::Deflator::Structured; | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =head1 DESCRIPTION | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | This module registers sane type deflators and inflators for L<MooseX::Types::Structured>. | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =head1 AUTHOR | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | Moritz Onken | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | This software is Copyright (c) 2012 by Moritz Onken. | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | This is free software, licensed under: | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | The (three-clause) BSD License | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | =cut | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | __END__ | ||||||||||||||||||||