Changeset 25424
- Timestamp:
- 11/30/08 18:14:50 (4 years ago)
- Location:
- lang/perl/Shika/trunk
- Files:
-
- 5 modified
-
lib/Shika.pm (modified) (5 diffs)
-
lib/Shika/Devel.pm (modified) (3 diffs)
-
lib/Shika/Devel/TypeConstraints.pm (modified) (4 diffs)
-
t/010_core/08_coerce.t (modified) (1 diff)
-
tools/accessor_installer_generator.pl (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Shika/trunk/lib/Shika.pm
r25423 r25424 73 73 next unless exists $args->{$name}; 74 74 next unless exists $has->{coerce} && $has->{coerce}; 75 Carp::croak "Shika requires CODE for cerce" unless ref $has->{coerce} eq 'CODE';76 $args->{$name} = $has->{coerce}->($args->{$name});75 die "does not pass the type constraint because: Validation failed for '$has->{isa}' failed $name" 76 unless Shika::Devel::TypeConstraints::check_valid($has, undef, $args->{$name}); 77 77 } 78 78 # process 'required' … … 98 98 sub add_attribute { 99 99 my ($target, $name, $attr) = @_; 100 101 die "You cannot have coercion without specifying a type constraint on attribute ($name)" 102 if $attr->{coerce} && !$attr->{isa}; 100 103 101 104 no strict 'refs'; … … 159 162 } 160 163 164 if (exists $has->{isa}) { 165 die "does not pass the type constraint because: Validation failed for '$has->{isa}' failed $_[0]" 166 unless Shika::Devel::TypeConstraints::check_valid($has, @_); 167 } 168 161 169 if (@_==2) { 162 return $_[0]->{$n} = $ has->{coerce}->($_[1]);170 return $_[0]->{$n} = $_[1]; 163 171 } 164 172 shift->{$n} = \@_; … … 197 205 } 198 206 207 if (exists $has->{isa}) { 208 die "does not pass the type constraint because: Validation failed for '$has->{isa}' failed $_[0]" 209 unless Shika::Devel::TypeConstraints::check_valid($has, @_); 210 } 211 199 212 if (@_==2) { 200 return $_[0]->{$n} = $ has->{coerce}->($_[1]);213 return $_[0]->{$n} = $_[1]; 201 214 } 202 215 shift->{$n} = \@_; … … 237 250 } 238 251 252 if (exists $has->{isa}) { 253 die "does not pass the type constraint because: Validation failed for '$has->{isa}' failed $_[0]" 254 unless Shika::Devel::TypeConstraints::check_valid($has, @_); 255 } 256 239 257 if (@_==2) { 240 return $_[0]->{$n} = $ has->{coerce}->($_[1]);258 return $_[0]->{$n} = $_[1]; 241 259 } 242 260 shift->{$n} = \@_; -
lang/perl/Shika/trunk/lib/Shika/Devel.pm
r25423 r25424 64 64 65 65 if (@_==2) { 66 return $_[0]->{$n} = $ has->{coerce}->($_[1]);66 return $_[0]->{$n} = $_[1]; 67 67 } 68 68 shift->{$n} = \@_; … … 112 112 113 113 if (@_==2) { 114 return $_[0]->{$n} = $ has->{coerce}->($_[1]);114 return $_[0]->{$n} = $_[1]; 115 115 } 116 116 shift->{$n} = \@_; … … 162 162 163 163 if (@_==2) { 164 return $_[0]->{$n} = $ has->{coerce}->($_[1]);164 return $_[0]->{$n} = $_[1]; 165 165 } 166 166 shift->{$n} = \@_; -
lang/perl/Shika/trunk/lib/Shika/Devel/TypeConstraints.pm
r25412 r25424 2 2 use strict; 3 3 use warnings; 4 5 my @DEFAULT_TYPES = qw( Any Bool Maybe Undef Defined Value Num Int Str ClassName Ref ScalarRef ArrayRef HashRef CodeRef GlobeRef FileHandle Object Role ); 4 6 5 7 sub import { … … 9 11 no strict 'refs'; 10 12 *{"$caller\::subtype"} = \&_subtype; 13 *{"$caller\::coerce"} = \&_coerce; 11 14 } 12 15 … … 15 18 my($name, $code) = @_; 16 19 $SUBTYPE->{$name} = $code; 20 } 21 22 my $COERCE = +{}; 23 sub _coerce { 24 my($name, $conf) = @_; 25 $COERCE->{$name} = $conf; 26 } 27 28 sub _apply_coerce { 29 my $isa = shift; 30 31 my @types = ( $isa ); 32 if ($isa =~ /\|/) { # or 33 $isa =~ s/\s//g; 34 for my $type (split /\|/, $isa) { 35 push @types, $type; 36 } 37 } 38 39 my $arg_type; 40 while (my($type, $code) = each %{ $SUBTYPE }) { 41 next unless $code->(@_); 42 $arg_type = $type; 43 last; 44 } 45 unless ($arg_type) { 46 for my $type (reverse @DEFAULT_TYPES) { 47 next unless eval { _check($type, @_) }; 48 $arg_type = $type; 49 last; 50 } 51 } 52 53 for my $type (@types) { 54 next unless defined $COERCE->{$type}; 55 next unless defined $COERCE->{$type}->{$arg_type}; 56 $COERCE->{$type}->{$arg_type}->(@_); 57 return _check($isa, @_); 58 } 17 59 } 18 60 … … 24 66 return 1 unless $isa; 25 67 return 1 if @_ > 1 && $isa eq 'ARRAY'; 26 return _check($isa, @_);68 _check($isa, @_) ? 1 : $has->{coerce} ? _apply_coerce($isa, @_) : 0; 27 69 } 28 70 -
lang/perl/Shika/trunk/t/010_core/08_coerce.t
r25104 r25424 12 12 package Response; 13 13 use Shika; 14 use Shika::Devel::TypeConstraints; 15 16 subtype 'Headers' => sub { defined $_[0] && eval { $_[0]->isa('Headers') } }; 17 coerce 'Headers' => +{ 18 HashRef => sub { 19 $_[0] = Headers->new(%{ $_[0] }); 20 }, 21 }; 22 14 23 has headers => ( 15 coerce => sub { 16 my $param = shift; 17 if (ref $param eq 'HASH') { 18 Headers->new(%$param) 19 } else { 20 $param 21 } 22 }, 24 isa => 'Headers', 25 coerce => 1, 23 26 ); 24 27 } 25 28 26 my $r = Response->new(headers => { foo => 'bar' });29 my $r = Response->new(headers => { foo => 'bar' }); 27 30 is($r->headers->foo, 'bar'); 28 31 $r->headers({foo => 'yay'}); -
lang/perl/Shika/trunk/tools/accessor_installer_generator.pl
r25423 r25424 32 32 } 33 33 34 [% IF debug -%]34 [% IF debug || coerce -%] 35 35 if (exists $has->{isa}) { 36 36 die "does not pass the type constraint because: Validation failed for '$has->{isa}' failed $_[0]" … … 40 40 [% END -%] 41 41 if (@_==2) { [%# setter %] 42 [% IF coerce -%]43 return $_[0]->{$n} = $has->{coerce}->($_[1]);44 [% ELSE -%]45 42 return $_[0]->{$n} = $_[1]; 46 [% END -%]47 43 } 48 44 shift->{$n} = \@_;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)