]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm
Allow passing a type constraint as the parent to LT::Meta::TypeConstraint::Autocoerce
[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('_load' => sub {
85   $tc->load(@_);
86  });
87
88  $tc = $class->$orig(%args);
89 };
90
91 =head2 C<load>
92
93 =cut
94
95 sub load {
96  my ($tc, $thing) = @_;
97
98  # When ->check is called inside coerce, a return value of 0 means that
99  # coercion should take place, while 1 signifies that the value is already
100  # OK.
101
102  # First, try a possible user defined constraint
103  my $user = $tc->user_constraint;
104  if (defined $user) {
105   my $ok = $user->($thing);
106   return 1 if $ok;
107  }
108
109  # Then, try the parent constraint
110  return 1 if $tc->parent->check($thing);
111
112  # If $thing isn't even an object, don't bother trying to coerce it
113  my $class = blessed($thing);
114  return 0 unless defined $class;
115
116  # Find the file to autoload
117  my $mapper = $tc->mapper;
118  my $pm = $class = $tc->$mapper($class);
119  $pm =~ s{::}{/}g;
120  $pm .= '.pm';
121  return 0 if $INC{$pm}; # already loaded
122
123  local $@;
124  eval { require $pm; 1 };
125
126  return 0;
127 }
128
129 around 'coerce' => sub {
130  my ($orig, $tc, $thing) = @_;
131
132  # The original coerce gets an hold onto the type coercions *before* calling
133  # the constraint. Thus, we have to force the loading before recalling into
134  # $orig. This is achieved by calling ->load.
135  return $thing if $tc->load($thing);
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