Vincent Pit > LaTeX-TikZ-0.02 > LaTeX::TikZ::Meta::TypeConstraint::Autocoerce

Download:
LaTeX-TikZ-0.02.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

New  2
Open  0
View/Report Bugs
Module Version: 0.02   Source  

NAME ^

LaTeX::TikZ::Meta::TypeConstraint::Autocoerce - Type constraint metaclass that autoloads type coercions.

VERSION ^

Version 0.02

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',
       parent => 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

DESCRIPTION ^

This type constraint metaclass tries to autoload a specific module when a type coercion is attempted, 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 Moose and Mouse by using Any::Moose.

Note that you will need "register_type_constraint" in Moose::Util::TypeConstraints or "register_type_constraint" in Mouse::Util::TypeConstraints to install this type constraint, and that the latter is only available starting Mouse 0.63.

RELATIONSHIPS ^

This class inherits from Moose::Meta::TypeConstraint or Mouse::Meta::TypeConstraint, depending on which mode Any::Moose runs.

ATTRIBUTES ^

name

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 Moose or Mouse type constraint metaclass.

mapper

A code reference that maps an object class name to the name of the package in which the coercion can be found, or undef to disable coercion for this class name. It is called with the type constraint object as first argument, followed by the class name.

parent

A type constraint that defines which objects are already valid and do not need to be coerced. This is somewhat different from "parent" in Moose::Meta::TypeConstraint. 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.

user_constraint

An optional user defined code reference which predates checking the parent for validity.

METHODS ^

new name => $name, mapper => $mapper, parent => $parent, [ user_constraint => sub { ... } ]

Constructs a type constraint object that will attempt to autocoerce objects that are not valid according to $parent by loading the class returned by $mapper.

coerce $thing

Tries to coerce $thing by first loading a class that might contain a type coercion for it.

SEE ALSO ^

Moose::Meta::TypeConstraint, Mouse::Meta::TypeConstraint.

AUTHOR ^

Vincent Pit, <perl at profvince.com>, http://www.profvince.com.

You can contact me by mail or on irc.perl.org (vincent).

BUGS ^

Please report any bugs or feature requests to bug-latex-tikz at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-TikZ. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT ^

You can find documentation for this module with the perldoc command.

    perldoc LaTeX::TikZ

COPYRIGHT & LICENSE ^

Copyright 2010 Vincent Pit, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: