]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm
b1d85079376c935b12593aedda98822c1dd88e9b
[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.02
13
14 =cut
15
16 our $VERSION = '0.02';
17
18 =head1 SYNOPSIS
19
20     # The target class of the autocoercion (cannot be changed)
21     {
22      package X;
23      use Mouse;
24      has 'id' => (
25       is  => 'ro',
26       isa => 'Int',
27      );
28      use LaTeX::TikZ::Meta::TypeConstraint::Autocoerce;
29      use Mouse::Util::TypeConstraints;
30      register_type_constraint(
31       LaTeX::TikZ::Meta::TypeConstraint::Autocoerce->new(
32        name   => 'X::Autocoerce',
33        target => find_type_constraint(__PACKAGE__),
34        mapper => sub { join '::', __PACKAGE__, 'From', $_[1] },
35       );
36      );
37      __PACKAGE__->meta->make_immutable;
38     }
39
40     # The class that does the coercion (cannot be changed)
41     {
42      package Y;
43      use Mouse;
44      has 'x' => (
45       is      => 'ro',
46       isa     => 'X::Autocoerce',
47       coerce  => 1,
48       handles => [ 'id' ],
49      );
50      __PACKAGE__->meta->make_immutable;
51     }
52
53     # Another class the user wants to use instead of X (cannot be changed)
54     {
55      package Z;
56      use Mouse;
57      has 'id' => (
58       is  => 'ro',
59       isa => 'Num',
60      );
61      __PACKAGE__->meta->make_immutable;
62     }
63
64     # The autocoercion class, defined by the user in X/From/Z.pm
65     {
66      package X::From::Z;
67      use Mouse::Util::TypeConstraints;
68      coerce 'X::Autocoerce'
69          => from 'Z'
70          => via { X->new(id => int $_->id) };
71     }
72
73     my $z = Z->new(id => 123);
74     my $y = Y->new(x => $z);
75     print $y->id; # 123
76
77 =head1 DESCRIPTION
78
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.
81
82 It only supports L<Mouse> currently.
83
84 Note that you will need L<Mouse::Util::TypeConstraints/register_type_constraint> to install this type constraint, which is only available starting L<Mouse> C<0.63>.
85
86 =cut
87
88 use Scalar::Util qw<blessed>;
89
90 use Sub::Name ();
91
92 use LaTeX::TikZ::Tools;
93
94 use Mouse;
95
96 =head1 RELATIONSHIPS
97
98 This class inherits from L<Mouse::Meta::TypeConstraint>.
99
100 =cut
101
102 extends 'Mouse::Meta::TypeConstraint';
103
104 =head1 ATTRIBUTES
105
106 =head2 C<name>
107
108 The name of the type constraint.
109 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
111 This attribute is inherited from the L<Mouse> type constraint metaclass.
112
113 =head2 C<mapper>
114
115 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.
116 It is called with the type constraint object as first argument, followed by the class name.
117
118 =cut
119
120 has 'mapper' => (
121  is       => 'ro',
122  isa      => 'CodeRef',
123  required => 1,
124 );
125
126 =head2 C<target>
127
128 A type constraint that defines into what the objects are going to be coerced.
129 Objects satisfying this type constraint will be automatically considered as valid and will not be coerced.
130 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.
131
132 =cut
133
134 has 'target' => (
135  is       => 'ro',
136  isa      => 'Mouse::Meta::TypeConstraint',
137  required => 1,
138 );
139
140 my $target_tc = __PACKAGE__->meta->find_attribute_by_name('target')
141                                  ->type_constraint;
142
143 =head1 METHODS
144
145 =head2 C<< new name => $name, mapper => $mapper, target => $target >>
146
147 Constructs a type constraint object that will attempt to autocoerce objects that are not valid according to C<$target> by loading the class returned by C<$mapper>.
148
149 =cut
150
151 around 'new' => sub {
152  my ($orig, $class, %args) = @_;
153
154  unless (exists $args{mapper}) {
155   $args{mapper} = sub { join '::', $_[0]->target->name, $_[1] };
156  }
157
158  my $target = delete $args{target};
159  unless (blessed $target) {
160   my $target_name = defined $target ? "target $target" : 'undefined target';
161   $target = LaTeX::TikZ::Tools::type_constraint($target) if defined $target;
162   Carp::confess("No meta object for $target_name")   unless defined $target;
163  }
164  $target_tc->assert_valid($target);
165  $args{target} = $target;
166
167  $args{constraint} = Sub::Name::subname('_constraint' => sub {
168   my ($thing) = @_;
169
170   # Remember that when ->check is called inside coerce, a return value of 0
171   # means that coercion should take place, while 1 signifies that the value is
172   # already OK. Thus we should return true if and only if $thing passes the
173   # target type constraint.
174
175   return $target->check($thing);
176  });
177
178  return $class->$orig(%args);
179 };
180
181 =head2 C<coerce $thing>
182
183 Tries to coerce C<$thing> by first loading a class that might contain a type coercion for it.
184
185 =cut
186
187 around 'coerce' => sub {
188  my ($orig, $tc, $thing) = @_;
189
190  # The original coerce gets an hold onto the type coercions *before* calling
191  # the constraint. Thus, we have to force the loading before recalling into
192  # $orig.
193
194  # First, check whether $thing is already of the right kind.
195  return $thing if $tc->check($thing);
196
197  # If $thing isn't even an object, don't bother trying to autoload a coercion
198  my $class = blessed($thing);
199  if (defined $class) {
200   $class = $tc->mapper->($tc, $class);
201
202   if (defined $class) {
203    # Find the file to autoload
204    (my $pm = $class) =~ s{::}{/}g;
205    $pm .= '.pm';
206
207    unless ($INC{$pm}) { # Not loaded yet
208     local $@;
209     eval {
210      # We die often here, even though we're not really interested in the error.
211      # However, if a die handler is set (e.g. to \&Carp::confess), this can get
212      # very slow. Resetting the handler shows a 10% total time improvement for
213      # the geodyn app.
214      local $SIG{__DIE__};
215      require $pm;
216     };
217    }
218   }
219  }
220
221  $tc->$orig($thing);
222 };
223
224 __PACKAGE__->meta->make_immutable(
225  inline_constructor => 0,
226 );
227
228 =head1 SEE ALSO
229
230 L<Mouse::Meta::TypeConstraint>.
231
232 =head1 AUTHOR
233
234 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
235
236 You can contact me by mail or on C<irc.perl.org> (vincent).
237
238 =head1 BUGS
239
240 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>.
241 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
242
243 =head1 SUPPORT
244
245 You can find documentation for this module with the perldoc command.
246
247     perldoc LaTeX::TikZ
248
249 =head1 COPYRIGHT & LICENSE
250
251 Copyright 2010,2011,2012,2013,2014,2015 Vincent Pit, all rights reserved.
252
253 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
254
255 =cut
256
257 1; # End of LaTeX::TikZ::Meta::TypeConstraint::Autocoerce