1 package LaTeX::TikZ::Meta::TypeConstraint::Autocoerce;
8 LaTeX::TikZ::Meta::TypeConstraint::Autocoerce - Type constraint metaclass that autoloads type coercions.
16 our $VERSION = '0.02';
20 # The target class of the autocoercion (cannot be changed)
28 use LaTeX::TikZ::Meta::TypeConstraint::Autocoerce;
29 use Any::Moose 'Util::TypeConstraints';
30 register_type_constraint(
31 LaTeX::TikZ::Meta::TypeConstraint::Autocoerce->new(
32 name => 'X::Autocoerce',
33 parent => find_type_constraint(__PACKAGE__),
34 mapper => sub { join '::', __PACKAGE__, 'From', $_[1] },
37 __PACKAGE__->meta->make_immutable;
40 # The class that does the coercion (cannot be changed)
46 isa => 'X::Autocoerce',
50 __PACKAGE__->meta->make_immutable;
53 # Another class the user wants to use instead of X (cannot be changed)
61 __PACKAGE__->meta->make_immutable;
64 # The autocoercion class, defined by the user in X/From/Z.pm
67 use Any::Moose 'Util::TypeConstraints';
68 coerce 'X::Autocoerce'
70 => via { X->new(id => int $_->id) };
73 my $z = Z->new(id => 123);
74 my $y = Y->new(x => $z);
79 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.
80 This allows you to declare types that can be replaced (through coercion) at the end user's discretion.
82 It works with both L<Moose> and L<Mouse> by using L<Any::Moose>.
84 Note that you will need L<Moose::Util::TypeConstraints/register_type_constraint> or L<Mouse::Util::TypeConstraints/register_type_constraint> to install this type constraint, and that the latter is only available starting L<Mouse> C<0.63>.
88 use Scalar::Util qw/blessed/;
93 use Any::Moose 'Util' => [ 'find_meta' ];
97 This class inherits from L<Moose::Meta::TypeConstraint> or L<Mouse::Meta::TypeConstraint>, depending on which mode L<Any::Moose> runs.
101 extends any_moose('Meta::TypeConstraint');
107 The name of the type constraint.
108 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.
110 This attribute is inherited from the L<Moose> or L<Mouse> type constraint metaclass.
114 A code reference that maps an object class name to the name of the package in which the coercion can be found, or C<undef> to disable coercion for this class name.
115 It is called with the type constraint object as first argument, followed by the class name.
127 A type constraint that defines which objects are already valid and do not need to be coerced.
128 This is somewhat different from L<Moose::Meta::TypeConstraint/parent>.
129 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.
135 isa => any_moose('Meta::TypeConstraint'),
139 =head2 C<user_constraint>
141 An optional user defined code reference which predates checking the parent for validity.
145 has 'user_constraint' => (
147 isa => 'Maybe[CodeRef]',
152 =head2 C<< new name => $name, mapper => $mapper, parent => $parent, [ user_constraint => sub { ... } ] >>
154 Constructs a type constraint object that will attempt to autocoerce objects that are not valid according to C<$parent> by loading the class returned by C<$mapper>.
158 around 'new' => sub {
159 my ($orig, $class, %args) = @_;
161 unless (exists $args{mapper}) {
162 $args{mapper} = sub { join '::', $_[0]->parent->name, $_[1] };
165 my $parent = delete $args{parent};
166 unless (defined $parent and blessed $parent) {
167 $parent = find_meta($parent);
168 Carp::confess("No meta object for parent $parent");
169 $parent = $parent->type_constraint;
171 __PACKAGE__->meta->find_attribute_by_name('parent')
172 ->type_constraint->assert_valid($parent);
173 $args{parent} = $parent;
175 if (any_moose() eq 'Moose') {
176 $args{coercion} = Moose::Meta::TypeCoercion->new;
180 $args{constraint} = Sub::Name::subname('_constraint' => sub {
183 # Remember that when ->check is called inside coerce, a return value of 0
184 # means that coercion should take place, while 1 signifies that the value is
187 # First, try a possible user defined constraint
188 my $user = $tc->user_constraint;
190 my $ok = $user->($thing);
194 # Then, it's valid if and only if it passes the parent type constraint
195 return $tc->parent->check($thing);
198 $tc = $class->$orig(%args);
201 =head2 C<coerce $thing>
203 Tries to coerce C<$thing> by first loading a class that might contain a type coercion for it.
207 around 'coerce' => sub {
208 my ($orig, $tc, $thing) = @_;
210 # The original coerce gets an hold onto the type coercions *before* calling
211 # the constraint. Thus, we have to force the loading before recalling into
214 # First, check whether $thing is already of the right kind.
215 return $thing if $tc->check($thing);
217 # If $thing isn't even an object, don't bother trying to autoload a coercion
218 my $class = blessed($thing);
219 if (defined $class) {
220 $class = $tc->mapper->($tc, $class);
222 if (defined $class) {
223 # Find the file to autoload
224 (my $pm = $class) =~ s{::}{/}g;
227 unless ($INC{$pm}) { # Not loaded yet
230 # We die often here, even though we're not really interested in the error.
231 # However, if a die handler is set (e.g. to \&Carp::confess), this can get
232 # very slow. Resetting the handler shows a 10% total time improvement for
244 __PACKAGE__->meta->make_immutable(
245 inline_constructor => 0,
250 L<Moose::Meta::TypeConstraint>, L<Mouse::Meta::TypeConstraint>.
254 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
256 You can contact me by mail or on C<irc.perl.org> (vincent).
260 Please report any bugs or feature requests to C<bug-latex-tikz at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-TikZ>.
261 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
265 You can find documentation for this module with the perldoc command.
269 =head1 COPYRIGHT & LICENSE
271 Copyright 2010 Vincent Pit, all rights reserved.
273 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
277 1; # End of LaTeX::TikZ::Meta::TypeConstraint::Autocoerce