]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blobdiff - lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm
First cut at the documentation
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Meta / TypeConstraint / Autocoerce.pm
index 4706998ef34e0610da97cdbf8da8073b55584ff8..521a473b67c083524d503c32a6e5e40820a14ea6 100644 (file)
@@ -21,23 +21,41 @@ use Any::Moose;
 
 extends any_moose('Meta::TypeConstraint');
 
+=head1 ATTRIBUTES
+
+=head2 C<mapper>
+
+=cut
+
 has 'mapper' => (
  is  => 'ro',
  isa => 'CodeRef',
 );
 
+=head2 C<parent_name>
+
+=cut
+
 has 'parent_name' => (
  is       => 'ro',
  isa      => 'ClassName',
  required => 1,
 );
 
+=head2 C<user_constraint>
+
+=cut
+
 has 'user_constraint' => (
  is       => 'ro',
  isa      => 'Maybe[CodeRef]',
  required => 1,
 );
 
+=head1 METHODS
+
+=cut
+
 around 'new' => sub {
  my ($orig, $class, %args) = @_;
 
@@ -60,47 +78,55 @@ around 'new' => sub {
  $parent_name =~ s/::+/_/g;
 
  my $tc;
- $args{constraint} = Sub::Name::subname("${parent_name}_load" => sub {
-  my ($thing) = @_;
+ $args{constraint} = Sub::Name::subname('_load' => sub {
+  $tc->load(@_);
+ });
+
+ $tc = $class->$orig(%args);
+};
+
+=head2 C<load>
 
-  # First, try a possible user defined constraint
-  my $user = $tc->user_constraint;
-  if (defined $user) {
-   my $ok = $user->($thing);
-   return 1 if $ok;
-  }
+=cut
 
-  # When ->check is called inside coerce, a return value of 0 means that
-  # coercion should take place, while 1 signifies that the value is already
-  # OK.
+sub load {
+ my ($tc, $thing) = @_;
 
-  my $class = blessed($thing);
-  return 0 unless $class;
-  return 1 if     $class->isa($tc->parent_name);
+ # First, try a possible user defined constraint
+ my $user = $tc->user_constraint;
+ if (defined $user) {
+  my $ok = $user->($thing);
+  return 1 if $ok;
+ }
 
-  my $mapper = $tc->mapper;
-  my $pm = $class = $tc->$mapper($class);
+ # When ->check is called inside coerce, a return value of 0 means that
+ # coercion should take place, while 1 signifies that the value is already
+ # OK.
 
 $pm =~ s{::}{/}g;
 $pm .= '.pm';
-  return 0 if $INC{$pm}; # already loaded
my $class = blessed($thing);
return 0 unless $class;
+ return 1 if     $class->isa($tc->parent_name);
 
 local $@;
 eval { require $pm; 1 };
my $mapper = $tc->mapper;
my $pm = $class = $tc->$mapper($class);
 
-  return 0;
- });
+ $pm =~ s{::}{/}g;
+ $pm .= '.pm';
+ return 0 if $INC{$pm}; # already loaded
 
- $tc = $class->$orig(%args);
-};
+ local $@;
+ eval { require $pm; 1 };
+
+ return 0;
+}
 
 around 'coerce' => sub {
  my ($orig, $tc, $thing) = @_;
 
  # The original coerce gets an hold onto the type coercions *before* calling
  # the constraint. Thus, we have to force the loading before recalling into
- # $orig. This is achieved by calling ->check.
- return $thing if $tc->check($thing);
+ # $orig. This is achieved by calling ->load.
+ return $thing if $tc->load($thing);
 
  $tc->$orig($thing);
 };