File Coverage

blib/lib/SQL/Translator/Types.pm
Criterion Covered Total %
statement 34 34 100.0
branch 13 16 81.2
condition 8 13 61.5
subroutine 11 11 100.0
pod 2 2 100.0
total 68 76 89.4


line stmt bran cond sub pod time code
1             package SQL::Translator::Types;
2              
3 77     77   566 use warnings;
  77         185  
  77         5279  
4 77     77   506 use strict;
  77         174  
  77         3056  
5              
6             =head1 NAME
7              
8             SQL::Translator::Types - Type checking functions
9              
10             =head1 SYNOPSIS
11              
12             package Foo;
13             use Moo;
14             use SQL::Translator::Types qw(schema_obj enum);
15              
16             has foo => ( is => 'rw', isa => schema_obj('Trigger') );
17             has bar => ( is => 'rw', isa => enum([qw(baz quux quuz)], {
18             msg => "Invalid value for bar: '%s'", icase => 1,
19             });
20              
21             =head1 DESCRIPTIONS
22              
23             This module exports functions that return coderefs suitable for L
24             C type checks.
25             Errors are reported using L.
26              
27             =cut
28              
29 77     77   462 use SQL::Translator::Utils qw(throw);
  77         213  
  77         5644  
30 77     77   738 use Scalar::Util qw(blessed);
  77         173  
  77         6018  
31              
32 77     77   680 use Exporter qw(import);
  77         304  
  77         40310  
33             our @EXPORT_OK = qw(schema_obj enum);
34              
35             =head1 FUNCTIONS
36              
37             =head2 schema_obj($type)
38              
39             Returns a coderef that checks that its arguments is an object of the
40             class C<< SQL::Translator::Schema::I<$type> >>.
41              
42             =cut
43              
44             sub schema_obj {
45 674     674 1 2826 my ($class) = @_;
46 674         2494 my $name = lc $class;
47 674 100       3399 $class = 'SQL::Translator::Schema' . ($class eq 'Schema' ? '' : "::$class");
48             return sub {
49 3027 50 33 3027   153105 throw("Not a $name object")
50             unless blessed($_[0])
51             and $_[0]->isa($class);
52 674         6336 };
53             }
54              
55             =head2 enum(\@strings, [$msg | \%parameters])
56              
57             Returns a coderef that checks that the argument is one of the provided
58             C<@strings>.
59              
60             =head3 Parameters
61              
62             =over
63              
64             =item msg
65              
66             L string for the error message.
67             If no other parameters are needed, this can be provided on its own,
68             instead of the C<%parameters> hashref.
69             The invalid value is passed as the only argument.
70             Defaults to C.
71              
72             =item icase
73              
74             If true, folds the values to lower case before checking for equality.
75              
76             =item allow_undef
77              
78             If true, allow C in addition to the specified strings.
79              
80             =item allow_false
81              
82             If true, allow any false value in addition to the specified strings.
83              
84             =back
85              
86             =cut
87              
88             sub enum {
89 374     374 1 49720 my ($values, $args) = @_;
90 374   50     1738 $args ||= {};
91 374 50       1635 $args = { msg => $args } unless ref($args) eq 'HASH';
92 374         1244 my $icase = !!$args->{icase};
93 374 100       898 my %values = map { ($icase ? lc : $_) => undef } @{$values};
  1356         4926  
  374         1130  
94 374   50     1541 my $msg = $args->{msg} || "Invalid value: '%s'";
95             my $extra_test
96 104     104   2678 = $args->{allow_undef} ? sub { defined $_[0] }
97 1453     1453   34707 : $args->{allow_false} ? sub { !!$_[0] }
98 374 50       2531 : undef;
    100          
99              
100             return sub {
101 1557 100   1557   67128 my $val = $icase ? lc $_[0] : $_[0];
102             throw(sprintf($msg, $val))
103             if (!defined($extra_test) || $extra_test->($val))
104 1557 100 66     6273 && !exists $values{$val};
      100        
105 374         3989 };
106             }
107              
108             1;