| 1 | package Brick; | ||||||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use subs qw(); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Carp qw( carp croak ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Brick::Profile; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 6 | $VERSION = '0.904'; | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | Brick - Complex business rule data validation | ||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | use Brick; | ||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | my $brick = Brick->new( { | ||||||||||||||||||||||||||||||||||||
| 25 | external_packages => [ qw(Foo::Validator Bar::Validator) ] | ||||||||||||||||||||||||||||||||||||
| 26 | } ); | ||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | my $profile = Brick::Profile->new( $brick, | ||||||||||||||||||||||||||||||||||||
| 29 | [ required => sub { .... } => $hash ], | ||||||||||||||||||||||||||||||||||||
| 30 | [ optional => optional_fields => $hash ], | ||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||
| 32 | [ inside => in_number => $hash ], | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | [ outside => ex_number => $hash ], | ||||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | my %input_from_app = ( | ||||||||||||||||||||||||||||||||||||
| 38 | name => 'Joe Snuffy', | ||||||||||||||||||||||||||||||||||||
| 39 | ... | ||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | my $results = $brick->apply( $profile, \%%input_from_app ); | ||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||
| 47 | =head2 Class methods | ||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||
| 49 | =over 4 | ||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | =item Brick->new | ||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | Create a new C<Brick>. Currently this doesn't do anything other than | ||||||||||||||||||||||||||||||||||||
| 54 | give you an object so you can call methods. | ||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | Future ideas? Maybe store several buckets or profiles? | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | sub new { | ||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | my( $class, $args ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 3 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||
| 65 | 3 | $self->init( $args ); | |||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | 3 | $self->_load_external_packages( @{ $args->{external_packages} } ); | |||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||
| 69 | 3 | $self; | |||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||
| 72 | sub _load_external_packages { | ||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | my( $self, @packages ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_load_external_packages: called CC=3 SCAR=15.4
| |||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | 3 | my $bucket_class = $self->bucket_class; | |||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | 3 | foreach my $package ( @packages ) { | |||||||||||||||||||||||||||||||||||
| 78 | 0 | eval "package $bucket_class; require $package; $package->import"; | |||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | croak "Could not load $package: $@" if $@; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||
| 84 | =item Brick->error( MESSAGE ) | ||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||
| 86 | Set the error message from the last things that happened. | ||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | =item Brick->error_str | ||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | Get the error message from the last things that happened. | ||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||||||||||
| 95 | 6 | my $Error; | |||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub error { $_[0]->_set_error( $_[1] ); croak $_[1]; } | ||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | sub error_str { $Error } | ||||||||||||||||||||||||||||||||||
|
Subroutine
error_str: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | # do some stuff to figure out caller, etc | ||||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | sub _set_error { $Error = $_[1] } | ||||||||||||||||||||||||||||||||||
|
Subroutine
_set_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||
| 104 | =back | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | =head2 Instance methods | ||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | =over 4 | ||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | =item create_bucket( PROFILE_ARRAYREF ) | ||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||
| 112 | =item create_pool # DEPRECATED | ||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | This method creates a C<Brick::Bucket> instance (or an instance in | ||||||||||||||||||||||||||||||||||||
| 115 | the package returned by C<$brick->bucket_class> ) based on the profile | ||||||||||||||||||||||||||||||||||||
| 116 | and returns the bucket instance. Along the way it affects the args | ||||||||||||||||||||||||||||||||||||
| 117 | hashref in each profile element to add the element name as the key | ||||||||||||||||||||||||||||||||||||
| 118 | C<profile_name> and the actual coderef (not just the method name) as | ||||||||||||||||||||||||||||||||||||
| 119 | the key C<code>. The closure generators are allowed to use those keys. | ||||||||||||||||||||||||||||||||||||
| 120 | For instance, C<__make_constraint>, which is usually the top level | ||||||||||||||||||||||||||||||||||||
| 121 | closure, uses it to name the closure in the bucket. | ||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | If the profile doesn't pass C<lint> test, this method croaks. You | ||||||||||||||||||||||||||||||||||||
| 124 | might want to safeguard that by calling C<lint> first. | ||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||
| 126 | my $bucket = do { | ||||||||||||||||||||||||||||||||||||
| 127 | if( my( $lint ) = $brick->lint( $profile ) ) | ||||||||||||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||||||||||
| 129 | $brick->create_bucket( $profile ); | ||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||
| 131 | else | ||||||||||||||||||||||||||||||||||||
| 132 | { | ||||||||||||||||||||||||||||||||||||
| 133 | Data::Dumper->Dump( [ $lint ], [qw(lint)] ); | ||||||||||||||||||||||||||||||||||||
| 134 | undef; | ||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||
| 138 | From the profile it extracts the method name to create the closure for | ||||||||||||||||||||||||||||||||||||
| 139 | it based on its arguments. If the method item is already a code | ||||||||||||||||||||||||||||||||||||
| 140 | reference it uses it add is, but still adds it to the bucket. This could | ||||||||||||||||||||||||||||||||||||
| 141 | be handy for using closures from other classes, but I haven't | ||||||||||||||||||||||||||||||||||||
| 142 | investigated the consequences of that. | ||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | In scalar context this returns a new bucket instance. If the profile might | ||||||||||||||||||||||||||||||||||||
| 145 | be bad, use an eval to catch the croak: | ||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||
| 147 | my $bucket = eval{ $brick->create_bucket( \@profile ) }; | ||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | In list context, it returns the C<$bucket> instance and an anonymous array | ||||||||||||||||||||||||||||||||||||
| 150 | reference with the stringified closures (which are also the keys in the | ||||||||||||||||||||||||||||||||||||
| 151 | bucket). The elements in the anonymous array correspond to the elements in | ||||||||||||||||||||||||||||||||||||
| 152 | the profile. This is handy in C<explain> which needs to find the bucket | ||||||||||||||||||||||||||||||||||||
| 153 | entries for each profile elements. You probably won't need the second | ||||||||||||||||||||||||||||||||||||
| 154 | argument most of the time. | ||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | my( $bucket, $refs ) = eval { $brick->create_bucket( \@profile ) }; | ||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | sub create_pool { croak "create_pool is now create_bucket!" } | ||||||||||||||||||||||||||||||||||
|
Subroutine
create_pool: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||
| 162 | sub create_bucket { | ||||||||||||||||||||||||||||||||||||
| 163 | 3 | ▶ | my( $brick, $profile ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
create_bucket: called CC=9 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||
| 165 | 3 | ▶ | unless( 0 == $brick->profile_class->lint( $profile || [] ) ) { # zero but true! | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$profile || []
MC/DC0%0/1
$profile || []
$profile
Truth table50%1/2
A
A= $profile
| |||||||||||||||||||||||||||||||||||||
| 166 | 0 | croak "Bad profile for create_bucket! Perhaps you need to check it with lint" | |||||||||||||||||||||||||||||||||||
| 167 | }; | ||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | 3 | my $bucket = $brick->bucket_class->new; | |||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||
| 171 | 3 | my @coderefs = (); | |||||||||||||||||||||||||||||||||||
| 172 | 3 | foreach my $entry ( @$profile ) { | |||||||||||||||||||||||||||||||||||
| 173 | 2 | my( $name, $method, $args ) = @$entry; | |||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||
| 175 | 2 | $args->{profile_name} = $name; | |||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||
| 177 | 2 | $args->{code} = do { | |||||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | if( eval { $method->isa( ref {} ) } or | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
eval {
do {
$method->isa(ref {})
}
} or ref $method eq ref sub {
}
MC/DC0%0/2
eval {
do {
$method->isa(ref {})
}
} or ref $method eq ref sub {
}
eval {
do {
$method->isa(ref {})
}
}
ref $method eq ref sub {
}
Truth table33%1/3
A or B
A= eval {
do {
$method->isa(ref {})
}
}B= ref $method eq ref sub {
}
| |||||||||||||||||||||||||||||||||||||
| 179 | ▶ | ref $method eq ref sub {} ) { | |||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 180 | 0 | $method; | |||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||
| 182 | 2 | elsif( my $code = eval{ $bucket->$method( $args ) } ) { | |||||||||||||||||||||||||||||||||||
| 183 | 2 | $code; | |||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||
| 185 | 0 | elsif( $@ ) { croak $@ } | |||||||||||||||||||||||||||||||||||
| 186 | }; | ||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | 2 | push @coderefs, map { "$_" } $bucket->add_to_bucket( $args ); | |||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | 3 | ▶ | wantarray ? ( $bucket, \@coderefs ) : $bucket; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||
| 194 | =item init | ||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||
| 196 | Initialize the instance, or return it to a pristine state. Normally | ||||||||||||||||||||||||||||||||||||
| 197 | you don't have to do this because C<new> does it for you, but if you | ||||||||||||||||||||||||||||||||||||
| 198 | subclass this you might want to override it. | ||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | sub init { | ||||||||||||||||||||||||||||||||||||
| 203 | 7 | ▶ | my( $self, $args ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||
| 205 | 7 | my $bucket_class = $self->bucket_class; | |||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||
| 207 | 7 | eval "require $bucket_class"; | |||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||
| 209 | 7 | $self->{buckets} = []; | |||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||
| 211 | 7 | ▶ | if( defined $args->{external_packages} && ref $args->{external_packages} eq ref [] ) { # defined and array ref | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $args->{"external_packages"} and ref $args->{"external_packages"} eq ref []
MC/DC100%2/2
defined $args->{"external_packages"} and ref $args->{"external_packages"} eq ref []
defined $args->{"external_packages"}
ref $args->{"external_packages"} eq ref []
Truth table100%3/3
A and B
A= defined $args->{"external_packages"}B= ref $args->{"external_packages"} eq ref []
| |||||||||||||||||||||||||||||||||||||
| 212 | 1 | $self->{external_packages} = $args->{external_packages}; | |||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||
| 214 | ▶ | elsif( defined $args->{external_packages} && | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $args->{"external_packages"} and not $args->{"external_packages"} eq ref []
MC/DC50%1/2
defined $args->{"external_packages"} and not $args->{"external_packages"} eq ref []
defined $args->{"external_packages"}
not $args->{"external_packages"} eq ref []
Truth table66%2/3
A and not(B)
A= defined $args->{"external_packages"}B= not $args->{"external_packages"} eq ref []
| |||||||||||||||||||||||||||||||||||||
| 215 | ! ($args->{external_packages} eq ref []) ) { # defined but not array ref | ||||||||||||||||||||||||||||||||||||
| 216 | 1 | carp "'external_packages' value must be an anonymous array"; | |||||||||||||||||||||||||||||||||||
| 217 | 1 | $self->{external_packages} = []; | |||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||
| 219 | else { # not defined | ||||||||||||||||||||||||||||||||||||
| 220 | 5 | $self->{external_packages} = []; | |||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||
| 224 | =item add_validator_packages( PACKAGES ) | ||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||
| 226 | Load external validator packages into the bucket. Each of these packages | ||||||||||||||||||||||||||||||||||||
| 227 | should export the functions they want to make available. C<add_validator_package> | ||||||||||||||||||||||||||||||||||||
| 228 | C<require>s each package and calls its C<import> routine. | ||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||
| 232 | sub add_validator_packages | ||||||||||||||||||||||||||||||||||||
| 233 | { | ||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | my( $self, @packages ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
add_validator_packages: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||
| 236 | 0 | $self->_load_external_packages( @packages ); | |||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||
| 239 | =item clone; | ||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||
| 241 | Based on the current instance, create another one just like it but not | ||||||||||||||||||||||||||||||||||||
| 242 | connected to it (in effect forking the instance). After the C<clone> | ||||||||||||||||||||||||||||||||||||
| 243 | you can change new instance without affecting the old one. This is | ||||||||||||||||||||||||||||||||||||
| 244 | handy in C<explain>, for instance, where I want a deep copy for a | ||||||||||||||||||||||||||||||||||||
| 245 | moment. At least I think I want a deep copy. | ||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||
| 247 | That's the idea. Right now this just returns the same instance. When | ||||||||||||||||||||||||||||||||||||
| 248 | not using a copy breaks, I'll fix that. | ||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||
| 250 | =cut | ||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||
| 252 | sub clone { | ||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | my( $brick ) = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | 0 | $brick; | |||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||
| 258 | sub explain { | ||||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | croak "Who's calling Brick::explain? That's in Brick::Profile now!"; | ||||||||||||||||||||||||||||||||||
|
Subroutine
explain: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||
| 262 | =item apply( PROFILE OBJECT, INPUT_DATA_HASHREF ) | ||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||
| 264 | Apply the profile to the data in the input hash reference. The profile | ||||||||||||||||||||||||||||||||||||
| 265 | can either be a profile object or an array ref that apply() will use to | ||||||||||||||||||||||||||||||||||||
| 266 | create the profile object. | ||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||
| 268 | This returns a results object blessed into the class name returned by | ||||||||||||||||||||||||||||||||||||
| 269 | results_class(), which is Brick::Result by default. If you don't like | ||||||||||||||||||||||||||||||||||||
| 270 | that, you can override it in your own subclass. | ||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||
| 272 | =cut | ||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||
| 274 | sub apply { | ||||||||||||||||||||||||||||||||||||
| 275 | 1 | ▶ | my( $brick, $profile, $input ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
apply: called CC=10 SCAR=40.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||
| 277 | croak "Did not get a profile object in Brick::apply()!\n" | ||||||||||||||||||||||||||||||||||||
| 278 | 1 | ▶ | unless eval { $profile->isa( $brick->profile_class ) }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||
| 280 | 1 | my $bucket = $profile->get_bucket; | |||||||||||||||||||||||||||||||||||
| 281 | 1 | my $coderefs = $profile->get_coderefs; | |||||||||||||||||||||||||||||||||||
| 282 | 1 | my $array = $profile->get_array; | |||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||
| 284 | my @entries = map { | ||||||||||||||||||||||||||||||||||||
| 285 | 1 | my $e = $bucket->get_from_bucket( $_ ); | |||||||||||||||||||||||||||||||||||
| 286 | 0 | [ map { $e->$_ } qw(get_coderef get_name) ] | |||||||||||||||||||||||||||||||||||
| 287 | } @$coderefs; | ||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||
| 289 | 1 | my @results = (); | |||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||
| 291 | 1 | foreach my $index ( 0 .. $#entries ) { | |||||||||||||||||||||||||||||||||||
| 292 | 0 | my $e = $entries[$index]; | |||||||||||||||||||||||||||||||||||
| 293 | 0 | my $name = $array->[$index][0]; | |||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||
| 295 | 0 | my $bucket_entry = $bucket->get_from_bucket( "$e->[0]" ); | |||||||||||||||||||||||||||||||||||
| 296 | 0 | my $sub_name = $bucket_entry->get_name; | |||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||
| 298 | 0 | my $result = eval{ $e->[0]->( $input ) }; | |||||||||||||||||||||||||||||||||||
| 299 | 0 | my $eval_error = $@; | |||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | carp "Brick: $sub_name: eval error \$\@ is not a string or hash reference" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not ref $eval_error or ref $eval_error eq ref {}
MC/DC0%0/2
not ref $eval_error or ref $eval_error eq ref {}
not ref $eval_error
ref $eval_error eq ref {}
Truth table0%0/3
not(A) or B
A= not ref $eval_errorB= ref $eval_error eq ref {}
| |||||||||||||||||||||||||||||||||||||
| 302 | unless( ! ref $eval_error or ref $eval_error eq ref {} ); | ||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||
| 304 | 0 | ▶ | if( defined $eval_error and ref $eval_error eq ref {} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $eval_error and ref $eval_error eq ref {}
MC/DC0%0/2
defined $eval_error and ref $eval_error eq ref {}
defined $eval_error
ref $eval_error eq ref {}
Truth table0%0/3
A and B
A= defined $eval_errorB= ref $eval_error eq ref {}
| |||||||||||||||||||||||||||||||||||||
| 305 | 0 | $result = 0; | |||||||||||||||||||||||||||||||||||
| 306 | carp "Brick: $sub_name died with reference, but didn't define 'handler' key" | ||||||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | unless exists $eval_error->{handler}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||
| 309 | carp "Brick: $sub_name died with reference, but didn't define 'message' key" | ||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | unless exists $eval_error->{message}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||
| 312 | elsif( defined $eval_error ) { # but not a reference | ||||||||||||||||||||||||||||||||||||
| 313 | 0 | $eval_error = { | |||||||||||||||||||||||||||||||||||
| 314 | handler => 'program_error', | ||||||||||||||||||||||||||||||||||||
| 315 | message => $eval_error, | ||||||||||||||||||||||||||||||||||||
| 316 | program_error => 1, | ||||||||||||||||||||||||||||||||||||
| 317 | errors => [], | ||||||||||||||||||||||||||||||||||||
| 318 | }; | ||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||
| 321 | 0 | my $handler = $array->[$index][1]; | |||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||
| 323 | 0 | my $result_item = $brick->result_class->result_item_class->new( | |||||||||||||||||||||||||||||||||||
| 324 | label => $name, | ||||||||||||||||||||||||||||||||||||
| 325 | method => $handler, | ||||||||||||||||||||||||||||||||||||
| 326 | result => $result, | ||||||||||||||||||||||||||||||||||||
| 327 | messages => $eval_error, | ||||||||||||||||||||||||||||||||||||
| 328 | ); | ||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||
| 330 | 0 | push @results, $result_item; | |||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||
| 333 | 1 | return bless \@results, $brick->result_class; | |||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||
| 336 | =item bucket_class | ||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||
| 338 | The namespace where the constraint building blocks are defined. By | ||||||||||||||||||||||||||||||||||||
| 339 | default this is C<Brick::Bucket>. If you don't like that, override | ||||||||||||||||||||||||||||||||||||
| 340 | this in a subclass. Things that need to work with the bucket class | ||||||||||||||||||||||||||||||||||||
| 341 | name, such as a factory method, will use the return value of this | ||||||||||||||||||||||||||||||||||||
| 342 | method. | ||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||
| 344 | This method also loads the right class, so if you override it, | ||||||||||||||||||||||||||||||||||||
| 345 | remember to load the class too! | ||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||
| 347 | =cut | ||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||
| 349 | 25 | ▶ | sub bucket_class { require Brick::Bucket; 'Brick::Bucket' } | ||||||||||||||||||||||||||||||||||
|
Subroutine
bucket_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||
| 351 | =item result_class | ||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||
| 353 | The namespace that C<apply> uses for its result object. By default | ||||||||||||||||||||||||||||||||||||
| 354 | this is C<Brick::Result>. If you don't like that, override this in a | ||||||||||||||||||||||||||||||||||||
| 355 | subclass. Things that need to work with the result class name, such as | ||||||||||||||||||||||||||||||||||||
| 356 | a factory method, will use the return value of this method. | ||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||
| 358 | This method also loads the right class, so if you override it, | ||||||||||||||||||||||||||||||||||||
| 359 | remember to load the class too! | ||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||
| 361 | =cut | ||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||
| 363 | 2 | ▶ | sub result_class { require Brick::Result; 'Brick::Result' } | ||||||||||||||||||||||||||||||||||
|
Subroutine
result_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||
| 365 | =item profile_class | ||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||
| 367 | The namespace for the profile object. By default this is | ||||||||||||||||||||||||||||||||||||
| 368 | C<Brick::Profile>. If you don't like that, override this in a | ||||||||||||||||||||||||||||||||||||
| 369 | subclass. Things that need to work with the result class name, such as | ||||||||||||||||||||||||||||||||||||
| 370 | a factory method, will use the return value of this method. | ||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||
| 372 | This method also loads the right class, so if you override it, | ||||||||||||||||||||||||||||||||||||
| 373 | remember to load the class too! | ||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||
| 375 | =cut | ||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||
| 377 | 16 | ▶ | sub profile_class { require Brick::Profile; 'Brick::Profile' } | ||||||||||||||||||||||||||||||||||
|
Subroutine
profile_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||
| 379 | =back | ||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||
| 381 | =head1 TO DO | ||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||
| 383 | TBA | ||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||
| 385 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||
| 387 | L<Brick::Tutorial>, L<Brick::UserGuide> | ||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||
| 389 | =head1 SOURCE AVAILABILITY | ||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||
| 391 | This source is in Github: | ||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||
| 393 | https://github.com/briandfoy/brick | ||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||
| 395 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||
| 397 | brian d foy, C<< <briandfoy@pobox.com> >> | ||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||
| 399 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||
| 401 | Copyright © 2007-2025, brian d foy <briandfoy@pobox.com>. All rights reserved. | ||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||
| 403 | You may redistribute this under the terms of the Artistic License 2.0. | ||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||
| 405 | =cut | ||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||
| 407 | 6 | 1; | |||||||||||||||||||||||||||||||||||