File Coverage

blib/lib/Params/Dry/Types.pm
Criterion Covered Total %
statement 36 36 100.0
branch 16 18 88.8
condition 7 9 77.7
subroutine 13 13 100.0
pod 5 5 100.0
total 77 81 95.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             #*
3             #* Name: Params::Dry::Types
4             #* Info: Types definitions module
5             #* Author: Pawel Guspiel (neo77)
6             #*
7             #* This module keeps validation functions. You can of course add your modules which uses this and will add additional checks
8             #* Build in types for Params::Dry
9             #*
10             package Params::Dry::Types;
11             {
12 3     3   1220 use strict;
  3         5  
  3         97  
13 3     3   16 use warnings;
  3         5  
  3         88  
14 3     3   999 use utf8;
  3         15  
  3         30  
15              
16             # --- version ---
17             our $VERSION = 1.20_03;
18              
19             #=------------------------------------------------------------------------ { use, constants }
20              
21 3     3   146 use Scalar::Util 'blessed';
  3         6  
  3         258  
22              
23 3     3   17 use constant PASS => 1; # pass test
  3         8  
  3         317  
24 3     3   17 use constant FAIL => 0; # test fail
  3         6  
  3         131  
25              
26             #=------------------------------------------------------------------------ { export }
27              
28 3     3   18 use Exporter; # to export _ rq and opt
  3         13  
  3         1376  
29             our @ISA = qw(Exporter);
30              
31             our @EXPORT_OK = qw(PASS FAIL);
32              
33             our %EXPORT_TAGS = ( const => [qw(PASS FAIL)], );
34              
35             #=------------------------------------------------------------------------ { module public functions }
36              
37             #=---------
38             # String
39             #=---------
40             #* string type check (parameter sets max length)
41             #* RETURN: PASS if test pass otherwise FAIL
42             sub String {
43 17 100   17 1 2774 ref( $_[0] ) and return FAIL;
44 16 100 100     102 $_[1] and length $_[0] > $_[1] and return FAIL;
45 12         40 PASS;
46             } #+ end of: sub String
47              
48             #=---------
49             # Object
50             #=---------
51             #* Object type check, Object - just object, or Object(Params::Dry::Types) check if is Params::Dry::Types type
52             #* RETURN: PASS if test pass otherwise FAIL
53             sub Object {
54 4     4 1 2026 my $class = blessed( $_[0] );
55 4 100       19 return FAIL if !$class; # not an object
56 1 50 33     11 return FAIL if $_[1] and ( $_[1] ne $class );
57 1         4 PASS;
58             } #+ end of: sub Object
59              
60             #=------
61             # Ref
62             #=------
63             #* ref type check
64             #* RETURN: PASS if test pass otherwise FAIL
65             sub Ref {
66 24 100   24 1 1949 my $ref = ref( $_[0] ) or return FAIL;
67              
68 20 100 100     108 return FAIL if $_[1] and $ref ne $_[1];
69 18         49 PASS;
70             } #+ end of: sub Ref
71              
72             #=----------
73             # Defined
74             #=----------
75             #* Allows anything what is defined
76             #* RETURN: PASS if defined
77             sub Defined {
78 3 100   3 1 1383 defined $_[0] ? PASS : FAIL;
79             } #+ end of: sub Defined
80              
81             #=--------
82             # Value
83             #=--------
84             #* Allows anything what is not a reference
85             #* RETURN: PASS if defined
86             sub Value {
87 2 100   2 1 993 $_[0] and ref $_[0] ? FAIL : PASS;
    50          
88             } #+ end of: sub Value
89              
90             {
91 3     3   18 no warnings 'once';
  3         5  
  3         625  
92              
93             #+ Number - mapped types
94             *Params::Dry::Types::Int = *Params::Dry::Types::Number::Int;
95             *Params::Dry::Types::Float = *Params::Dry::Types::Number::Float;
96             *Params::Dry::Types::Bool = *Params::Dry::Types::Number::Bool;
97              
98             #+ Ref - mapped types
99             *Params::Dry::Types::Scalar = *Params::Dry::Types::Ref::Scalar;
100             *Params::Dry::Types::Array = *Params::Dry::Types::Ref::Array;
101             *Params::Dry::Types::Hash = *Params::Dry::Types::Ref::Hash;
102             *Params::Dry::Types::Code = *Params::Dry::Types::Ref::Code;
103             *Params::Dry::Types::Regexp = *Params::Dry::Types::Ref::Regexp;
104             };
105              
106             };
107             0115 && 0x4d;
108              
109             #+ End of Params::Dry::Types
110             __END__