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   22203 use strict;
  1         7  
  1         25  
2 1     1   5 use warnings;
  1         1  
  1         38  
3             package Data::Rx::Type::MooseTC 0.008;
4             # ABSTRACT: experimental / proof of concept Rx types from Moose types
5 1     1   4 use parent 'Data::Rx::CommonType::EasyNew';
  1         2  
  1         4  
6              
7 1     1   3770 use Carp ();
  1         2  
  1         13  
8 1     1   510 use Moose::Util::TypeConstraints ();
  1         234254  
  1         198  
9              
10             #pod =head1 SYNOPSIS
11             #pod
12             #pod use Data::Rx;
13             #pod use Data::Rx::Type::MooseTC;
14             #pod use Test::More tests => 2;
15             #pod
16             #pod my $rx = Data::Rx->new({
17             #pod prefix => {
18             #pod moose => 'tag:rjbs.manxome.org,2008-10-04:rx/moose/',
19             #pod },
20             #pod type_plugins => [ 'Data::Rx::Type::MooseTC' ]
21             #pod });
22             #pod
23             #pod my $array_of_int = $rx->make_schema({
24             #pod type => '/moose/tc',
25             #pod moose_type => 'ArrayRef[Int]',
26             #pod });
27             #pod
28             #pod ok($array_of_int->check([1]), "[1] is an ArrayRef[Int]");
29             #pod ok(! $array_of_int->check( 1 ), "1 is not an ArrayRef[Int]");
30             #pod
31             #pod =head1 WARNING
32             #pod
33             #pod This module is primarly provided as a proof of concept and demonstration of
34             #pod user-written Rx type plugins. It isn't meant to be used for serious work.
35             #pod Moose type constraints may change their interface in the future.
36             #pod
37             #pod =cut
38              
39 1     1 0 10804 sub type_uri { 'tag:rjbs.manxome.org,2008-10-04:rx/moose/tc' }
40              
41             sub guts_from_arg {
42 2     2 0 508 my ($class, $arg) = @_;
43              
44 2 50       8 Carp::croak("no type supplied for $class") unless my $mt = $arg->{moose_type};
45              
46 2         2 my $tc;
47              
48 2 50       5 if (ref $mt) {
49 0         0 $tc = $mt;
50             } else {
51             package
52             Moose::Util::TypeConstraints; # SUCH LONG IDENTIFIERS
53 2         8 $tc = find_or_parse_type_constraint( normalize_type_constraint_name($mt) );
54             }
55              
56 2 50       4341 Carp::croak("could not make Moose type constraint from $mt")
57             unless $tc->isa('Moose::Meta::TypeConstraint');
58              
59 2         8 return { tc => $tc };
60             }
61              
62             sub assert_valid {
63 3     3 0 507 my ($self, $value) = @_;
64              
65 3 100       14 unless ($self->{tc}->check($value)) {
66 1         72 $self->fail({
67             error => [ qw(type) ],
68             message => "found value does not pass type constraint",
69             value => $value,
70             });
71             }
72              
73 2         146 return 1;
74             }
75              
76             1;
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =head1 NAME
85              
86             Data::Rx::Type::MooseTC - experimental / proof of concept Rx types from Moose types
87              
88             =head1 VERSION
89              
90             version 0.008
91              
92             =head1 SYNOPSIS
93              
94             use Data::Rx;
95             use Data::Rx::Type::MooseTC;
96             use Test::More tests => 2;
97              
98             my $rx = Data::Rx->new({
99             prefix => {
100             moose => 'tag:rjbs.manxome.org,2008-10-04:rx/moose/',
101             },
102             type_plugins => [ 'Data::Rx::Type::MooseTC' ]
103             });
104              
105             my $array_of_int = $rx->make_schema({
106             type => '/moose/tc',
107             moose_type => 'ArrayRef[Int]',
108             });
109              
110             ok($array_of_int->check([1]), "[1] is an ArrayRef[Int]");
111             ok(! $array_of_int->check( 1 ), "1 is not an ArrayRef[Int]");
112              
113             =head1 PERL VERSION
114              
115             This library should run on perls released even a long time ago. It should work
116             on any version of perl released in the last five years.
117              
118             Although it may work on older versions of perl, no guarantee is made that the
119             minimum required version will not be increased. The version may be increased
120             for any reason, and there is no promise that patches will be accepted to lower
121             the minimum required perl.
122              
123             =head1 WARNING
124              
125             This module is primarly provided as a proof of concept and demonstration of
126             user-written Rx type plugins. It isn't meant to be used for serious work.
127             Moose type constraints may change their interface in the future.
128              
129             =head1 AUTHOR
130              
131             Ricardo SIGNES <cpan@semiotic.systems>
132              
133             =head1 CONTRIBUTOR
134              
135             =for stopwords Ricardo Signes
136              
137             Ricardo Signes <rjbs@semiotic.systems>
138              
139             =head1 COPYRIGHT AND LICENSE
140              
141             This software is copyright (c) 2022 by Ricardo SIGNES.
142              
143             This is free software; you can redistribute it and/or modify it under
144             the same terms as the Perl 5 programming language system itself.
145              
146             =cut