=pod

=encoding utf-8

=for stopwords inlinable

=head1 NAME

Type::Tiny::Manual::UsingWithOther - how to use Type::Tiny and Type::Library with other OO frameworks

=head1 DESCRIPTION

=head2 Class::InsideOut

You want L<Class::InsideOut> 1.13 or above, which has support for blessed
and overloaded objects (including Type::Tiny type constraints) for the
C<get_hook> and C<set_hook> options.

   {
      package Person;
      
      use Class::InsideOut qw( public );
      use Types::Standard qw( Str Int );
      use Type::Utils qw( declare as where inline_as coerce from );
      
      public name => my %_name, {
         set_hook => Str,
      };
      
      my $PositiveInt = declare
         as        Int,
         where     {  $_ > 0  },
         inline_as { "$_ =~ /^[0-9]\$/ and $_ > 0" };
      
      coerce $PositiveInt, from Int, q{ abs $_ };
      
      public age => my %_age, {
         set_hook => sub { $_ = $PositiveInt->assert_coerce($_) },
      };
      
      sub get_older {
         my $self = shift;
         my ($years) = @_;
         $PositiveInt->assert_valid($years);
         $self->_set_age($self->age + $years);
      }
   }

I probably need to make coercions a little prettier.

B<< See also: >> C<< t/25_accessor_hooks_typetiny.t >> and
C<< t/Object/HookedTT.pm >> in the Class::InsideOut test suite.

=head2 Params::Check and Object::Accessor

The Params::Check C<< allow() >> function, the C<allow> option for the
Params::Check C<< check() >> function, and the input validation mechanism
for Object::Accessor all work in the same way, which is basically a
limited pure-Perl implementation of the smart match operator. While this
doesn't directly support Type::Tiny constraints, it does support coderefs.
You can use Type::Tiny's C<compiled_check> method to obtain a suitable
coderef.

B<< L<Param::Check> example: >>

   my $tmpl = {
      name => { allow => Str->compiled_check },
      age  => { allow => Int->compiled_check },
   };
   check($tmpl, { name => "Bob", age => 32 })
      or die Params::Check::last_error();

B<< L<Object::Accessor> example: >>

   my $obj = Object::Accessor->new;
   $obj->mk_accessors(
      { name => Str->compiled_check },
      { age  => Int->compiled_check },
   );

Caveat: Object::Accessor doesn't die when a value fails to meet its type
constraint; instead it outputs a warning to STDERR. This behaviour can
be changed by setting C<< $Object::Accessor::FATAL = 1 >>.

=head2 Validation::Class

You want L<Validation::Class> 7.900017 or above.

The C<to_TypeTiny> function from L<Types::TypeTiny> can be used to create
a Type::Tiny type constraint from a Validation::Class::Simple object (and
probably from Validation::Class, but this is untested).

   use Types::TypeTiny qw( to_TypeTiny );
   use Validation::Class::Simple;
   
   my $type = to_TypeTiny Validation::Class::Simple->new(
      fields => {
         name => {
            required => 1,
            pattern  => qr{^\w+(\s\w+)*$},
            filters  => ["trim", "strip"],
         },
         email => { required => 1, email => 1 },
         pass  => { required => 1, min_length => 6 },
      },
   );
   
   # true
   $type->check({
      name   => "Toby Inkster",
      email  => "tobyink@cpan.org",
      pass   => "foobar",
   });
   
   # false
   $type->check({
      name   => "Toby Inkster ",    # trailing whitespace
      email  => "tobyink@cpan.org",
      pass   => "foobar",
   });
   
   # coercion from HashRef uses the filters defined above
   my $fixed = $type->coerce({
      name   => "Toby Inkster ",    # trailing whitespace
      email  => "tobyink@cpan.org",
      pass   => "foobar",
   });
   
   # true
   $type->check($fixed);

Type constraints built with Validation::Class are not inlinable, so won't
be as fast as C<Dict> from L<Types::Standard>, but the filters are a pretty
useful feature. (Note that filters are explicitly I<ignored> for type
constraint checking, and only come into play for coercion.)

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2013 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

=cut
