X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FLaTeX%2FTikZ%2FMeta%2FTypeConstraint%2FAutocoerce.pm;h=25f3879a6fcb39f57b573eb86d40173e51807f1c;hb=ecb1d1524df9ef8b8e0452d0de3efc871c3855e8;hp=0dc4d9d07aba4f77a1dc60bdc9c2dc78331b3b69;hpb=a721d9bb9525aca6118c567455adfc092ea6bba0;p=perl%2Fmodules%2FLaTeX-TikZ.git diff --git a/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm b/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm index 0dc4d9d..25f3879 100644 --- a/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm +++ b/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm @@ -9,37 +9,129 @@ LaTeX::TikZ::Meta::TypeConstraint::Autocoerce - Type constraint metaclass that a =head1 VERSION -Version 0.01 +Version 0.02 =cut -our $VERSION = '0.01'; +our $VERSION = '0.02'; + +=head1 SYNOPSIS + + # The target class of the autocoercion (cannot be changed) + { + package X; + use Any::Moose; + has 'id' => ( + is => 'ro', + isa => 'Int', + ); + use LaTeX::TikZ::Meta::TypeConstraint::Autocoerce; + use Any::Moose 'Util::TypeConstraints'; + register_type_constraint( + LaTeX::TikZ::Meta::TypeConstraint::Autocoerce->new( + name => 'X::Autocoerce', + target => find_type_constraint(__PACKAGE__), + mapper => sub { join '::', __PACKAGE__, 'From', $_[1] }, + ); + ); + __PACKAGE__->meta->make_immutable; + } + + # The class that does the coercion (cannot be changed) + { + package Y; + use Any::Moose; + has 'x' => ( + is => 'ro', + isa => 'X::Autocoerce', + coerce => 1, + handles => [ 'id' ], + ); + __PACKAGE__->meta->make_immutable; + } + + # Another class the user wants to use instead of X (cannot be changed) + { + package Z; + use Any::Moose; + has 'id' => ( + is => 'ro', + isa => 'Num', + ); + __PACKAGE__->meta->make_immutable; + } + + # The autocoercion class, defined by the user in X/From/Z.pm + { + package X::From::Z; + use Any::Moose 'Util::TypeConstraints'; + coerce 'X::Autocoerce' + => from 'Z' + => via { X->new(id => int $_->id) }; + } + + my $z = Z->new(id => 123); + my $y = Y->new(x => $z); + print $y->id; # 123 + +=head1 DESCRIPTION + +When a type coercion is attempted, this type constraint metaclass tries to autoload a specific module which is supposed to contain the actual coercion code. +This allows you to declare types that can be replaced (through coercion) at the end user's discretion. + +It works with both L and L by using L. + +Note that you will need L or L to install this type constraint, and that the latter is only available starting L C<0.63>. -use Scalar::Util qw/blessed/; +=cut + +use Scalar::Util qw; use Sub::Name (); +use LaTeX::TikZ::Tools; + use Any::Moose; -use Any::Moose 'Util' => [ 'find_meta' ]; + +=head1 RELATIONSHIPS + +This class inherits from L or L, depending on which mode L runs. + +=cut extends any_moose('Meta::TypeConstraint'); =head1 ATTRIBUTES +=head2 C + +The name of the type constraint. +This must be the target of both the classes that want to use the autocoercion feature and the user defined coercions in the autoloaded classes. + +This attribute is inherited from the L or L type constraint metaclass. + =head2 C +A code reference that maps an object class name to the name of the package in which the coercion can be found, or C to disable coercion for this class name. +It is called with the type constraint object as first argument, followed by the class name. + =cut has 'mapper' => ( - is => 'ro', - isa => 'CodeRef', + is => 'ro', + isa => 'CodeRef', + required => 1, ); -=head2 C +=head2 C + +A type constraint that defines into what the objects are going to be coerced. +Objects satisfying this type constraint will be automatically considered as valid and will not be coerced. +If it is given as a plain string, then a type constraint with the same name is searched for in the global type constraint registry. =cut -has 'parent' => ( +has 'target' => ( is => 'ro', isa => any_moose('Meta::TypeConstraint'), required => 1, @@ -47,34 +139,39 @@ has 'parent' => ( =head2 C +An optional user defined code reference which predates checking the target for validity. + =cut has 'user_constraint' => ( - is => 'ro', - isa => 'Maybe[CodeRef]', - required => 1, + is => 'ro', + isa => 'Maybe[CodeRef]', ); =head1 METHODS +=head2 C<< new name => $name, mapper => $mapper, target => $target, [ user_constraint => sub { ... } ] >> + +Constructs a type constraint object that will attempt to autocoerce objects that are not valid according to C<$target> by loading the class returned by C<$mapper>. + =cut around 'new' => sub { my ($orig, $class, %args) = @_; unless (exists $args{mapper}) { - $args{mapper} = sub { join '::', $_[0]->parent->name, $_[1] }; + $args{mapper} = sub { join '::', $_[0]->target->name, $_[1] }; } - my $parent = delete $args{parent}; - unless (blessed $parent) { - $parent = find_meta($parent)->type_constraint; + my $target = delete $args{target}; + unless (blessed $target) { + my $target_name = defined $target ? "target $target" : 'undefined target'; + $target = LaTeX::TikZ::Tools::type_constraint($target) if defined $target; + Carp::confess("No meta object for $target_name") unless defined $target; } - __PACKAGE__->meta->find_attribute_by_name('parent') - ->type_constraint->assert_valid($parent); - $args{parent} = $parent; - - $args{user_constraint} = delete $args{constraint}; + __PACKAGE__->meta->find_attribute_by_name('target') + ->type_constraint->assert_valid($target); + $args{target} = $target; if (any_moose() eq 'Moose') { $args{coercion} = Moose::Meta::TypeCoercion->new; @@ -95,13 +192,19 @@ around 'new' => sub { return 1 if $ok; } - # Then, it's valid if and only if it passes the parent type constraint - return $tc->parent->check($thing); + # Then, it's valid if and only if it passes the target type constraint + return $tc->target->check($thing); }); $tc = $class->$orig(%args); }; +=head2 C + +Tries to coerce C<$thing> by first loading a class that might contain a type coercion for it. + +=cut + around 'coerce' => sub { my ($orig, $tc, $thing) = @_; @@ -115,22 +218,24 @@ around 'coerce' => sub { # If $thing isn't even an object, don't bother trying to autoload a coercion my $class = blessed($thing); if (defined $class) { - # Find the file to autoload - my $mapper = $tc->mapper; - my $pm = $class = $tc->$mapper($class); - $pm =~ s{::}{/}g; - $pm .= '.pm'; - - unless ($INC{$pm}) { # Not loaded yet - local $@; - eval { - # We die often here, even though we're not really interested in the error. - # However, if a die handler is set (e.g. to \&Carp::confess), this can get - # very slow. Resetting the handler shows a 10% total time improvement for - # the geodyn app. - local $SIG{__DIE__}; - require $pm; - }; + $class = $tc->mapper->($tc, $class); + + if (defined $class) { + # Find the file to autoload + (my $pm = $class) =~ s{::}{/}g; + $pm .= '.pm'; + + unless ($INC{$pm}) { # Not loaded yet + local $@; + eval { + # We die often here, even though we're not really interested in the error. + # However, if a die handler is set (e.g. to \&Carp::confess), this can get + # very slow. Resetting the handler shows a 10% total time improvement for + # the geodyn app. + local $SIG{__DIE__}; + require $pm; + }; + } } } @@ -141,6 +246,10 @@ __PACKAGE__->meta->make_immutable( inline_constructor => 0, ); +=head1 SEE ALSO + +L, L. + =head1 AUTHOR Vincent Pit, C<< >>, L.