File Coverage

blib/lib/Data/Rx/Type/MooseTC.pm
Criterion Covered Total %
statement 27 28 96.4
branch 5 8 62.5
condition n/a
subroutine 8 8 100.0
pod 0 3 0.0
total 40 47 85.1


line stmt bran cond sub pod time code
1 1     1   39262 use strict;
  1         2  
  1         32  
2 1     1   7 use warnings;
  1         2  
  1         54  
3             package Data::Rx::Type::MooseTC;
4             {
5             $Data::Rx::Type::MooseTC::VERSION = '0.006';
6             }
7             # ABSTRACT: experimental / proof of concept Rx types from Moose types
8 1     1   5 use parent 'Data::Rx::CommonType::EasyNew';
  1         2  
  1         6  
9              
10 1     1   6648 use Carp ();
  1         3  
  1         16  
11 1     1   1153 use Moose::Util::TypeConstraints ();
  1         317943  
  1         333  
12              
13              
14 1     1 0 16740 sub type_uri { 'tag:rjbs.manxome.org,2008-10-04:rx/moose/tc' }
15              
16             sub guts_from_arg {
17 2     2 0 741 my ($class, $arg) = @_;
18              
19 2 50       11 Carp::croak("no type supplied for $class") unless my $mt = $arg->{moose_type};
20              
21 2         3 my $tc;
22              
23 2 50       6 if (ref $mt) {
24 0         0 $tc = $mt;
25             } else {
26             package
27             Moose::Util::TypeConstraints; # SUCH LONG IDENTIFIERS
28 2         11 $tc = find_or_parse_type_constraint( normalize_type_constraint_name($mt) );
29             }
30              
31 2 50       9313 Carp::croak("could not make Moose type constraint from $mt")
32             unless $tc->isa('Moose::Meta::TypeConstraint');
33              
34 2         11 return { tc => $tc };
35             }
36              
37             sub assert_valid {
38 3     3 0 900 my ($self, $value) = @_;
39              
40 3 100       23 unless ($self->{tc}->check($value)) {
41 1         122 $self->fail({
42             error => [ qw(type) ],
43             message => "found value does not pass type constraint",
44             value => $value,
45             });
46             }
47              
48 2         229 return 1;
49             }
50              
51             1;
52              
53             __END__
54              
55             =pod
56              
57             =encoding UTF-8
58              
59             =head1 NAME
60              
61             Data::Rx::Type::MooseTC - experimental / proof of concept Rx types from Moose types
62              
63             =head1 VERSION
64              
65             version 0.006
66              
67             =head1 SYNOPSIS
68              
69             use Data::Rx;
70             use Data::Rx::Type::MooseTC;
71             use Test::More tests => 2;
72              
73             my $rx = Data::Rx->new({
74             prefix => {
75             moose => 'tag:rjbs.manxome.org,2008-10-04:rx/moose/',
76             },
77             type_plugins => [ 'Data::Rx::Type::MooseTC' ]
78             });
79              
80             my $array_of_int = $rx->make_schema({
81             type => '/moose/tc',
82             moose_type => 'ArrayRef[Int]',
83             });
84              
85             ok($array_of_int->check([1]), "[1] is an ArrayRef[Int]");
86             ok(! $array_of_int->check( 1 ), "1 is not an ArrayRef[Int]");
87              
88             =head1 WARNING
89              
90             This module is primarly provided as a proof of concept and demonstration of
91             user-written Rx type plugins. It isn't meant to be used for serious work.
92             Moose type constraints may change their interface in the future.
93              
94             =head1 AUTHOR
95              
96             Ricardo SIGNES <rjbs@cpan.org>
97              
98             =head1 COPYRIGHT AND LICENSE
99              
100             This software is copyright (c) 2013 by Ricardo SIGNES.
101              
102             This is free software; you can redistribute it and/or modify it under
103             the same terms as the Perl 5 programming language system itself.
104              
105             =cut