1 package MooseX::ClassAttribute::Trait::Attribute;
6 use namespace::autoclean;
9 # This is the worst role evar! Really, this should be a subclass,
10 # because it overrides a lot of behavior. However, as a subclass it
11 # won't cooperate with _other_ subclasses.
13 around '_process_options' => sub {
19 confess 'A class attribute cannot be required'
20 if $options->{required};
22 return $class->$orig( $name, $options );
25 around 'attach_to_class' => sub {
31 $self->initialize_instance_slot($meta, $meta->name);
34 override set_initial_value => sub {
35 my ($self, $instance, $value) = @_;
36 $self->_set_initial_slot_value(
44 before 'detach_from_class' => sub {
45 shift->clear_value(shift);
48 sub set_slot_value { $_[0]->set_value($_[0], $_[3]) }
50 around 'set_value' => sub {
53 shift; # ignoring instance or class name
55 $self->associated_class()
56 ->set_class_attribute_value( $self->name() => $value );
59 around 'get_value' => sub {
63 return $self->associated_class()
64 ->get_class_attribute_value( $self->name() );
67 around 'has_value' => sub {
71 return $self->associated_class()
72 ->has_class_attribute_value( $self->name() );
75 around 'clear_value' => sub {
79 return $self->associated_class()
80 ->clear_class_attribute_value( $self->name() );
84 sub _inline_instance_get {}
86 around ['inline_get', '_inline_instance_get'] => sub {
87 my ($orig, $self) = @_;
89 return $self->associated_class()
90 ->inline_get_class_slot_value( $self->slots() );
93 sub _inline_weaken_value {}
94 around ['_inline_weaken_value'] => sub {
95 my ($orig, $self, $instance, $value) = @_;
96 return '' unless $self->is_weak_ref;
98 $self->associated_class->inline_weaken_class_slot_value( $self->slots() )
99 . 'if ref ' . $value . ';';
102 sub _inline_instance_set {}
103 around ['_inline_instance_set'] => sub {
104 my ($orig, $self, undef, $value) = @_;
106 my $meta = $self->associated_class();
109 = $meta->inline_set_class_slot_value( $self->slots(), $value ) . ";";
115 around ['inline_set'] => sub {
116 my ($orig, $self, undef, $value) = @_;
118 my $meta = $self->associated_class();
121 = $meta->inline_set_class_slot_value( $self->slots(), $value ) . ";";
123 .= $self->_inline_weaken_value( $self->slots(), $value );
128 sub _inline_instance_has {}
130 around ['inline_has', '_inline_instance_has'] => sub {
131 my ($orig, $self) = @_;
133 return $self->associated_class()
134 ->inline_is_class_slot_initialized( $self->slots() );
137 sub _inline_clear_value {}
139 around ['inline_clear', '_inline_clear_value'] => sub {
140 my ($orig, $self) = @_;
142 return $self->associated_class()
143 ->inline_deinitialize_class_slot( $self->slots() );
148 # ABSTRACT: A trait for class attributes
156 This role modifies the behavior of class attributes in various
157 ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
158 if it were then it couldn't be combined with other attribute
159 metaclasses, like C<MooseX::AttributeHelpers>.
161 There are no new public methods implemented by this role. All it does
162 is change the behavior of a number of existing methods.
166 See L<MooseX::ClassAttribute> for details.