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 after attach_to_class => sub {
29 $self->_initialize($meta)
30 unless $self->is_lazy();
33 before detach_from_class => sub {
37 $self->clear_value($meta);
42 my $metaclass = shift;
44 if ( $self->has_default() ) {
47 $self->default( $self->associated_class() )
50 elsif ( $self->has_builder() ) {
51 $self->set_value( undef, $self->_call_builder( $metaclass->name() ) );
55 around default => sub {
59 my $default = $self->$orig();
61 if ( $self->is_default_a_coderef() && @_ ) {
62 return $default->(@_);
68 around _call_builder => sub {
73 my $builder = $self->builder();
75 return $class->$builder()
76 if $class->can( $self->builder );
78 confess( "$class does not support builder method '"
85 around set_value => sub {
88 shift; # ignoring instance or class name
91 $self->associated_class()
92 ->set_class_attribute_value( $self->name() => $value );
95 around get_value => sub {
99 return $self->associated_class()
100 ->get_class_attribute_value( $self->name() );
103 around has_value => sub {
107 return $self->associated_class()
108 ->has_class_attribute_value( $self->name() );
111 around clear_value => sub {
115 return $self->associated_class()
116 ->clear_class_attribute_value( $self->name() );
119 if ( $Moose::VERSION < 1.99 ) {
120 around inline_get => sub {
124 return $self->associated_class()
125 ->_inline_get_class_slot_value( $self->slots() );
128 around inline_set => sub {
134 my $meta = $self->associated_class();
137 = $meta->_inline_set_class_slot_value( $self->slots(), $value )
139 $code .= $meta->_inline_weaken_class_slot_value(
144 if $self->is_weak_ref();
149 around inline_has => sub {
153 return $self->associated_class()
154 ->_inline_is_class_slot_initialized( $self->slots() );
157 around inline_clear => sub {
161 return $self->associated_class()
162 ->_inline_deinitialize_class_slot( $self->slots() );
166 around _inline_instance_get => sub {
170 return $self->associated_class()
171 ->_inline_get_class_slot_value( $self->slots() );
174 around _inline_instance_set => sub {
180 return $self->associated_class()
181 ->_inline_set_class_slot_value( $self->slots(), $value );
184 around _inline_instance_has => sub {
188 return $self->associated_class()
189 ->_inline_is_class_slot_initialized( $self->slots() );
192 around _inline_instance_clear => sub {
196 return $self->associated_class()
197 ->_inline_deinitialize_class_slot( $self->slots() );
200 around _inline_weaken_value => sub {
206 return unless $self->is_weak_ref();
209 $self->associated_class->_inline_weaken_class_slot_value(
210 $self->slots(), $value
212 'if ref ' . $value . ';',
219 # ABSTRACT: A trait for class attributes
227 This role modifies the behavior of class attributes in various
228 ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
229 if it were then it couldn't be combined with other attribute
230 metaclasses, like C<MooseX::AttributeHelpers>.
232 There are no new public methods implemented by this role. All it does
233 is change the behavior of a number of existing methods.
237 See L<MooseX::ClassAttribute> for details.