alltests pass!
Guillermo Roditi [Tue, 26 Aug 2008 00:16:19 +0000 (00:16 +0000)]
lib/MooseX/Types/Data/GUID.pm [new file with mode: 0644]
t/basic.t [new file with mode: 0644]

diff --git a/lib/MooseX/Types/Data/GUID.pm b/lib/MooseX/Types/Data/GUID.pm
new file mode 100644 (file)
index 0000000..d36cf56
--- /dev/null
@@ -0,0 +1,68 @@
+package MooseX::Types::Data::GUID;
+
+use strict;
+use warnings;
+
+use Data::GUID;
+use MooseX::Types -declare => [qw/ GUID /];
+use Moose::Util::TypeConstraints;
+
+class_type 'Data::GUID';
+subtype GUID, as 'Data::GUID';
+
+coerce 'Data::GUID' =>
+  from Str => via { Data::GUID->from_any_string($_) };
+
+coerce GUID,
+  from Str => via { Data::GUID->from_any_string($_) };
+
+1;
+
+__END__;
+
+=head1 NAME
+
+MooseX::Types::Data::GUID - L<Data::GUID> related constraints and coercions for
+Moose
+
+=head1 SYNOPSIS
+
+Export Example:
+
+    use MooseX::Types::Data::GUID qw(TimeZone);
+
+    has guid => (
+        isa => GUID,
+        is => "rw",
+        coerce => 1,
+    );
+
+    Class->new( guid => "C6A9FE9A-72FE-11DD-B3B4-B2EC1DADD46B");
+
+Namespaced Example:
+
+    use MooseX::Types::Data::GUID;
+
+    has guid => (
+        isa => 'Data::GUID',
+        is => "rw",
+        coerce => 1,
+    );
+
+    Class->new( guid => "C6A9FE9A-72FE-11DD-B3B4-B2EC1DADD46B");
+
+=head1 DESCRIPTION
+
+This module packages several L<Moose::Util::TypeConstraints> with coercions,
+designed to work with L<Data::GUID>.
+
+=head1 AUTHOR
+
+Guillermo Roditi (groditi) E<lt>groditi@cpan.orgE<gt>
+
+=head1 COPYRIGHT
+
+Copyright (c) 2008 Guillermo Roditi. This program is free software; you can 
+redistribute it and/or modify it under the same terms as Perl itself.
+
+=cut
diff --git a/t/basic.t b/t/basic.t
new file mode 100644 (file)
index 0000000..916ce6b
--- /dev/null
+++ b/t/basic.t
@@ -0,0 +1,28 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+
+use Test::More tests => 10;
+use Moose::Util::TypeConstraints;
+use MooseX::Types::Data::GUID qw/GUID/;
+
+for my $key ( GUID, 'Data::GUID' ){
+  if(my $constraint = find_type_constraint($key) ){
+    isa_ok( $constraint, "Moose::Meta::TypeConstraint" );
+    if ( $constraint->has_coercion ){
+      ok(1, 'has coercion');
+      my $coercion = $constraint->coercion;
+      ok( $coercion->has_coercion_for_type('Str'), 'has coercion for Str');
+      my $original_str = 'C6A9FE9A-72FE-11DD-B3B4-B2EC1DADD46B';
+      if( my $guid = $coercion->coerce($original_str) ){
+        isa_ok($guid, 'Data::GUID');
+        is($guid->as_string, $original_str, 'Same GUID was built');
+      }
+    } else {
+      ok(0, 'Failed to find type coercion');
+    }
+  } else {
+    ok(0, "Failed to find type constraint '${key}'");
+  }
+}