| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||
| 3 | # File: GX/Class/Object.pm | ||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package GX::Class::Object; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 208 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 208 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 208 | ▶ | use base 'GX::Class'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 208 | ▶ | use GX::Exception (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 17 | # Export mechanism | ||||||||||||||||||||
| 18 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1223 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1223 | my $package = shift; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1223 | ▶ | return unless $package eq __PACKAGE__; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 573 | my $caller = caller(); | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 573 | ▶ | if ( $caller eq 'main' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 0 | GX::Exception->complain( "Cannot import \"$package\" into package \"main\"" ); | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | { | ||||||||||||||||||||
| 33 | 208 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 573 | *{"${caller}::__initialize_instance"} = \&__initialize_instance; | |||||||||||||||||||
| 35 | 573 | *{"${caller}::__clear_instance"} = \&__clear_instance; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 573 | unshift( @_, 'GX::Class', superclass => $package ); | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 573 | goto &GX::Class::import; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 46 | # Constructor | ||||||||||||||||||||
| 47 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 3481 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 3481 | my $class = shift; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 3481 | my $self = bless {}, $class; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 3481 | eval { | |||||||||||||||||||
| 56 | 3481 | $self->__initialize_instance( @_ ) | |||||||||||||||||||
| 57 | }; | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 3481 | ▶ | GX::Exception->complain( $@ ) if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 3391 | return $self; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 67 | # Public methods | ||||||||||||||||||||
| 68 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 29 | ▶ | sub clear { | ||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 29 | return shift->__clear_instance( @_ ); | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | sub dump { | ||||||||||||||||||
|
Subroutine
dump: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | require Data::Dumper; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | my $string = Data::Dumper::Dumper( $self ); | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | ▶ | if ( defined wantarray ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | return $string; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||
| 88 | 0 | warn $string; | |||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 96 | # Internal methods | ||||||||||||||||||||
| 97 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 992 | ▶ | sub __build { | ||||||||||||||||||
|
Subroutine
__build: called CC=3 SCAR=12.0
| |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 992 | my $class = shift; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 992 | ▶ | if ( ref $class ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 0 | GX::Exception->complain( "__build() is a class method" ); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 992 | ▶ | if ( $class eq __PACKAGE__ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 0 | GX::Exception->complain( "__build() cannot be called on $class" ); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 992 | $class->__build_initialize_instance; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 992 | $class->__build_clear_instance; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 992 | return; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 992 | ▶ | sub __build_clear_instance { | ||||||||||||||||||
|
Subroutine
__build_clear_instance: called CC=5 SCAR=16.4
| |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 992 | my $class = shift; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 992 | ▶ | if ( ref $class ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 0 | GX::Exception->complain( "__build_clear_instance() is a class method" ); | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 992 | ▶ | if ( $class eq __PACKAGE__ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 0 | GX::Exception->complain( "__build_clear_instance() cannot be called on $class" ); | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 992 | my $code = []; | |||||||||||||||||||
| 132 | 992 | my $vars = {}; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 992 | push @$code, | |||||||||||||||||||
| 135 | 'sub {', | ||||||||||||||||||||
| 136 | 'my $self = shift;'; | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 992 | for my $attribute ( $class->meta->all_instance_attributes ) { | |||||||||||||||||||
| 139 | 3642 | ▶ | next if $attribute->is_sticky; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 3641 | my $data = { 'invocant' => '$self' }; | |||||||||||||||||||
| 141 | 3641 | $attribute->inline_clear_instance_slot( $data, $code, $vars ); | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 992 | push @$code, | |||||||||||||||||||
| 145 | 'return;', | ||||||||||||||||||||
| 146 | '}'; | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 992 | $class->meta->add_method( '__clear_instance' => GX::Meta::Util::eval_code( $code, $vars ) ); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 992 | return; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 992 | ▶ | sub __build_initialize_instance { | ||||||||||||||||||
|
Subroutine
__build_initialize_instance: called CC=10 SCAR=23.1
| |||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 992 | my $class = shift; | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 992 | ▶ | if ( ref $class ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 0 | GX::Exception->complain( "__build_initialize_instance() is a class method" ); | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 992 | ▶ | if ( $class eq __PACKAGE__ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | 0 | GX::Exception->complain( "__build_initialize_instance() cannot be called on $class" ); | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 992 | my $meta = $class->meta; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 992 | my $code = []; | |||||||||||||||||||
| 169 | 992 | my $vars = {}; | |||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 992 | push @$code, | |||||||||||||||||||
| 172 | 'sub {', | ||||||||||||||||||||
| 173 | 'my $self = shift;', | ||||||||||||||||||||
| 174 | 'my $args = { @_ };'; | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 992 | my %attributes; | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 992 | for my $attribute ( $meta->all_instance_attributes ) { | |||||||||||||||||||
| 179 | 3642 | push @{ $attributes{$attribute->class->name} }, $attribute; | |||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 992 | ▶ | for my $class_name ( grep { $_->isa( __PACKAGE__ ) && $_ ne __PACKAGE__ } reverse $meta->linearized_isa ) { | ||||||||||||||||||
|
Condition100%3/3
$_->isa(__PACKAGE__) && $_ ne __PACKAGE__
MC/DC100%2/2
$_->isa(__PACKAGE__) && $_ ne __PACKAGE__
$_->isa(__PACKAGE__)
$_ ne __PACKAGE__
Truth table100%3/3
A && B
A= $_->isa(__PACKAGE__)B= $_ ne __PACKAGE__
| |||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 1541 | ▶ | if ( $class_name->meta->has_method( '__initialize' ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | 50 | push @$code, $class_name . '::__initialize( $self, $args );'; | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 1541 | ▶ | if ( $attributes{$class_name} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 1023 | for my $attribute ( @{$attributes{$class_name}} ) { | |||||||||||||||||||
| 191 | 3642 | my $data = { 'invocant' => '$self', 'args' => '$args' }; | |||||||||||||||||||
| 192 | 3642 | $attribute->inline_initialize_instance_slot( $data, $code, $vars ); | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 1541 | ▶ | if ( $class_name->meta->has_method( '__finalize' ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 198 | 27 | push @$code, $class_name . '::__finalize( $self, $args );'; | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 992 | push @$code, | |||||||||||||||||||
| 204 | 'return;', | ||||||||||||||||||||
| 205 | '}'; | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 992 | $meta->add_method( '__initialize_instance' => GX::Meta::Util::eval_code( $code, $vars ) ); | |||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | 992 | return; | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 13 | ▶ | sub __clear_instance { | ||||||||||||||||||
|
Subroutine
__clear_instance: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 13 | my $self = shift; | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 13 | for my $attribute ( $self->meta->all_instance_attributes ) { | |||||||||||||||||||
| 218 | 142 | ▶ | $attribute->clear_instance_slot( $self ) unless $attribute->is_sticky; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 13 | return; | |||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 0 | ▶ | sub __finalize { | ||||||||||||||||||
|
Subroutine
__finalize: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | # Abstract method | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 0 | ▶ | sub __initialize { | ||||||||||||||||||
|
Subroutine
__initialize: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | # Abstract method | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | } | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 39 | ▶ | sub __initialize_instance { | ||||||||||||||||||
|
Subroutine
__initialize_instance: called CC=8 SCAR=21.4
| |||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 39 | my $self = shift; | |||||||||||||||||||
| 240 | 39 | my %data = @_; | |||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | 39 | my $meta = $self->meta; | |||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | 39 | my %attributes; | |||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | 39 | for my $attribute ( $meta->all_instance_attributes ) { | |||||||||||||||||||
| 247 | 409 | push @{ $attributes{$attribute->class->name} }, $attribute; | |||||||||||||||||||
| 248 | } | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 39 | ▶ | for my $class_name ( grep { $_->isa( __PACKAGE__ ) && $_ ne __PACKAGE__ } reverse $meta->linearized_isa ) { | ||||||||||||||||||
|
Condition100%3/3
$_->isa(__PACKAGE__) && $_ ne __PACKAGE__
MC/DC100%2/2
$_->isa(__PACKAGE__) && $_ ne __PACKAGE__
$_->isa(__PACKAGE__)
$_ ne __PACKAGE__
Truth table100%3/3
A && B
A= $_->isa(__PACKAGE__)B= $_ ne __PACKAGE__
| |||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | 41 | ▶ | if ( defined &{"${class_name}::__initialize"} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 253 | 0 | ( \&{"${class_name}::__initialize"} )->( $self, \%data ); | |||||||||||||||||||
| 254 | } | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 41 | ▶ | if ( $attributes{$class_name} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 39 | for my $attribute ( @{ $attributes{$class_name} } ) { | |||||||||||||||||||
| 259 | 252 | $attribute->initialize_instance_slot( $self, \%data ); | |||||||||||||||||||
| 260 | } | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | } | ||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | 26 | ▶ | if ( defined &{"${class_name}::__finalize"} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 265 | 0 | ( \&{"${class_name}::__finalize"} )->( $self, \%data ); | |||||||||||||||||||
| 266 | } | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | } | ||||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | 24 | return; | |||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | } | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | 208 | 1; | |||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | __END__ | ||||||||||||||||||||