| 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::Moose; | ||||||||||
| 11 | { | ||||||||||
| 12 | 6 | $MooseX::Attribute::Deflator::Moose::VERSION = '2.2.2'; | |||||||||
| 13 | } | ||||||||||
| 14 | |||||||||||
| 15 | # ABSTRACT: Deflators for Moose type constraints | ||||||||||
| 16 | |||||||||||
| 17 | 6 | ▶ | use MooseX::Attribute::Deflator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 6 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 6 | ▶ | if($ENV{HARNESS_ACTIVE}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 0 | ▶ | deflate [qw(ArrayRef HashRef)], via { JSON->new->utf8->canonical->encode($_) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 22 | 31 | ▶ | inline_as {'JSON->new->utf8->canonical->encode($value)'}; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 0 | ▶ | inflate [qw(ArrayRef HashRef)], via { JSON->new->utf8->canonical->decode($_) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 24 | 31 | ▶ | inline_as {'JSON->new->utf8->canonical->decode($value)'}; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 25 | } else { | ||||||||||
| 26 | 0 | ▶ | deflate [qw(ArrayRef HashRef)], via { JSON::encode_json($_) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 27 | 0 | ▶ | inline_as {'JSON::encode_json($value)'}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 28 | 0 | ▶ | inflate [qw(ArrayRef HashRef)], via { JSON::decode_json($_) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 29 | 0 | ▶ | inline_as {'JSON::decode_json($value)'}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | deflate 'ScalarRef', via {$$_}, inline_as {'$$value'}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||
| 34 | 0 | ▶ | inflate 'ScalarRef', via { \$_ }, inline_as {'\$value'}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | deflate 'Bool', via { $_ ? JSON::true : JSON::false }, | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 37 | 2 | ▶ | inline_as {'$value ? JSON::true : JSON::false'}; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 0 | ▶ | inflate 'Bool', via { $_ ? 1 : 0 }, inline_as {'$value ? 1 : 0'}; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=10.5
Subroutine
__ANON__: called CC=1 SCAR=2.0
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | deflate 'Item', via {$_}, inline_as {'$value'}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||
| 41 | 0 | ▶ | inflate 'Item', via {$_}, inline_as {'$value'}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | deflate 'HashRef[]', via { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 44 | 0 | my ( $attr, $constraint, $deflate ) = @_; | |||||||||
| 45 | 0 | my $value = {%$_}; | |||||||||
| 46 | 0 | ▶ | while ( my ( $k, $v ) = each %$value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | $value->{$k} | ||||||||||
| 48 | 0 | = $deflate->( $value->{$k}, $constraint->type_parameter ); | |||||||||
| 49 | } | ||||||||||
| 50 | 0 | return $deflate->( $value, $constraint->parent ); | |||||||||
| 51 | 1 | ▶ | }, inline_as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 1 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||
| 53 | return ( | ||||||||||
| 54 | 1 | '$value = {%$value};', | |||||||||
| 55 | 'while ( my ( $k, $v ) = each %$value ) {', | ||||||||||
| 56 | '$value->{$k} = do {', | ||||||||||
| 57 | ' my $value = $value->{$k};', | ||||||||||
| 58 | ' $value = do {', | ||||||||||
| 59 | $deflators->( $constraint->type_parameter ), | ||||||||||
| 60 | ' };', | ||||||||||
| 61 | ' };', | ||||||||||
| 62 | '}', | ||||||||||
| 63 | $deflators->( $constraint->parent ), | ||||||||||
| 64 | ); | ||||||||||
| 65 | 6 | }; | |||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | inflate 'HashRef[]', via { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 68 | 0 | my ( $attr, $constraint, $inflate ) = @_; | |||||||||
| 69 | 0 | my $value = $inflate->( $_, $constraint->parent ); | |||||||||
| 70 | 0 | ▶ | while ( my ( $k, $v ) = each %$value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | $value->{$k} | ||||||||||
| 72 | 0 | = $inflate->( $value->{$k}, $constraint->type_parameter ); | |||||||||
| 73 | } | ||||||||||
| 74 | 0 | return $value; | |||||||||
| 75 | 1 | ▶ | }, inline_as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 1 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||
| 77 | return ( | ||||||||||
| 78 | 1 | '$value = do {', | |||||||||
| 79 | $deflators->( $constraint->parent ), | ||||||||||
| 80 | ' };', | ||||||||||
| 81 | 'while ( my ( $k, $v ) = each %$value ) {', | ||||||||||
| 82 | ' $value->{$k} = do {', | ||||||||||
| 83 | ' my $value = $value->{$k};', | ||||||||||
| 84 | ' $value = do {', | ||||||||||
| 85 | $deflators->( $constraint->type_parameter ), | ||||||||||
| 86 | ' };', | ||||||||||
| 87 | ' };', | ||||||||||
| 88 | '}', | ||||||||||
| 89 | '$value', | ||||||||||
| 90 | ); | ||||||||||
| 91 | 6 | }; | |||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | deflate 'ArrayRef[]', via { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 94 | 0 | my ( $attr, $constraint, $deflate ) = @_; | |||||||||
| 95 | 0 | my $value = [@$_]; | |||||||||
| 96 | 0 | $_ = $deflate->( $_, $constraint->type_parameter ) for (@$value); | |||||||||
| 97 | 0 | return $deflate->( $value, $constraint->parent ); | |||||||||
| 98 | 6 | ▶ | }, inline_as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 99 | 6 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||
| 100 | return ( | ||||||||||
| 101 | 6 | '$value = [@$value];', | |||||||||
| 102 | 'for( @$value ) {', | ||||||||||
| 103 | ' $_ = do {', | ||||||||||
| 104 | ' my $value = $_;', | ||||||||||
| 105 | ' $value = do {', | ||||||||||
| 106 | $deflators->( $constraint->type_parameter ), | ||||||||||
| 107 | ' };', | ||||||||||
| 108 | ' };', | ||||||||||
| 109 | '}', | ||||||||||
| 110 | $deflators->( $constraint->parent ), | ||||||||||
| 111 | ); | ||||||||||
| 112 | 6 | }; | |||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | inflate 'ArrayRef[]', via { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 115 | 0 | my ( $attr, $constraint, $inflate ) = @_; | |||||||||
| 116 | 0 | my $value = $inflate->( $_, $constraint->parent ); | |||||||||
| 117 | 0 | $_ = $inflate->( $_, $constraint->type_parameter ) for (@$value); | |||||||||
| 118 | 0 | return $value; | |||||||||
| 119 | 6 | ▶ | }, inline_as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 120 | 6 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||
| 121 | return ( | ||||||||||
| 122 | 6 | '$value = do {', | |||||||||
| 123 | $deflators->( $constraint->parent ), | ||||||||||
| 124 | ' };', | ||||||||||
| 125 | 'for( @$value ) {', | ||||||||||
| 126 | ' $_ = do {', | ||||||||||
| 127 | ' my $value = $_;', | ||||||||||
| 128 | ' $value = do {', | ||||||||||
| 129 | $deflators->( $constraint->type_parameter ), | ||||||||||
| 130 | ' };', | ||||||||||
| 131 | ' };', | ||||||||||
| 132 | '}', | ||||||||||
| 133 | '$value', | ||||||||||
| 134 | ); | ||||||||||
| 135 | 6 | }; | |||||||||
| 136 | |||||||||||
| 137 | 0 | ▶ | deflate 'Maybe[]', via { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 138 | 0 | my ( $attr, $constraint, $deflate ) = @_; | |||||||||
| 139 | 0 | return $deflate->( $_, $constraint->type_parameter ); | |||||||||
| 140 | 1 | ▶ | }, inline_as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 141 | 1 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||
| 142 | 1 | return $deflators->( $constraint->type_parameter ); | |||||||||
| 143 | 6 | }; | |||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | inflate 'Maybe[]', via { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 146 | 0 | my ( $attr, $constraint, $inflate ) = @_; | |||||||||
| 147 | 0 | return $inflate->( $_, $constraint->type_parameter ); | |||||||||
| 148 | 1 | ▶ | }, inline_as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 149 | 1 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||
| 150 | 1 | return $deflators->( $constraint->type_parameter ); | |||||||||
| 151 | 6 | }; | |||||||||
| 152 | |||||||||||
| 153 | 0 | ▶ | deflate 'ScalarRef[]', via { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 154 | 0 | my ( $attr, $constraint, $deflate ) = @_; | |||||||||
| 155 | 0 | return ${ $deflate->( $_, $constraint->type_parameter ) }; | |||||||||
| 156 | 5 | ▶ | }, inline_as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 157 | 5 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||
| 158 | 5 | my $parameter = $deflators->( $constraint->type_parameter ); | |||||||||
| 159 | 5 | return ( '$value = do {', $parameter, '};', '$$value' ); | |||||||||
| 160 | 6 | }; | |||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | inflate 'ScalarRef[]', via { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 163 | 0 | my ( $attr, $constraint, $inflate ) = @_; | |||||||||
| 164 | 0 | return \$inflate->( $_, $constraint->type_parameter ); | |||||||||
| 165 | 5 | ▶ | }, inline_as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 166 | 5 | my ( $attr, $constraint, $deflators ) = @_; | |||||||||
| 167 | 5 | my $parameter = $deflators->( $constraint->type_parameter ); | |||||||||
| 168 | 5 | return ( '$value = do {', $parameter, '};', '\$value' ); | |||||||||
| 169 | 6 | }; | |||||||||
| 170 | |||||||||||
| 171 | 6 | 1; | |||||||||
| 172 | |||||||||||
| 173 | |||||||||||
| 174 | |||||||||||
| 175 | =pod | ||||||||||
| 176 | |||||||||||
| 177 | =head1 NAME | ||||||||||
| 178 | |||||||||||
| 179 | MooseX::Attribute::Deflator::Moose - Deflators for Moose type constraints | ||||||||||
| 180 | |||||||||||
| 181 | =head1 VERSION | ||||||||||
| 182 | |||||||||||
| 183 | version 2.2.2 | ||||||||||
| 184 | |||||||||||
| 185 | =head1 SYNOPSIS | ||||||||||
| 186 | |||||||||||
| 187 | use MooseX::Attribute::Deflator::Moose; | ||||||||||
| 188 | |||||||||||
| 189 | =head1 DESCRIPTION | ||||||||||
| 190 | |||||||||||
| 191 | Using this module registers sane type deflators and inflators for Moose's built in types. | ||||||||||
| 192 | |||||||||||
| 193 | Some notes: | ||||||||||
| 194 | |||||||||||
| 195 | =over | ||||||||||
| 196 | |||||||||||
| 197 | =item * HashRef and ArrayRef deflate/inflate using JSON | ||||||||||
| 198 | |||||||||||
| 199 | =item * ScalarRef is dereferenced on deflation and returns a reference on inflation | ||||||||||
| 200 | |||||||||||
| 201 | =back | ||||||||||
| 202 | |||||||||||
| 203 | =head1 AUTHOR | ||||||||||
| 204 | |||||||||||
| 205 | Moritz Onken | ||||||||||
| 206 | |||||||||||
| 207 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 208 | |||||||||||
| 209 | This software is Copyright (c) 2012 by Moritz Onken. | ||||||||||
| 210 | |||||||||||
| 211 | This is free software, licensed under: | ||||||||||
| 212 | |||||||||||
| 213 | The (three-clause) BSD License | ||||||||||
| 214 | |||||||||||
| 215 | =cut | ||||||||||
| 216 | |||||||||||
| 217 | |||||||||||
| 218 | __END__ | ||||||||||