| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||
| 3 | # File: GX/Meta/Attribute/Object.pm | ||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package GX::Meta::Attribute::Object; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 37 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 37 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 37 | ▶ | use base 'GX::Meta::Attribute::Scalar'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 37 | ▶ | use GX::Meta::Exception; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 37 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 19 | # Constants | ||||||||||||||||||||
| 20 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 37 | ▶ | use constant TYPE_CONSTRAINT => sub { blessed $_[1] }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 26 | # Public methods | ||||||||||||||||||||
| 27 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 16 | ▶ | sub add_delegator { | ||||||||||||||||||
|
Subroutine
add_delegator: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 16 | my $self = shift; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 16 | my $delegator = eval { | |||||||||||||||||||
| 34 | 16 | $self->create_delegator( @_ ); | |||||||||||||||||||
| 35 | }; | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 16 | ▶ | if ( ! $delegator ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 0 | GX::Exception->complain( | |||||||||||||||||||
| 39 | message => "Cannot create delegator", | ||||||||||||||||||||
| 40 | subexception => $@ | ||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 16 | push @{$self->{'delegators'}}, $delegator; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 16 | return $delegator; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 113 | ▶ | sub delegators { | ||||||||||||||||||
|
Subroutine
delegators: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 113 | ▶ | return @{ $_[0]->{'delegators'} || return }; | ||||||||||||||||||
|
Condition100%2/2
$_[0]{'delegators'} || (return)
MC/DC100%1/1
$_[0]{'delegators'} || (return)
$_[0]{'delegators'}
Truth table100%2/2
A
A= $_[0]{'delegators'}
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 99 | ▶ | sub install { | ||||||||||||||||||
|
Subroutine
install: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 99 | my $self = shift; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 99 | eval { | |||||||||||||||||||
| 61 | 99 | $self->SUPER::install; | |||||||||||||||||||
| 62 | 99 | $self->install_delegators; | |||||||||||||||||||
| 63 | }; | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 99 | ▶ | if ( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | complain $@; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 99 | return; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 101 | ▶ | sub install_delegators { | ||||||||||||||||||
|
Subroutine
install_delegators: called CC=4 SCAR=17.1
Pod
documented
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 101 | my $self = shift; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 101 | for my $delegator ( $self->delegators ) { | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 16 | ▶ | if ( my $method = $self->class->method( $delegator->name ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 0 | ▶ | next if $method->code == $delegator->code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | complain sprintf( "Cannot install delegator (method &%s already exists)", $method->full_name ); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||
| 84 | 16 | $self->class->add_method( $delegator->name, $delegator->code ); | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 101 | return; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 1 | ▶ | sub type { | ||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1 | return 'Object'; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 101 | # Internal methods | ||||||||||||||||||||
| 102 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 16 | ▶ | sub create_delegator { | ||||||||||||||||||
|
Subroutine
create_delegator: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 16 | my $self = shift; | |||||||||||||||||||
| 107 | 16 | my %args = @_; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 16 | ▶ | my $name = $args{'name'} // $self->name; | ||||||||||||||||||
|
Condition33%1/3
$args{"name"} // $self->name
MC/DC0%0/2
$args{"name"} // $self->name
$args{"name"}
$self->name
Truth table33%1/3
A // B
A= $args{"name"}B= $self->name
| |||||||||||||||||||||
| 110 | 16 | ▶ | my $to = $args{'to'} // $name; | ||||||||||||||||||
|
Condition66%2/3
$args{"to"} // $name
MC/DC0%0/2
$args{"to"} // $name
$args{"to"}
$name
Truth table66%2/3
A // B
A= $args{"to"}B= $name
| |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 16 | my $data = {}; | |||||||||||||||||||
| 113 | 16 | my $code = []; | |||||||||||||||||||
| 114 | 16 | my $vars = {}; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 16 | push @$code, | |||||||||||||||||||
| 117 | 'package ' . ref( $self ) . ';', | ||||||||||||||||||||
| 118 | 'sub {', | ||||||||||||||||||||
| 119 | 'my $invocant = shift;'; | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 16 | $data->{'invocant'} = '$invocant'; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 16 | $self->_inline_accessor_prologue( $data, $code, $vars ); | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 16 | $self->_inline_prepare_slot( $data, $code, $vars ); | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | push @$code, | ||||||||||||||||||||
| 128 | 16 | 'return ' . $data->{'slot'} . '->' . $to . '( @_ );', | |||||||||||||||||||
| 129 | '}'; | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 16 | my $delegator = eval { | |||||||||||||||||||
| 132 | 16 | GX::Meta::Delegator->new( | |||||||||||||||||||
| 133 | attribute => $self, | ||||||||||||||||||||
| 134 | name => $name, | ||||||||||||||||||||
| 135 | code => GX::Meta::Util::eval_code( $code, $vars ) | ||||||||||||||||||||
| 136 | ) | ||||||||||||||||||||
| 137 | }; | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 16 | ▶ | if ( ! $delegator ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 0 | complain $@; | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 16 | return $delegator; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 149 | # Private methods | ||||||||||||||||||||
| 150 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | ▶ | sub _get_delegators { | ||||||||||||||||||
|
Subroutine
_get_delegators: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | return $_[0]->{'delegators'}; | |||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 160 | # Object initialization | ||||||||||||||||||||
| 161 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 122 | ▶ | sub __initialize_delegators { | ||||||||||||||||||
|
Subroutine
__initialize_delegators: called CC=7 SCAR=19.5
| |||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 122 | my $self = shift; | |||||||||||||||||||
| 166 | 122 | my $args = shift; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 122 | my @delegators; | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 122 | ▶ | if ( exists $args->{'delegator'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 4 | ▶ | if ( defined $args->{'delegator'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 4 | push @delegators, $args->{'delegator'}; | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 4 | delete $args->{'delegator'}; | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 122 | ▶ | if ( exists $args->{'delegators'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 6 | ▶ | if ( ref $args->{'delegators'} ne 'ARRAY' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | 0 | throw "Invalid option (\"delegators\" must be an array reference)"; | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 6 | push @delegators, @{$args->{'delegators'}}; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 6 | delete $args->{'delegators'}; | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 122 | for my $delegator ( @delegators ) { | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 12 | ▶ | if ( ref $delegator eq 'HASH' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 195 | 4 | $self->add_delegator( %$delegator ); | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||
| 198 | 8 | $self->add_delegator( name => $delegator ); | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 122 | return; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 122 | ▶ | sub __initialize_other { | ||||||||||||||||||
|
Subroutine
__initialize_other: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | 122 | my $self = shift; | |||||||||||||||||||
| 210 | 122 | my $args = shift; | |||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 122 | $self->__initialize_delegators( $args ); | |||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 122 | return; | |||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 37 | 1; | |||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||||