]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blobdiff - lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm
Make sure POD headings are linkable
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Meta / TypeConstraint / Autocoerce.pm
index 00fbbf4a547ac485aa787a15defb9fdc92d651d2..28ca7067a1135f308f18d6234eb49bea2a0d7a92 100644 (file)
@@ -20,13 +20,13 @@ our $VERSION = '0.02';
     # The target class of the autocoercion (cannot be changed)
     {
      package X;
-     use Any::Moose;
+     use Mouse;
      has 'id' => (
       is  => 'ro',
       isa => 'Int',
      );
      use LaTeX::TikZ::Meta::TypeConstraint::Autocoerce;
-     use Any::Moose 'Util::TypeConstraints';
+     use Mouse::Util::TypeConstraints;
      register_type_constraint(
       LaTeX::TikZ::Meta::TypeConstraint::Autocoerce->new(
        name   => 'X::Autocoerce',
@@ -40,7 +40,7 @@ our $VERSION = '0.02';
     # The class that does the coercion (cannot be changed)
     {
      package Y;
-     use Any::Moose;
+     use Mouse;
      has 'x' => (
       is      => 'ro',
       isa     => 'X::Autocoerce',
@@ -53,7 +53,7 @@ our $VERSION = '0.02';
     # Another class the user wants to use instead of X (cannot be changed)
     {
      package Z;
-     use Any::Moose;
+     use Mouse;
      has 'id' => (
       is  => 'ro',
       isa => 'Num',
@@ -64,7 +64,7 @@ our $VERSION = '0.02';
     # The autocoercion class, defined by the user in X/From/Z.pm
     {
      package X::From::Z;
-     use Any::Moose 'Util::TypeConstraints';
+     use Mouse::Util::TypeConstraints;
      coerce 'X::Autocoerce'
          => from 'Z'
          => via { X->new(id => int $_->id) };
@@ -79,9 +79,9 @@ our $VERSION = '0.02';
 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.
 This allows you to declare types that can be replaced (through coercion) at the end user's discretion.
 
-It works with both L<Moose> and L<Mouse> by using L<Any::Moose>.
+It only supports L<Mouse> currently.
 
-Note that you will need L<Moose::Util::TypeConstraints/register_type_constraint> or L<Mouse::Util::TypeConstraints/register_type_constraint> to install this type constraint, and that the latter is only available starting L<Mouse> C<0.63>.
+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>.
 
 =cut
 
@@ -91,15 +91,15 @@ use Sub::Name ();
 
 use LaTeX::TikZ::Tools;
 
-use Any::Moose;
+use Mouse;
 
 =head1 RELATIONSHIPS
 
-This class inherits from L<Moose::Meta::TypeConstraint> or L<Mouse::Meta::TypeConstraint>, depending on which mode L<Any::Moose> runs.
+This class inherits from L<Mouse::Meta::TypeConstraint>.
 
 =cut
 
-extends any_moose('Meta::TypeConstraint');
+extends 'Mouse::Meta::TypeConstraint';
 
 =head1 ATTRIBUTES
 
@@ -108,7 +108,7 @@ extends any_moose('Meta::TypeConstraint');
 The name of the type constraint.
 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.
 
-This attribute is inherited from the L<Moose> or L<Mouse> type constraint metaclass.
+This attribute is inherited from the L<Mouse> type constraint metaclass.
 
 =head2 C<mapper>
 
@@ -133,27 +133,22 @@ If it is given as a plain string, then a type constraint with the same name is s
 
 has 'target' => (
  is       => 'ro',
- isa      => any_moose('Meta::TypeConstraint'),
+ isa      => 'Mouse::Meta::TypeConstraint',
  required => 1,
 );
 
 my $target_tc = __PACKAGE__->meta->find_attribute_by_name('target')
                                  ->type_constraint;
 
-=head2 C<user_constraint>
-
-An optional user defined code reference which predates checking the target for validity.
-
-=cut
-
-has 'user_constraint' => (
- is  => 'ro',
- isa => 'Maybe[CodeRef]',
-);
-
 =head1 METHODS
 
-=head2 C<< new name => $name, mapper => $mapper, target => $target, [ user_constraint => sub { ... } ] >>
+=head2 C<new>
+
+    my $tc = LaTeX::TikZ::Meta::TypeConstraint::Autocoerce->new(
+     name   => $name,
+     mapper => $mapper,
+     target => $target,
+    );
 
 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>.
 
@@ -175,33 +170,23 @@ around 'new' => sub {
  $target_tc->assert_valid($target);
  $args{target} = $target;
 
- if (any_moose() eq 'Moose') {
-  $args{coercion} = Moose::Meta::TypeCoercion->new;
- }
-
- my $tc;
  $args{constraint} = Sub::Name::subname('_constraint' => sub {
   my ($thing) = @_;
 
   # Remember that 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.
+  # already OK. Thus we should return true if and only if $thing passes the
+  # target type constraint.
 
-  # First, try a possible user defined constraint
-  my $user = $tc->user_constraint;
-  if (defined $user) {
-   my $ok = $user->($thing);
-   return 1 if $ok;
-  }
-
-  # Then, it's valid if and only if it passes the target type constraint
-  return $tc->target->check($thing);
+  return $target->check($thing);
  });
 
$tc = $class->$orig(%args);
return $class->$orig(%args);
 };
 
-=head2 C<coerce $thing>
+=head2 C<coerce>
+
+    $tc->coerce($thing)
 
 Tries to coerce C<$thing> by first loading a class that might contain a type coercion for it.
 
@@ -250,7 +235,7 @@ __PACKAGE__->meta->make_immutable(
 
 =head1 SEE ALSO
 
-L<Moose::Meta::TypeConstraint>, L<Mouse::Meta::TypeConstraint>.
+L<Mouse::Meta::TypeConstraint>.
 
 =head1 AUTHOR
 
@@ -271,7 +256,7 @@ You can find documentation for this module with the perldoc command.
 
 =head1 COPYRIGHT & LICENSE
 
-Copyright 2010 Vincent Pit, all rights reserved.
+Copyright 2010,2011,2012,2013,2014,2015 Vincent Pit, all rights reserved.
 
 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.