| 1 | package MooseX::ClassAttribute::Trait::Attribute; | ||||||||||||||||||||||||||||
| 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 namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # This is the worst role evar! Really, this should be a subclass, | ||||||||||||||||||||||||||||
| 12 | # because it overrides a lot of behavior. However, as a subclass it | ||||||||||||||||||||||||||||
| 13 | # won't cooperate with _other_ subclasses. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 91 | ▶ | around _process_options => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 16 | 91 | my $orig = shift; | |||||||||||||||||||||||||||
| 17 | 91 | my $class = shift; | |||||||||||||||||||||||||||
| 18 | 91 | my $name = shift; | |||||||||||||||||||||||||||
| 19 | 91 | my $options = shift; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | confess 'A class attribute cannot be required' | ||||||||||||||||||||||||||||
| 22 | 91 | ▶ | if $options->{required}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 91 | return $class->$orig( $name, $options ); | |||||||||||||||||||||||||||
| 25 | 8 | }; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 95 | ▶ | after attach_to_class => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 28 | 95 | my $self = shift; | |||||||||||||||||||||||||||
| 29 | 95 | my $meta = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 95 | ▶ | $self->_initialize($meta) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | unless $self->is_lazy(); | ||||||||||||||||||||||||||||
| 33 | 8 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | before detach_from_class => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 1 | my $meta = shift; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | $self->clear_value($meta); | |||||||||||||||||||||||||||
| 40 | 8 | }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 73 | ▶ | sub _initialize { | ||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 43 | 73 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 73 | my $metaclass = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 73 | ▶ | if ( $self->has_default() ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 45 | $self->set_value( | |||||||||||||||||||||||||||
| 48 | undef, | ||||||||||||||||||||||||||||
| 49 | $self->default( $self->associated_class() ) | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | elsif ( $self->has_builder() ) { | ||||||||||||||||||||||||||||
| 53 | 7 | $self->set_value( undef, $self->_call_builder( $metaclass->name() ) ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 134 | ▶ | around default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 58 | 134 | my $orig = shift; | |||||||||||||||||||||||||||
| 59 | 134 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 134 | my $default = $self->$orig(); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 134 | ▶ | if ( $self->is_default_a_coderef() && @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->is_default_a_coderef and @_
MC/DC100%2/2
$self->is_default_a_coderef and @_
$self->is_default_a_coderef
@_
Truth table100%3/3
A and B
A= $self->is_default_a_coderefB= @_
| |||||||||||||||||||||||||||||
| 64 | 20 | return $default->(@_); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 114 | return $default; | |||||||||||||||||||||||||||
| 68 | 8 | }; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 7 | ▶ | around _call_builder => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 71 | 7 | shift; | |||||||||||||||||||||||||||
| 72 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 7 | my $builder = $self->builder(); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 7 | ▶ | return $class->$builder() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | if $class->can( $self->builder ); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | confess( "$class does not support builder method '" | |||||||||||||||||||||||||||
| 81 | . $self->builder | ||||||||||||||||||||||||||||
| 82 | . "' for attribute '" | ||||||||||||||||||||||||||||
| 83 | . $self->name | ||||||||||||||||||||||||||||
| 84 | . "'" ); | ||||||||||||||||||||||||||||
| 85 | 8 | }; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 52 | ▶ | around set_value => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 52 | shift; | |||||||||||||||||||||||||||
| 89 | 52 | my $self = shift; | |||||||||||||||||||||||||||
| 90 | 52 | shift; # ignoring instance or class name | |||||||||||||||||||||||||||
| 91 | 52 | my $value = shift; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 52 | $self->associated_class() | |||||||||||||||||||||||||||
| 94 | ->set_class_attribute_value( $self->name() => $value ); | ||||||||||||||||||||||||||||
| 95 | 8 | }; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | around get_value => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | 0 | shift; | |||||||||||||||||||||||||||
| 99 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | return $self->associated_class() | |||||||||||||||||||||||||||
| 102 | ->get_class_attribute_value( $self->name() ); | ||||||||||||||||||||||||||||
| 103 | 8 | }; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | around has_value => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 0 | shift; | |||||||||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | return $self->associated_class() | |||||||||||||||||||||||||||
| 110 | ->has_class_attribute_value( $self->name() ); | ||||||||||||||||||||||||||||
| 111 | 8 | }; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | ▶ | around clear_value => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 1 | shift; | |||||||||||||||||||||||||||
| 115 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | return $self->associated_class() | |||||||||||||||||||||||||||
| 118 | ->clear_class_attribute_value( $self->name() ); | ||||||||||||||||||||||||||||
| 119 | 8 | }; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 8 | ▶ | if ( $Moose::VERSION < 1.99 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | around inline_get => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 123 | 0 | shift; | |||||||||||||||||||||||||||
| 124 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | return $self->associated_class() | |||||||||||||||||||||||||||
| 127 | ->_inline_get_class_slot_value( $self->slots() ); | ||||||||||||||||||||||||||||
| 128 | 0 | }; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | around inline_set => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 131 | 0 | shift; | |||||||||||||||||||||||||||
| 132 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | 0 | shift; | |||||||||||||||||||||||||||
| 134 | 0 | my $value = shift; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $meta = $self->associated_class(); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | my $code | |||||||||||||||||||||||||||
| 139 | = $meta->_inline_set_class_slot_value( $self->slots(), $value ) | ||||||||||||||||||||||||||||
| 140 | . ";"; | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $code .= $meta->_inline_weaken_class_slot_value( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | $self->slots(), | ||||||||||||||||||||||||||||
| 143 | $value | ||||||||||||||||||||||||||||
| 144 | ) | ||||||||||||||||||||||||||||
| 145 | . " if ref $value;" | ||||||||||||||||||||||||||||
| 146 | if $self->is_weak_ref(); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | return $code; | |||||||||||||||||||||||||||
| 149 | 0 | }; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | around inline_has => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 152 | 0 | shift; | |||||||||||||||||||||||||||
| 153 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | return $self->associated_class() | |||||||||||||||||||||||||||
| 156 | ->_inline_is_class_slot_initialized( $self->slots() ); | ||||||||||||||||||||||||||||
| 157 | 0 | }; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | around inline_clear => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 160 | 0 | shift; | |||||||||||||||||||||||||||
| 161 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return $self->associated_class() | |||||||||||||||||||||||||||
| 164 | ->_inline_deinitialize_class_slot( $self->slots() ); | ||||||||||||||||||||||||||||
| 165 | 0 | }; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||||||||||
| 168 | 132 | ▶ | around _inline_instance_get => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 169 | 132 | shift; | |||||||||||||||||||||||||||
| 170 | 132 | my $self = shift; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 132 | return $self->associated_class() | |||||||||||||||||||||||||||
| 173 | ->_inline_get_class_slot_value( $self->slots() ); | ||||||||||||||||||||||||||||
| 174 | 8 | }; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 72 | ▶ | around _inline_instance_set => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 177 | 72 | shift; | |||||||||||||||||||||||||||
| 178 | 72 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | 72 | shift; | |||||||||||||||||||||||||||
| 180 | 72 | my $value = shift; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 72 | return $self->associated_class() | |||||||||||||||||||||||||||
| 183 | ->_inline_set_class_slot_value( $self->slots(), $value ); | ||||||||||||||||||||||||||||
| 184 | 8 | }; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 37 | ▶ | around _inline_instance_has => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 187 | 37 | shift; | |||||||||||||||||||||||||||
| 188 | 37 | my $self = shift; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 37 | return $self->associated_class() | |||||||||||||||||||||||||||
| 191 | ->_inline_is_class_slot_initialized( $self->slots() ); | ||||||||||||||||||||||||||||
| 192 | 8 | }; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 7 | ▶ | around _inline_instance_clear => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 195 | 7 | shift; | |||||||||||||||||||||||||||
| 196 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 7 | return $self->associated_class() | |||||||||||||||||||||||||||
| 199 | ->_inline_deinitialize_class_slot( $self->slots() ); | ||||||||||||||||||||||||||||
| 200 | 8 | }; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 72 | ▶ | around _inline_weaken_value => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 203 | 72 | shift; | |||||||||||||||||||||||||||
| 204 | 72 | my $self = shift; | |||||||||||||||||||||||||||
| 205 | 72 | shift; | |||||||||||||||||||||||||||
| 206 | 72 | my $value = shift; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 72 | ▶ | return unless $self->is_weak_ref(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | return ( | ||||||||||||||||||||||||||||
| 211 | 7 | $self->associated_class->_inline_weaken_class_slot_value( | |||||||||||||||||||||||||||
| 212 | $self->slots(), $value | ||||||||||||||||||||||||||||
| 213 | ), | ||||||||||||||||||||||||||||
| 214 | 'if ref ' . $value . ';', | ||||||||||||||||||||||||||||
| 215 | ); | ||||||||||||||||||||||||||||
| 216 | 8 | }; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 8 | 1; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # ABSTRACT: A trait for class attributes | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | __END__ | ||||||||||||||||||||||||||||