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.
47 isa => any_moose('Meta::TypeConstraint'),
51 =head2 C<user_constraint>
55 has 'user_constraint' => (
57 isa => 'Maybe[CodeRef]',
66 my ($orig, $class, %args) = @_;
68 unless (exists $args{mapper}) {
69 $args{mapper} = sub { join '::', $_[0]->parent->name, $_[1] };
72 my $parent = delete $args{parent};
73 unless (blessed $parent) {
74 $parent = find_meta($parent)->type_constraint;
76 __PACKAGE__->meta->find_attribute_by_name('parent')
77 ->type_constraint->assert_valid($parent);
78 $args{parent} = $parent;
80 $args{user_constraint} = delete $args{constraint};
82 if (any_moose() eq 'Moose') {
83 $args{coercion} = Moose::Meta::TypeCoercion->new;
87 $args{constraint} = Sub::Name::subname('_constraint' => sub {
90 # Remember that when ->check is called inside coerce, a return value of 0
91 # means that coercion should take place, while 1 signifies that the value is
94 # First, try a possible user defined constraint
95 my $user = $tc->user_constraint;
97 my $ok = $user->($thing);
101 # Then, it's valid if and only if it passes the parent type constraint
102 return $tc->parent->check($thing);
105 $tc = $class->$orig(%args);
108 around 'coerce' => sub {
109 my ($orig, $tc, $thing) = @_;
111 # The original coerce gets an hold onto the type coercions *before* calling
112 # the constraint. Thus, we have to force the loading before recalling into
115 # First, check whether $thing is already of the right kind.
116 return $thing if $tc->check($thing);
118 # If $thing isn't even an object, don't bother trying to autoload a coercion
119 my $class = blessed($thing);
120 if (defined $class) {
121 $class = $tc->mapper->($tc, $class);
123 if (defined $class) {
124 # Find the file to autoload
125 (my $pm = $class) =~ s{::}{/}g;
128 unless ($INC{$pm}) { # Not loaded yet
131 # We die often here, even though we're not really interested in the error.
132 # However, if a die handler is set (e.g. to \&Carp::confess), this can get
133 # very slow. Resetting the handler shows a 10% total time improvement for
145 __PACKAGE__->meta->make_immutable(
146 inline_constructor => 0,
151 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
153 You can contact me by mail or on C<irc.perl.org> (vincent).
157 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>.
158 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
162 You can find documentation for this module with the perldoc command.
166 =head1 COPYRIGHT & LICENSE
168 Copyright 2010 Vincent Pit, all rights reserved.
170 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
174 1; # End of LaTeX::TikZ::Meta::TypeConstraint::Autocoerce