| 1 | package MooseX::ClassAttribute::Trait::Class; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 8 | our $VERSION = '0.29'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use MooseX::ClassAttribute::Trait::Attribute; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | with 'MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has _class_attribute_values => ( | ||||||||||||||||||||||||||||
| 17 | traits => ['Hash'], | ||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 20 | handles => { | ||||||||||||||||||||||||||||
| 21 | 'get_class_attribute_value' => 'get', | ||||||||||||||||||||||||||||
| 22 | 'set_class_attribute_value' => 'set', | ||||||||||||||||||||||||||||
| 23 | 'has_class_attribute_value' => 'exists', | ||||||||||||||||||||||||||||
| 24 | 'clear_class_attribute_value' => 'delete', | ||||||||||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||||||||||
| 26 | lazy => 1, | ||||||||||||||||||||||||||||
| 27 | 18 | ▶ | default => sub { $_[0]->_class_attribute_values_hashref() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 8 | init_arg => undef, | |||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 95 | ▶ | around add_class_attribute => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 32 | 95 | my $orig = shift; | |||||||||||||||||||||||||||
| 33 | 95 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 95 | ▶ | my $attr = ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("Class::MOP::Attribute")
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa("Class::MOP::Attribute")
builtin::blessed($_[0])
$_[0]->isa("Class::MOP::Attribute")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Class::MOP::Attribute")
| |||||||||||||||||||||||||||||
| 35 | blessed $_[0] && $_[0]->isa('Class::MOP::Attribute') | ||||||||||||||||||||||||||||
| 36 | ? $_[0] | ||||||||||||||||||||||||||||
| 37 | : $self->_process_class_attribute(@_) | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 95 | $self->$orig($attr); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 95 | return $attr; | |||||||||||||||||||||||||||
| 43 | 8 | }; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 95 | ▶ | sub _post_add_class_attribute { | ||||||||||||||||||||||||||
|
Subroutine
_post_add_class_attribute: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 46 | 95 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 95 | my $attr = shift; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 95 | my $name = $attr->name(); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 95 | my $e = do { | |||||||||||||||||||||||||||
| 52 | 95 | local $@; | |||||||||||||||||||||||||||
| 53 | 95 | eval { $attr->install_accessors() }; | |||||||||||||||||||||||||||
| 54 | 95 | $@; | |||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 95 | ▶ | if ($e) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $self->remove_attribute($name); | |||||||||||||||||||||||||||
| 59 | 0 | die $e; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 95 | ▶ | sub _attach_class_attribute { | ||||||||||||||||||||||||||
|
Subroutine
_attach_class_attribute: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 95 | my ( $self, $attribute ) = @_; | |||||||||||||||||||||||||||
| 65 | 95 | $attribute->attach_to_class($self); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # It'd be nice if I didn't have to replicate this for class | ||||||||||||||||||||||||||||
| 69 | # attributes, since it's basically just a copy of | ||||||||||||||||||||||||||||
| 70 | # Moose::Meta::Class->_process_attribute | ||||||||||||||||||||||||||||
| 71 | 56 | ▶ | sub _process_class_attribute { | ||||||||||||||||||||||||||
|
Subroutine
_process_class_attribute: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||
| 72 | 56 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | 56 | my $name = shift; | |||||||||||||||||||||||||||
| 74 | 56 | my @args = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | @args = %{ $args[0] } if scalar @args == 1 && ref( $args[0] ) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @args == 1 and ref $args[0] eq "HASH"
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "HASH"
scalar @args == 1
ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= scalar @args == 1B= ref $args[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 56 | ▶ | if ( $name =~ /^\+(.*)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 4 | return $self->_process_inherited_class_attribute( $1, @args ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||
| 82 | 52 | return $self->_process_new_class_attribute( $name, @args ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 52 | ▶ | sub _process_new_class_attribute { | ||||||||||||||||||||||||||
|
Subroutine
_process_new_class_attribute: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 87 | 52 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 52 | my $name = shift; | |||||||||||||||||||||||||||
| 89 | 52 | my %p = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 52 | ▶ | if ( $p{traits} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 6 | push @{ $p{traits} }, 'MooseX::ClassAttribute::Trait::Attribute'; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 46 | $p{traits} = ['MooseX::ClassAttribute::Trait::Attribute']; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 52 | return Moose::Meta::Attribute->interpolate_class_and_new( $name, %p ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | ▶ | sub _process_inherited_class_attribute { | ||||||||||||||||||||||||||
|
Subroutine
_process_inherited_class_attribute: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 102 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | 4 | my $name = shift; | |||||||||||||||||||||||||||
| 104 | 4 | my %p = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 4 | my $inherited_attr = $self->find_class_attribute_by_name($name); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 4 | ▶ | ( defined $inherited_attr ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | || confess | ||||||||||||||||||||||||||||
| 110 | "Could not find an attribute by the name of '$name' to inherit from"; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 4 | return $inherited_attr->clone_and_inherit_options(%p); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | around remove_class_attribute => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 116 | 1 | my $orig = shift; | |||||||||||||||||||||||||||
| 117 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | my $removed_attr = $self->$orig(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | or return; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | $removed_attr->remove_accessors(); | |||||||||||||||||||||||||||
| 123 | 1 | $removed_attr->detach_from_class(); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | return $removed_attr; | |||||||||||||||||||||||||||
| 126 | 8 | }; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2 | ▶ | sub get_all_class_attributes { | ||||||||||||||||||||||||||
|
Subroutine
get_all_class_attributes: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | my %attrs = map { | ||||||||||||||||||||||||||||
| 132 | 2 | my $meta = Class::MOP::class_of($_); | |||||||||||||||||||||||||||
| 133 | $meta && $meta->can('_class_attribute_map') | ||||||||||||||||||||||||||||
| 134 | 5 | ▶ | ? %{ $meta->_class_attribute_map() } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$meta and $meta->can("_class_attribute_map")
MC/DC50%1/2
$meta and $meta->can("_class_attribute_map")
$meta
$meta->can("_class_attribute_map")
Truth table66%2/3
A and B
A= $metaB= $meta->can("_class_attribute_map")
| |||||||||||||||||||||||||||||
| 135 | : () | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | reverse $self->linearized_isa; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 2 | return values %attrs; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub compute_all_applicable_class_attributes { | ||||||||||||||||||||||||||
|
Subroutine
compute_all_applicable_class_attributes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 0 | warn | |||||||||||||||||||||||||||
| 144 | 'The compute_all_applicable_class_attributes method has been deprecated.' | ||||||||||||||||||||||||||||
| 145 | . " Use get_all_class_attributes instead.\n"; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | shift->compute_all_applicable_class_attributes(@_); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 4 | ▶ | sub find_class_attribute_by_name { | ||||||||||||||||||||||||||
|
Subroutine
find_class_attribute_by_name: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 4 | my $name = shift; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 4 | foreach my $class ( $self->linearized_isa() ) { | |||||||||||||||||||||||||||
| 155 | 8 | ▶ | my $meta = Class::MOP::class_of($class) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | or next; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 8 | ▶ | return $meta->get_class_attribute($name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$meta->can("has_class_attribute") and $meta->has_class_attribute($name)
MC/DC50%1/2
$meta->can("has_class_attribute") and $meta->has_class_attribute($name)
$meta->can("has_class_attribute")
$meta->has_class_attribute($name)
Truth table66%2/3
A and B
A= $meta->can("has_class_attribute")B= $meta->has_class_attribute($name)
| |||||||||||||||||||||||||||||
| 159 | if $meta->can('has_class_attribute') | ||||||||||||||||||||||||||||
| 160 | && $meta->has_class_attribute($name); | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 18 | ▶ | sub _class_attribute_values_hashref { | ||||||||||||||||||||||||||
|
Subroutine
_class_attribute_values_hashref: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 170 | 18 | return \%{ $self->_class_attribute_var_name() }; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 273 | ▶ | sub _class_attribute_var_name { | ||||||||||||||||||||||||||
|
Subroutine
_class_attribute_var_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | 273 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 273 | return $self->name() . q'::__ClassAttributeValues'; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 255 | ▶ | sub _inline_class_slot_access { | ||||||||||||||||||||||||||
|
Subroutine
_inline_class_slot_access: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 180 | 255 | my $self = shift; | |||||||||||||||||||||||||||
| 181 | 255 | my $name = shift; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | return | ||||||||||||||||||||||||||||
| 184 | 255 | '$' | |||||||||||||||||||||||||||
| 185 | . $self->_class_attribute_var_name . '{"' | ||||||||||||||||||||||||||||
| 186 | . quotemeta($name) . '"}'; | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 132 | ▶ | sub _inline_get_class_slot_value { | ||||||||||||||||||||||||||
|
Subroutine
_inline_get_class_slot_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 190 | 132 | my $self = shift; | |||||||||||||||||||||||||||
| 191 | 132 | my $name = shift; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 132 | return $self->_inline_class_slot_access($name); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 72 | ▶ | sub _inline_set_class_slot_value { | ||||||||||||||||||||||||||
|
Subroutine
_inline_set_class_slot_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 197 | 72 | my $self = shift; | |||||||||||||||||||||||||||
| 198 | 72 | my $name = shift; | |||||||||||||||||||||||||||
| 199 | 72 | my $val_name = shift; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 72 | return $self->_inline_class_slot_access($name) . ' = ' . $val_name; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 37 | ▶ | sub _inline_is_class_slot_initialized { | ||||||||||||||||||||||||||
|
Subroutine
_inline_is_class_slot_initialized: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | 37 | my $self = shift; | |||||||||||||||||||||||||||
| 206 | 37 | my $name = shift; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 37 | return 'exists ' . $self->_inline_class_slot_access($name); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 7 | ▶ | sub _inline_deinitialize_class_slot { | ||||||||||||||||||||||||||
|
Subroutine
_inline_deinitialize_class_slot: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 212 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 213 | 7 | my $name = shift; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 7 | return 'delete ' . $self->_inline_class_slot_access($name); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 7 | ▶ | sub _inline_weaken_class_slot_value { | ||||||||||||||||||||||||||
|
Subroutine
_inline_weaken_class_slot_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 219 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 220 | 7 | my $name = shift; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | return | ||||||||||||||||||||||||||||
| 223 | 7 | 'Scalar::Util::weaken( ' | |||||||||||||||||||||||||||
| 224 | . $self->_inline_class_slot_access($name) . ')'; | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 8 | 1; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # ABSTRACT: A trait for classes with class attributes | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | __END__ | ||||||||||||||||||||||||||||