1 package LaTeX::TikZ::Meta::TypeConstraint::Autocoerce;
8 LaTeX::TikZ::Meta::TypeConstraint::Autocoerce - Type constraint metaclass that autoloads type coercions.
16 our $VERSION = '0.01';
18 use Scalar::Util qw/blessed/;
23 use Any::Moose 'Util' => [ 'find_meta' ];
25 extends any_moose('Meta::TypeConstraint');
31 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.
32 It is called with the type constraint object as first argument, followed by the class name.
48 isa => any_moose('Meta::TypeConstraint'),
52 =head2 C<user_constraint>
56 has 'user_constraint' => (
58 isa => 'Maybe[CodeRef]',
67 my ($orig, $class, %args) = @_;
69 unless (exists $args{mapper}) {
70 $args{mapper} = sub { join '::', $_[0]->parent->name, $_[1] };
73 my $parent = delete $args{parent};
74 unless (blessed $parent) {
75 $parent = find_meta($parent)->type_constraint;
77 __PACKAGE__->meta->find_attribute_by_name('parent')
78 ->type_constraint->assert_valid($parent);
79 $args{parent} = $parent;
81 $args{user_constraint} = delete $args{constraint};
83 if (any_moose() eq 'Moose') {
84 $args{coercion} = Moose::Meta::TypeCoercion->new;
88 $args{constraint} = Sub::Name::subname('_constraint' => sub {
91 # Remember that when ->check is called inside coerce, a return value of 0
92 # means that coercion should take place, while 1 signifies that the value is
95 # First, try a possible user defined constraint
96 my $user = $tc->user_constraint;
98 my $ok = $user->($thing);
102 # Then, it's valid if and only if it passes the parent type constraint
103 return $tc->parent->check($thing);
106 $tc = $class->$orig(%args);
109 around 'coerce' => sub {
110 my ($orig, $tc, $thing) = @_;
112 # The original coerce gets an hold onto the type coercions *before* calling
113 # the constraint. Thus, we have to force the loading before recalling into
116 # First, check whether $thing is already of the right kind.
117 return $thing if $tc->check($thing);
119 # If $thing isn't even an object, don't bother trying to autoload a coercion
120 my $class = blessed($thing);
121 if (defined $class) {
122 $class = $tc->mapper->($tc, $class);
124 if (defined $class) {
125 # Find the file to autoload
126 (my $pm = $class) =~ s{::}{/}g;
129 unless ($INC{$pm}) { # Not loaded yet
132 # We die often here, even though we're not really interested in the error.
133 # However, if a die handler is set (e.g. to \&Carp::confess), this can get
134 # very slow. Resetting the handler shows a 10% total time improvement for
146 __PACKAGE__->meta->make_immutable(
147 inline_constructor => 0,
152 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
154 You can contact me by mail or on C<irc.perl.org> (vincent).
158 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>.
159 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
163 You can find documentation for this module with the perldoc command.
167 =head1 COPYRIGHT & LICENSE
169 Copyright 2010 Vincent Pit, all rights reserved.
171 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
175 1; # End of LaTeX::TikZ::Meta::TypeConstraint::Autocoerce