]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm
9cc2322913697542f680313b7c9bba6aeba995dc
[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 Sub::Name ();
19
20 use Any::Moose;
21
22 extends any_moose('Meta::TypeConstraint');
23
24 has 'mapper' => (
25  is  => 'ro',
26  isa => 'CodeRef',
27 );
28
29 has 'parent_name' => (
30  is       => 'ro',
31  isa      => 'ClassName',
32  required => 1,
33 );
34
35 has 'user_constraint' => (
36  is       => 'ro',
37  isa      => 'Maybe[CodeRef]',
38  required => 1,
39 );
40
41 around 'new' => sub {
42  my ($orig, $class, %args) = @_;
43
44  unless (exists $args{mapper}) {
45   $args{mapper} = sub { join '::', $_[0]->parent_name, $_[1] };
46  }
47
48  my $parent = delete $args{parent};
49  $args{parent_name} = defined $parent
50                       ? (blessed($parent) ? $parent->name : $parent)
51                       : '__ANON__';
52
53  $args{user_constraint} = $args{constraint};
54
55  if (any_moose() eq 'Moose') {
56   $args{coercion} = Moose::Meta::TypeCoercion->new;
57  }
58
59  my $parent_name = $args{parent_name};
60  $parent_name =~ s/::+/_/g;
61
62  my $tc;
63  $args{constraint} = Sub::Name::subname('_load' => sub {
64   $tc->load(@_);
65  });
66
67  $tc = $class->$orig(%args);
68 };
69
70 sub load {
71  my ($tc, $thing) = @_;
72
73  # First, try a possible user defined constraint
74  my $user = $tc->user_constraint;
75  if (defined $user) {
76   my $ok = $user->($thing);
77   return 1 if $ok;
78  }
79
80  # When ->check is called inside coerce, a return value of 0 means that
81  # coercion should take place, while 1 signifies that the value is already
82  # OK.
83
84  my $class = blessed($thing);
85  return 0 unless $class;
86  return 1 if     $class->isa($tc->parent_name);
87
88  my $mapper = $tc->mapper;
89  my $pm = $class = $tc->$mapper($class);
90
91  $pm =~ s{::}{/}g;
92  $pm .= '.pm';
93  return 0 if $INC{$pm}; # already loaded
94
95  local $@;
96  eval { require $pm; 1 };
97
98  return 0;
99 }
100
101 around 'coerce' => sub {
102  my ($orig, $tc, $thing) = @_;
103
104  # The original coerce gets an hold onto the type coercions *before* calling
105  # the constraint. Thus, we have to force the loading before recalling into
106  # $orig. This is achieved by calling ->load.
107  return $thing if $tc->load($thing);
108
109  $tc->$orig($thing);
110 };
111
112 __PACKAGE__->meta->make_immutable(
113  inline_constructor => 0,
114 );
115
116 =head1 AUTHOR
117
118 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
119
120 You can contact me by mail or on C<irc.perl.org> (vincent).
121
122 =head1 BUGS
123
124 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>.
125 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
126
127 =head1 SUPPORT
128
129 You can find documentation for this module with the perldoc command.
130
131     perldoc LaTeX::TikZ
132
133 =head1 COPYRIGHT & LICENSE
134
135 Copyright 2010 Vincent Pit, all rights reserved.
136
137 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
138
139 =cut
140
141 1; # End of LaTeX::TikZ::Meta::TypeConstraint::Autocoerce