| 1 | package FormValidator::Nested::ProfileProvider; | ||||||||||
| 2 | 18 | ▶ | use Any::Moose '::Role'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 18 | ▶ | use Any::Moose 'X::AttributeHelpers'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 18 | ▶ | use namespace::clean -except => 'meta'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 18 | ▶ | use FormValidator::Nested::Profile; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 18 | ▶ | use Hash::Merge; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | my $behavior = { | ||||||||||
| 9 | SCALAR => { | ||||||||||
| 10 | 2 | ▶ | SCALAR => sub { $_[1] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 0 | ▶ | ARRAY => sub { [ @{$_[1]} ] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 12 | 0 | ▶ | HASH => sub { $_[1] } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 13 | }, | ||||||||||
| 14 | ARRAY => { | ||||||||||
| 15 | 0 | ▶ | SCALAR => sub { $_[1] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 16 | 2 | ▶ | ARRAY => sub { [ @{$_[1]} ] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 0 | ▶ | HASH => sub { $_[1] } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 18 | }, | ||||||||||
| 19 | HASH => { | ||||||||||
| 20 | 0 | ▶ | SCALAR => sub { $_[1] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 21 | 0 | ▶ | ARRAY => sub { [ @{$_[1]} ] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 22 | 8 | ▶ | HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | }, | ||||||||||
| 24 | 18 | }; | |||||||||
| 25 | 18 | Hash::Merge::specify_behavior($behavior); | |||||||||
| 26 | |||||||||||
| 27 | 18 | requires 'get_profile_data'; | |||||||||
| 28 | |||||||||||
| 29 | has '_profiles' => ( | ||||||||||
| 30 | metaclass => 'Collection::Hash', | ||||||||||
| 31 | is => 'rw', | ||||||||||
| 32 | isa => 'HashRef[FormValidator::Nested::Profile]', | ||||||||||
| 33 | 17 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 18 | provides => { | |||||||||
| 35 | set => '_set_profile', | ||||||||||
| 36 | get => '_get_profile', | ||||||||||
| 37 | exists => '_exists_profile', | ||||||||||
| 38 | }, | ||||||||||
| 39 | ); | ||||||||||
| 40 | 18 | has 'profile_keys' => ( | |||||||||
| 41 | metaclass => 'Collection::Array', | ||||||||||
| 42 | is => 'rw', | ||||||||||
| 43 | isa => 'ArrayRef[Str]', | ||||||||||
| 44 | lazy_build => 1, | ||||||||||
| 45 | provides => { | ||||||||||
| 46 | elements => 'get_profile_keys', | ||||||||||
| 47 | push => 'push_profile_keys', | ||||||||||
| 48 | }, | ||||||||||
| 49 | ); | ||||||||||
| 50 | 18 | has 'init_read_all_profile' => ( | |||||||||
| 51 | is => 'ro', | ||||||||||
| 52 | isa => 'Bool', | ||||||||||
| 53 | default => 0, | ||||||||||
| 54 | ); | ||||||||||
| 55 | 18 | has 'visitor' => ( | |||||||||
| 56 | is => 'ro', | ||||||||||
| 57 | isa => 'Data::Visitor::Callback', | ||||||||||
| 58 | ); | ||||||||||
| 59 | 18 | has 'filter' => ( | |||||||||
| 60 | is => 'ro', | ||||||||||
| 61 | isa => 'CodeRef', | ||||||||||
| 62 | ); | ||||||||||
| 63 | |||||||||||
| 64 | 17 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 65 | 17 | my $self = shift; | |||||||||
| 66 | |||||||||||
| 67 | 17 | ▶ | if ( $self->init_read_all_profile ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 2 | $self->read_all_profile; | |||||||||
| 69 | } | ||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | 2 | ▶ | sub read_all_profile { | ||||||||
|
Subroutine
read_all_profile: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 74 | 2 | my $self = shift; | |||||||||
| 75 | |||||||||||
| 76 | 2 | foreach my $profile_key ( $self->get_profile_keys ) { | |||||||||
| 77 | 22 | $self->get_profile($profile_key); | |||||||||
| 78 | } | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 199 | ▶ | sub get_profile { | ||||||||
|
Subroutine
get_profile: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||
| 82 | 199 | my $self = shift; | |||||||||
| 83 | 199 | my $key = shift; | |||||||||
| 84 | |||||||||||
| 85 | 199 | ▶ | if ( !$self->_exists_profile($key) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 44 | my $data = $self->get_profile_data($key); | |||||||||
| 87 | 44 | ▶ | if ( !$data ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 2 | return 0; | |||||||||
| 89 | } | ||||||||||
| 90 | 42 | ▶ | if ( $self->visitor ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | $data = $self->visitor->visit($data); | |||||||||
| 92 | } | ||||||||||
| 93 | 42 | ▶ | if ( $self->filter ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 21 | $self->filter->($data); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 42 | ▶ | if ( $data->{extends} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 4 | $data = Hash::Merge::merge($self->get_profile($data->{extends})->data, $data); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 42 | $self->_set_profile($key => FormValidator::Nested::Profile->new({ | |||||||||
| 102 | provider => $self, | ||||||||||
| 103 | key => $key, | ||||||||||
| 104 | data => $data, | ||||||||||
| 105 | })); | ||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 196 | return $self->_get_profile($key); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 18 | 1; | |||||||||
| 112 | |||||||||||