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