« blib/lib/Class/MOP/Mixin/HasOverloads.pm
↑ Summary
blib/lib/Moose/Meta/Method/Accessor/Native/Writer.pm »
| 1 | package Class::MOP::Mixin::HasAttributes; | ||||||||||
| 2 | 459 | our $VERSION = '2.4001'; | |||||||||
| 3 | |||||||||||
| 4 | 459 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 459 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 459 | ▶ | use Scalar::Util 'blessed'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 459 | ▶ | use parent 'Class::MOP::Mixin'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 58611 | ▶ | sub add_attribute { | ||||||||
|
Subroutine
add_attribute: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 12 | 58611 | my $self = shift; | |||||||||
| 13 | |||||||||||
| 14 | 58611 | ▶ | my $attribute | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | = blessed( $_[0] ) ? $_[0] : $self->attribute_metaclass->new(@_); | ||||||||||
| 16 | |||||||||||
| 17 | 58609 | ▶ | ( $attribute->isa('Class::MOP::Mixin::AttributeCore') ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | || $self->_throw_exception( AttributeMustBeAnClassMOPMixinAttributeCoreOrSubclass => attribute => $attribute, | ||||||||||
| 19 | class_name => $self->name, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 58607 | $self->_attach_attribute($attribute); | |||||||||
| 23 | |||||||||||
| 24 | 58607 | my $attr_name = $attribute->name; | |||||||||
| 25 | |||||||||||
| 26 | 58607 | ▶ | $self->remove_attribute($attr_name) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | if $self->has_attribute($attr_name); | ||||||||||
| 28 | |||||||||||
| 29 | 58607 | my $order = ( scalar keys %{ $self->_attribute_map } ); | |||||||||
| 30 | 58607 | $attribute->_set_insertion_order($order); | |||||||||
| 31 | |||||||||||
| 32 | 58607 | $self->_attribute_map->{$attr_name} = $attribute; | |||||||||
| 33 | |||||||||||
| 34 | # This method is called to allow for installing accessors. Ideally, we'd | ||||||||||
| 35 | # use method overriding, but then the subclass would be responsible for | ||||||||||
| 36 | # making the attribute, which would end up with lots of code | ||||||||||
| 37 | # duplication. Even more ideally, we'd use augment/inner, but this is | ||||||||||
| 38 | # Class::MOP! | ||||||||||
| 39 | 58607 | ▶ | $self->_post_add_attribute($attribute) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | if $self->can('_post_add_attribute'); | ||||||||||
| 41 | |||||||||||
| 42 | 58585 | return $attribute; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 90374 | ▶ | sub has_attribute { | ||||||||
|
Subroutine
has_attribute: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 46 | 90374 | my ( $self, $attribute_name ) = @_; | |||||||||
| 47 | |||||||||||
| 48 | 90374 | ▶ | ( defined $attribute_name ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | || $self->_throw_exception( MustDefineAnAttributeName => class_name => $self->name ); | ||||||||||
| 50 | |||||||||||
| 51 | 90372 | exists $self->_attribute_map->{$attribute_name}; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 55876 | ▶ | sub get_attribute { | ||||||||
|
Subroutine
get_attribute: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 55 | 55876 | my ( $self, $attribute_name ) = @_; | |||||||||
| 56 | |||||||||||
| 57 | 55876 | ▶ | ( defined $attribute_name ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | || $self->_throw_exception( MustDefineAnAttributeName => class_name => $self->name ); | ||||||||||
| 59 | |||||||||||
| 60 | 55874 | return $self->_attribute_map->{$attribute_name}; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 51 | ▶ | sub remove_attribute { | ||||||||
|
Subroutine
remove_attribute: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 64 | 51 | my ( $self, $attribute_name ) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 51 | ▶ | ( defined $attribute_name ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | || $self->_throw_exception( MustDefineAnAttributeName => class_name => $self->name ); | ||||||||||
| 68 | |||||||||||
| 69 | 49 | my $removed_attribute = $self->_attribute_map->{$attribute_name}; | |||||||||
| 70 | 48 | ▶ | return unless defined $removed_attribute; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | 47 | delete $self->_attribute_map->{$attribute_name}; | |||||||||
| 73 | |||||||||||
| 74 | 47 | return $removed_attribute; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 15615 | ▶ | sub get_attribute_list { | ||||||||
|
Subroutine
get_attribute_list: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 78 | 15615 | my $self = shift; | |||||||||
| 79 | 15615 | keys %{ $self->_attribute_map }; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 126 | ▶ | sub _restore_metaattributes_from { | ||||||||
|
Subroutine
_restore_metaattributes_from: called CC=2 SCAR=7.0
| |||||||||||
| 83 | 126 | my $self = shift; | |||||||||
| 84 | 126 | my ($old_meta) = @_; | |||||||||
| 85 | |||||||||||
| 86 | 0 | for my $attr (sort { $a->insertion_order <=> $b->insertion_order } | |||||||||
| 87 | 22 | map { $old_meta->get_attribute($_) } | |||||||||
| 88 | $old_meta->get_attribute_list) { | ||||||||||
| 89 | 22 | $attr->_make_compatible_with($self->attribute_metaclass); | |||||||||
| 90 | 22 | $self->add_attribute($attr); | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 459 | 1; | |||||||||
| 95 | |||||||||||
| 96 | # ABSTRACT: Methods for metaclasses which have attributes | ||||||||||
| 97 | |||||||||||
| 98 | __END__ | ||||||||||