File Coverage

blib/lib/Types/UUID.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod n/a
total 37 37 100.0


line stmt bran cond sub pod time code
1 1     1   24056 use 5.008;
  1         2  
  1         32  
2 1     1   3 use strict;
  1         6  
  1         23  
3 1     1   3 use warnings;
  1         7  
  1         59  
4              
5             package Types::UUID;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.004';
9              
10 1     1   458 use Type::Library -base;
  1         13738  
  1         7  
11 1     1   225 use Type::Tiny ();
  1         1  
  1         16  
12 1     1   580 use Types::Standard qw( Undef Str InstanceOf );
  1         33994  
  1         10  
13 1     1   1196 use UUID::Tiny qw( :std );
  1         18424  
  1         366  
14              
15             our @EXPORT = qw( Uuid );
16              
17             {
18             package #
19             Types::UUID::_Constraint;
20             our @ISA = "Type::Tiny";
21             sub generate {
22 1     1   10308 shift;
23 1         4 UUID::Tiny::create_uuid_as_string(@_);
24             }
25             sub generator {
26 1     1   362 shift;
27 1         3 my @args = @_;
28 1     1   3 sub { UUID::Tiny::create_uuid_as_string(@args) }
29 1         5 }
30             }
31              
32             my $type = __PACKAGE__->add_type(
33             'Types::UUID::_Constraint'->new(
34             name => 'Uuid',
35             parent => Str,
36             constraint => \&is_uuid_string,
37             inlined => sub {
38             return (
39             Str->inline_check($_),
40             "UUID::Tiny::is_uuid_string($_)",
41             );
42             },
43             ),
44             );
45              
46             $type->coercion->add_type_coercions(
47             Undef ,=> q[UUID::Tiny::create_uuid_as_string()],
48             Str ,=> q[eval{ UUID::Tiny::uuid_to_string(UUID::Tiny::string_to_uuid($_)) }],
49             InstanceOf['URI'] ,=> q[eval{ UUID::Tiny::uuid_to_string(UUID::Tiny::string_to_uuid(q().$_)) }],
50             );
51              
52             $type->coercion->freeze;
53              
54             __END__
55              
56             =pod
57              
58             =encoding utf-8
59              
60             =head1 NAME
61              
62             Types::UUID - type constraints for UUIDs
63              
64             =head1 SYNOPSIS
65              
66             package FroobleStick;
67            
68             use Moo;
69             use Types::UUID;
70            
71             has identifier => (
72             is => 'lazy',
73             isa => Uuid,
74             coerce => 1,
75             builder => Uuid->generator,
76             );
77              
78             =head1 DESCRIPTION
79              
80             L<Types::UUID> is a type constraint library suitable for use with
81             L<Moo>/L<Moose> attributes, L<Kavorka> sub signatures, and so forth.
82              
83             =head2 Type
84              
85             Currently the module only provides one type constraint, which is
86             exported by default.
87              
88             =over
89              
90             =item C<Uuid>
91              
92             A valid UUID string, as judged by the C<< is_uuid_string() >> function
93             provided by L<UUID::Tiny>.
94              
95             This constraint has coercions from C<Undef> (generates a new UUID),
96             C<Str> (fixes slightly broken-looking UUIDs, adding missing dashes;
97             also accepts base-64-encoded UUIDs) and L<URI> objects using the
98             C<< urn:uuid: >> URI scheme.
99              
100             =back
101              
102             =head2 Methods
103              
104             The C<Uuid> type constraint is actually blessed into a subclass of
105             L<Type::Tiny>, and provides an aditional method:
106              
107             =over
108              
109             =item C<< Uuid->generate >>
110              
111             Generates a new UUID. C<< Uuid->coerce(undef) >> would also work, but
112             looks a little odd.
113              
114             =item C<< Uuid->generator >>
115              
116             Returns a coderef which generates a new UUID. For an example usage, see
117             the L</SYNOPSIS>.
118              
119             =back
120              
121             =head1 BUGS
122              
123             Please report any bugs to
124             L<http://rt.cpan.org/Dist/Display.html?Queue=Types-UUID>.
125              
126             =head1 SEE ALSO
127              
128             L<Type::Tiny::Manual>, L<UUID::Tiny>.
129              
130             =head1 AUTHOR
131              
132             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
133              
134             =head1 COPYRIGHT AND LICENCE
135              
136             This software is copyright (c) 2014 by Toby Inkster.
137              
138             This is free software; you can redistribute it and/or modify it under
139             the same terms as the Perl 5 programming language system itself.
140              
141              
142             =head1 DISCLAIMER OF WARRANTIES
143              
144             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
145             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
146             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
147