| 1 | package Syccess::Field; | ||||||||||
| 2 | 9 | our $AUTHORITY = 'cpan:GETTY'; | |||||||||
| 3 | # ABSTRACT: Syccess field | ||||||||||
| 4 | 9 | $Syccess::Field::VERSION = '0.104'; | |||||||||
| 5 | 9 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 9 | ▶ | use Module::Runtime qw( use_module ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 9 | ▶ | use Module::Load::Conditional qw( can_load ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 9 | with qw( | |||||||||
| 10 | MooX::Traits | ||||||||||
| 11 | ); | ||||||||||
| 12 | |||||||||||
| 13 | 9 | has syccess => ( | |||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | required => 1, | ||||||||||
| 16 | weak_ref => 1, | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 9 | has name => ( | |||||||||
| 20 | is => 'ro', | ||||||||||
| 21 | required => 1, | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 9 | has label => ( | |||||||||
| 25 | is => 'lazy', | ||||||||||
| 26 | init_arg => undef, | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | 18 | ▶ | sub _build_label { | ||||||||
|
Subroutine
_build_label: called CC=5 SCAR=16.6
| |||||||||||
| 30 | 18 | my ( $self ) = @_; | |||||||||
| 31 | 18 | ▶ | if (ref $self->validators_list eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | return $self->validators_list->{label} | ||||||||||
| 33 | 0 | ▶ | if defined $self->validators_list->{label}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | } else { | ||||||||||
| 35 | 18 | my @validators_list = @{$self->validators_list}; | |||||||||
| 36 | 18 | ▶ | while (@validators_list) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 21 | my ( $key, $arg ) = splice(@validators_list,0,2); | |||||||||
| 38 | 21 | ▶ | return $arg if $key eq 'label'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | } | ||||||||||
| 40 | } | ||||||||||
| 41 | 15 | my $name = $self->name; | |||||||||
| 42 | 15 | $name =~ s/_([a-z])/ \U$1/g; | |||||||||
| 43 | 15 | return ucfirst($name); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 9 | has validators_args => ( | |||||||||
| 47 | is => 'ro', | ||||||||||
| 48 | predicate => 1, | ||||||||||
| 49 | ); | ||||||||||
| 50 | |||||||||||
| 51 | 9 | has validators_list => ( | |||||||||
| 52 | is => 'ro', | ||||||||||
| 53 | required => 1, | ||||||||||
| 54 | init_arg => 'validators', | ||||||||||
| 55 | ); | ||||||||||
| 56 | |||||||||||
| 57 | 9 | has validators => ( | |||||||||
| 58 | is => 'lazy', | ||||||||||
| 59 | init_arg => undef, | ||||||||||
| 60 | ); | ||||||||||
| 61 | |||||||||||
| 62 | 19 | ▶ | sub _build_validators { | ||||||||
|
Subroutine
_build_validators: called CC=6 SCAR=18.4
| |||||||||||
| 63 | 19 | my ( $self ) = @_; | |||||||||
| 64 | my %validators_args = $self->has_validators_args | ||||||||||
| 65 | 0 | ▶ | ? (%{$self->validators_args}) : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 19 | my @validators; | |||||||||
| 67 | my @validators_list = ref $self->validators_list eq 'HASH' | ||||||||||
| 68 | 0 | ? ( map { $_, $self->validators_list->{$_} } | |||||||||
| 69 | 0 | sort { $a cmp $b } | |||||||||
| 70 | 0 | keys %{$self->validators_list} ) | |||||||||
| 71 | 19 | ▶ | : ( @{$self->validators_list} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 19 | ▶ | while (@validators_list) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 22 | my ( $key, $arg ) = splice(@validators_list,0,2); | |||||||||
| 74 | 22 | ▶ | next if $key eq 'label'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 19 | my %args; | |||||||||
| 76 | 19 | ▶ | if (ref $arg eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 10 | %args = %{$arg}; | |||||||||
| 78 | } else { | ||||||||||
| 79 | 9 | $args{arg} = $arg; | |||||||||
| 80 | } | ||||||||||
| 81 | 19 | $args{syccess_field} = $self; | |||||||||
| 82 | 19 | push @validators, $self->load_class_by_key($key)->new( | |||||||||
| 83 | %validators_args, %args | ||||||||||
| 84 | ); | ||||||||||
| 85 | } | ||||||||||
| 86 | 19 | return [ @validators ]; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | has load_class_cache => ( | ||||||||||
| 90 | is => 'ro', | ||||||||||
| 91 | init_arg => undef, | ||||||||||
| 92 | 19 | ▶ | default => sub {{}}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 9 | ); | |||||||||
| 94 | |||||||||||
| 95 | 19 | ▶ | sub load_class_by_key { | ||||||||
|
Subroutine
load_class_by_key: called CC=7 SCAR=21.0
Pod
undocumented
| |||||||||||
| 96 | 19 | my ( $self, $key ) = @_; | |||||||||
| 97 | 19 | ▶ | return $self->load_class_cache->{$key} if defined $self->load_class_cache->{$key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 19 | my $class; | |||||||||
| 99 | 19 | ▶ | if ($key =~ m/::/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | ▶ | if (can_load( modules => { $key, 0 } )) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | $class = $key; | |||||||||
| 102 | } | ||||||||||
| 103 | } else { | ||||||||||
| 104 | 19 | my $module = $key; | |||||||||
| 105 | 19 | $module =~ s/_([a-z])/\U$1/g; | |||||||||
| 106 | 19 | $module = ucfirst($module); | |||||||||
| 107 | 19 | my @namespaces = @{$self->syccess->validator_namespaces}; | |||||||||
| 108 | 19 | for my $namespace (@namespaces) { | |||||||||
| 109 | 19 | my $can_class = $namespace.'::'.$module; | |||||||||
| 110 | 19 | ▶ | if (can_load( modules => { $can_class, 0 } )) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 19 | $class = $can_class; | |||||||||
| 112 | 19 | last; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | } | ||||||||||
| 116 | 19 | ▶ | die __PACKAGE__." can't load validator for ".$key unless $class; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 19 | return $self->load_class_cache->{$key} = use_module($class); | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 43 | ▶ | sub validate { | ||||||||
|
Subroutine
validate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 121 | 43 | my ( $self, %params ) = @_; | |||||||||
| 122 | 43 | my @validators = @{$self->validators}; | |||||||||
| 123 | 43 | my @messages; | |||||||||
| 124 | 43 | for my $validator (@validators) { | |||||||||
| 125 | 43 | push @messages, $validator->validate(%params); | |||||||||
| 126 | } | ||||||||||
| 127 | 43 | return @messages; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 9 | 1; | |||||||||
| 131 | |||||||||||
| 132 | __END__ | ||||||||||