]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm
0dc4d9d07aba4f77a1dc60bdc9c2dc78331b3b69
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Meta / TypeConstraint / Autocoerce.pm
1 package LaTeX::TikZ::Meta::TypeConstraint::Autocoerce;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Meta::TypeConstraint::Autocoerce - Type constraint metaclass that autoloads type coercions.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use Scalar::Util qw/blessed/;
19
20 use Sub::Name ();
21
22 use Any::Moose;
23 use Any::Moose 'Util' => [ 'find_meta' ];
24
25 extends any_moose('Meta::TypeConstraint');
26
27 =head1 ATTRIBUTES
28
29 =head2 C<mapper>
30
31 =cut
32
33 has 'mapper' => (
34  is  => 'ro',
35  isa => 'CodeRef',
36 );
37
38 =head2 C<parent>
39
40 =cut
41
42 has 'parent' => (
43  is       => 'ro',
44  isa      => any_moose('Meta::TypeConstraint'),
45  required => 1,
46 );
47
48 =head2 C<user_constraint>
49
50 =cut
51
52 has 'user_constraint' => (
53  is       => 'ro',
54  isa      => 'Maybe[CodeRef]',
55  required => 1,
56 );
57
58 =head1 METHODS
59
60 =cut
61
62 around 'new' => sub {
63  my ($orig, $class, %args) = @_;
64
65  unless (exists $args{mapper}) {
66   $args{mapper} = sub { join '::', $_[0]->parent->name, $_[1] };
67  }
68
69  my $parent = delete $args{parent};
70  unless (blessed $parent) {
71   $parent = find_meta($parent)->type_constraint;
72  }
73  __PACKAGE__->meta->find_attribute_by_name('parent')
74                   ->type_constraint->assert_valid($parent);
75  $args{parent} = $parent;
76
77  $args{user_constraint} = delete $args{constraint};
78
79  if (any_moose() eq 'Moose') {
80   $args{coercion} = Moose::Meta::TypeCoercion->new;
81  }
82
83  my $tc;
84  $args{constraint} = Sub::Name::subname('_constraint' => sub {
85   my ($thing) = @_;
86
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
89   # already OK.
90
91   # First, try a possible user defined constraint
92   my $user = $tc->user_constraint;
93   if (defined $user) {
94    my $ok = $user->($thing);
95    return 1 if $ok;
96   }
97
98   # Then, it's valid if and only if it passes the parent type constraint
99   return $tc->parent->check($thing);
100  });
101
102  $tc = $class->$orig(%args);
103 };
104
105 around 'coerce' => sub {
106  my ($orig, $tc, $thing) = @_;
107
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
110  # $orig.
111
112  # First, check whether $thing is already of the right kind.
113  return $thing if $tc->check($thing);
114
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);
121   $pm =~ s{::}{/}g;
122   $pm .= '.pm';
123
124   unless ($INC{$pm}) { # Not loaded yet
125    local $@;
126    eval {
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
130     # the geodyn app.
131     local $SIG{__DIE__};
132     require $pm;
133    };
134   }
135  }
136
137  $tc->$orig($thing);
138 };
139
140 __PACKAGE__->meta->make_immutable(
141  inline_constructor => 0,
142 );
143
144 =head1 AUTHOR
145
146 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
147
148 You can contact me by mail or on C<irc.perl.org> (vincent).
149
150 =head1 BUGS
151
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.
154
155 =head1 SUPPORT
156
157 You can find documentation for this module with the perldoc command.
158
159     perldoc LaTeX::TikZ
160
161 =head1 COPYRIGHT & LICENSE
162
163 Copyright 2010 Vincent Pit, all rights reserved.
164
165 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
166
167 =cut
168
169 1; # End of LaTeX::TikZ::Meta::TypeConstraint::Autocoerce