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 {
32 $self->_initialize($meta)
33 unless $self->is_lazy();
36 around detach_from_class => sub {
41 $self->clear_value($meta);
48 my $metaclass = shift;
50 if ( $self->has_default() ) {
51 $self->set_value( undef, $self->default() );
53 elsif ( $self->has_builder() ) {
54 $self->set_value( undef, $self->_call_builder( $metaclass->name() ) );
58 around default => sub {
62 my $default = $self->$orig();
64 if ( $self->is_default_a_coderef() ) {
65 return $default->( $self->associated_class() );
71 around _call_builder => sub {
76 my $builder = $self->builder();
78 return $class->$builder()
79 if $class->can( $self->builder );
81 confess( "$class does not support builder method '"
88 around set_value => sub {
91 shift; # ignoring instance or class name
94 $self->associated_class()
95 ->set_class_attribute_value( $self->name() => $value );
98 around get_value => sub {
102 return $self->associated_class()
103 ->get_class_attribute_value( $self->name() );
106 around has_value => sub {
110 return $self->associated_class()
111 ->has_class_attribute_value( $self->name() );
114 around clear_value => sub {
118 return $self->associated_class()
119 ->clear_class_attribute_value( $self->name() );
122 if ( $Moose::VERSION < 1.99 ) {
123 around inline_get => sub {
127 return $self->associated_class()
128 ->inline_get_class_slot_value( $self->slots() );
131 around inline_set => sub {
137 my $meta = $self->associated_class();
140 = $meta->inline_set_class_slot_value( $self->slots(), $value )
143 .= $meta->inline_weaken_class_slot_value( $self->slots(), $value )
145 if $self->is_weak_ref();
150 around inline_has => sub {
154 return $self->associated_class()
155 ->inline_is_class_slot_initialized( $self->slots() );
158 around inline_clear => sub {
162 return $self->associated_class()
163 ->inline_deinitialize_class_slot( $self->slots() );
167 around _inline_instance_get => sub {
171 return $self->associated_class()
172 ->inline_get_class_slot_value( $self->slots() );
175 around _inline_instance_set => sub {
181 return $self->associated_class()
182 ->inline_set_class_slot_value( $self->slots(), $value );
185 around _inline_instance_has => sub {
189 return $self->associated_class()
190 ->inline_is_class_slot_initialized( $self->slots() );
193 around _inline_instance_clear => sub {
197 return $self->associated_class()
198 ->inline_deinitialize_class_slot( $self->slots() );
201 around _inline_weaken_value => sub {
207 return unless $self->is_weak_ref();
210 $self->associated_class->inline_weaken_class_slot_value(
211 $self->slots(), $value
213 'if ref ' . $value . ';',
220 # ABSTRACT: A trait for class attributes
228 This role modifies the behavior of class attributes in various
229 ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
230 if it were then it couldn't be combined with other attribute
231 metaclasses, like C<MooseX::AttributeHelpers>.
233 There are no new public methods implemented by this role. All it does
234 is change the behavior of a number of existing methods.
238 See L<MooseX::ClassAttribute> for details.