File Coverage

blib/lib/Brick/Constraints.pm
Criterion Covered Total %
statement 26 33 78.7
branch 3 8 37.5
condition 5 13 38.4
subroutine 6 9 66.6
pod n/a
total 40 63 63.4


line stmt bran cond sub pod time code
1             package Brick::Constraints;
2 5     5   32 use base qw(Exporter);
  5         12  
  5         589  
3 5     5   30 use vars qw($VERSION);
  5         8  
  5         285  
4              
5             $VERSION = '0.904';
6              
7             package Brick::Bucket;
8 5     5   24 use strict;
  5         9  
  5         154  
9              
10 5     5   26 use subs qw();
  5         8  
  5         116  
11              
12 5     5   20 use Carp qw(croak carp);
  5         7  
  5         2634  
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     2   39 my( $bucket, $validator, $setup ) = @_;
40              
41 2   50     8 $setup ||= {};
42              
43 2         7 my @callers = $bucket->__caller_chain_as_list();
44              
45             #print STDERR Data::Dumper->Dump( [\@callers], [qw(callers)] ); use Data::Dumper;
46              
47 2 50 33     25 if( $#callers >= 1 and exists $callers[1]{'sub'} and $callers[1]{'sub'} =~ m/^_/ ) {
      33        
48 0         0 carp "$callers[1]{'sub'} called from sub with leading underscore. Are you sure you want that?";
49             }
50              
51 2   50     13 my $name = $setup->{name} || $callers[1]{'sub'} || 'Anonymous';
52 2 50       8 print STDERR "Constraint name is $name\n" if $ENV{DEBUG};
53              
54 2 50 33     5 unless(
55 2     0   124 eval { $validator->isa( ref sub {} ) } ||
56       0     UNIVERSAL::isa( $validator, ref sub {} )
57             ) {
58 0         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     0   0 my $input_hash = shift;
67              
68 0         0 my $result = eval{ $validator->( $input_hash ) };
  0         0  
69 0 0       0 die if $@;
70              
71 0         0 return 1;
72             },
73 2         27 } );
74              
75 2         10 $bucket->comprise( $constraint, $validator );
76              
77 2         19 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<< >>
154              
155             =head1 COPYRIGHT
156              
157             Copyright © 2007-2025, brian d foy . All rights reserved.
158              
159             You may redistribute this under the terms of the Artistic License 2.0.
160              
161             =cut
162              
163             1;