Brick/Constraints.pm

filter: Statementstmt 80.0% 28 / 35 Branchbran 37.5% 3 / 8 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 66.6% 6 / 9 Podpod n/a totaltotal 56.5% 43 / 76 CC 11 SCAR 26.3
CC
11
Cov
63%
__make_constraint
24.4
__ANON__
6.0
BEGIN
1.0
CRAP 13.8
« Brick/General.pm ↑ Summary Brick/Bucket.pm »
1 package Brick::Constraints;
2 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 $VERSION = '0.904';
6  
7 package Brick::Bucket;
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use subs qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 Brick::Constraints - Connect the input data to the closures in the pool
19  
20 =head1 SYNOPSIS
21  
22 use Brick;
23  
24 =head1 DESCRIPTION
25  
26 =over 4
27  
28 =item __make_constraint( CODEREF, INPUT_HASH_REF )
29  
30 Turn a closure into a constraint by providing the bridge between the
31 input hash and code reference.
32  
33 Call this in your top level generator after you have composed all the
34 pieces you want.
35  
36 =cut
37  
38 sub __make_constraint { # may need to change name to make generic
39 2 ▶ my( $bucket, $validator, $setup ) = @_;
Subroutine __make_constraint: called CC=10 SCAR=31.9
40  
41 2 ▶ $setup ||= {};
Condition50%1/2
$setup ||= {}
l!l
20
MC/DC0%0/1
$setup ||= {}
$setup
Truth table50%1/2
A
A= $setup
Aresult
11
00
42  
43 2 my @callers = $bucket->__caller_chain_as_list();
44  
45 #print STDERR Data::Dumper->Dump( [\@callers], [qw(callers)] ); use Data::Dumper;
46  
47 2 ▶ if( $#callers >= 1 and exists $callers[1]{'sub'} and $callers[1]{'sub'} =~ m/^_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#callers >= 1 and exists $callers[1]{"sub"} and $callers[1]{"sub"} =~ /^_/) 0 2 2
Condition33%2/6
$#callers >= 1 and exists $callers[1]{"sub"} and $callers[1]{"sub"} =~ /^_/
!ll&&!rl&&r
020
$#callers >= 1 and exists $callers[1]{"sub"}
!ll&&!rl&&r
002
MC/DC0%0/3
$#callers >= 1 and exists $callers[1]{"sub"} and $callers[1]{"sub"} =~ /^_/
$#callers >= 1 exists $callers[1]{"sub"} $callers[1]{"sub"} =~ /^_/
Truth table25%1/4
A and B and C
A= $#callers >= 1B= exists $callers[1]{"sub"}C= $callers[1]{"sub"} =~ /^_/
ABCresult
0XX0
10X0
1100
1111
48 0 carp "$callers[1]{'sub'} called from sub with leading underscore. Are you sure you want that?";
49 }
50  
51 2 ▶ my $name = $setup->{name} || $callers[1]{'sub'} || 'Anonymous';
Condition40%2/5
$setup->{'name'} || $callers[1]{'sub'} || 'Anonymous'
l!l
20
$setup->{"name"} or $callers[1]{"sub"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$setup->{'name'} || $callers[1]{'sub'} || 'Anonymous'
$setup->{"name"} $callers[1]{"sub"}
Truth table33%1/3
A or B
A= $setup->{"name"}B= $callers[1]{"sub"}
ABresult
1X1
011
000
52 2 ▶ print STDERR "Constraint name is $name\n" if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 2 2
53  
54 2 ▶ unless(
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $validator->isa(ref sub { } ) } } or UNIVERSAL::isa($validator, ref sub { } )) 0 2 2
Condition33%1/3
eval { do { $validator->isa(ref sub { } ) } } or UNIVERSAL::isa($validator, ref sub { } )
l!l&&r!l&&!r
020
MC/DC0%0/2
eval { do { $validator->isa(ref sub { } ) } } or UNIVERSAL::isa($validator, ref sub { } )
eval { do { $validator->isa(ref sub { } ) } } UNIVERSAL::isa($validator, ref sub { } )
Truth table33%1/3
A or B
A= eval { do { $validator->isa(ref sub { } ) } }B= UNIVERSAL::isa($validator, ref sub { } )
ABresult
1X1
011
000
55 2 ▶ eval { $validator->isa( ref sub {} ) } ||
Subroutine __ANON__: not called CC=1 SCAR=0.0
56 ▶ UNIVERSAL::isa( $validator, ref sub {} )
Subroutine __ANON__: not called CC=1 SCAR=0.0
57 ) {
58 0 croak( "Argument to $callers[1]{'sub'} must be a code reference [$validator]: $@" );
59 }
60  
61 my $constraint = $bucket->add_to_bucket( {
62 name => $name,
63 description => "Brick constraint sub for $name",
64  
65 code => sub {
66 0 ▶ my $input_hash = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
67  
68 0 my $result = eval{ $validator->( $input_hash ) };
69 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
70  
71 0 return 1;
72 },
73 2 } );
74  
75 2 $bucket->comprise( $constraint, $validator );
76  
77 2 return $constraint;
78 }
79  
80  
81 =item __make_dfv_constraint
82  
83 Adapter for Data::FormValidator
84  
85 =cut
86  
87 =pod
88  
89 sub __make_dfv_constraint # may need to change name to make generic
90 {
91 my( $bucket, $validator, $hash ) = @_;
92  
93 $hash ||= {};
94  
95 my @callers = main::__caller_chain_as_list();
96  
97 my $name = $hash->{profile_name} || $callers[-1]{'sub'} || 'Anonymous';
98  
99 unless(
100 eval { $validator->isa( ref sub {} ) } or
101 UNIVERSAL::isa( $validator, ref sub {} )
102 )
103 {
104 carp( "Argument to $callers[1]{'sub'} must be a code reference [$validator]: $@" );
105 return $bucket->add_to_bucket( { code => sub {}, name => "Null subroutine",
106 description => "This sub does nothing, because something didn't happen correctly."
107 } );
108 }
109  
110 my $constraint = $bucket->add_to_bucket( {
111 name => $name,
112 description => "Data::FormValidator constraint sub for $callers[-1]{'sub'}",
113  
114 code => sub {
115 my( $dfv ) = @_;
116  
117 $dfv->name_this( $callers[-1]{'sub'} );
118 my( $field, $value ) = map {
119 $dfv->${\ "get_current_constraint_$_"}
120 } qw(field value);
121  
122 my $hash_ref = $dfv->get_filtered_data;
123  
124 return unless $validator->( $hash_ref );
125  
126 return $field;
127 },
128 } );
129  
130 $bucket->comprise( $constraint, $validator );
131  
132 return $constraint;
133 }
134  
135 =back
136  
137 =head1 TO DO
138  
139 TBA
140  
141 =head1 SEE ALSO
142  
143 TBA
144  
145 =head1 SOURCE AVAILABILITY
146  
147 This source is in Github:
148  
149 https://github.com/briandfoy/brick
150  
151 =head1 AUTHOR
152  
153 brian d foy, C<< <briandfoy@pobox.com> >>
154  
155 =head1 COPYRIGHT
156  
157 Copyright © 2007-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
158  
159 You may redistribute this under the terms of the Artistic License 2.0.
160  
161 =cut
162  
163 5 1;
« Brick/General.pm ↑ Summary Brick/Bucket.pm »