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() ) {
45 $self->set_value( undef, $self->default() );
47 elsif ( $self->has_builder() ) {
48 $self->set_value( undef, $self->_call_builder( $metaclass->name() ) );
52 around default => sub {
56 my $default = $self->$orig();
58 if ( $self->is_default_a_coderef() ) {
59 return $default->( $self->associated_class() );
65 around _call_builder => sub {
70 my $builder = $self->builder();
72 return $class->$builder()
73 if $class->can( $self->builder );
75 confess( "$class does not support builder method '"
82 around set_value => sub {
85 shift; # ignoring instance or class name
88 $self->associated_class()
89 ->set_class_attribute_value( $self->name() => $value );
92 around get_value => sub {
96 return $self->associated_class()
97 ->get_class_attribute_value( $self->name() );
100 around has_value => sub {
104 return $self->associated_class()
105 ->has_class_attribute_value( $self->name() );
108 around clear_value => sub {
112 return $self->associated_class()
113 ->clear_class_attribute_value( $self->name() );
116 if ( $Moose::VERSION < 1.99 ) {
117 around inline_get => sub {
121 return $self->associated_class()
122 ->inline_get_class_slot_value( $self->slots() );
125 around inline_set => sub {
131 my $meta = $self->associated_class();
134 = $meta->inline_set_class_slot_value( $self->slots(), $value )
137 .= $meta->inline_weaken_class_slot_value( $self->slots(), $value )
139 if $self->is_weak_ref();
144 around inline_has => sub {
148 return $self->associated_class()
149 ->inline_is_class_slot_initialized( $self->slots() );
152 around inline_clear => sub {
156 return $self->associated_class()
157 ->inline_deinitialize_class_slot( $self->slots() );
161 around _inline_instance_get => sub {
165 return $self->associated_class()
166 ->inline_get_class_slot_value( $self->slots() );
169 around _inline_instance_set => sub {
175 return $self->associated_class()
176 ->inline_set_class_slot_value( $self->slots(), $value );
179 around _inline_instance_has => sub {
183 return $self->associated_class()
184 ->inline_is_class_slot_initialized( $self->slots() );
187 around _inline_instance_clear => sub {
191 return $self->associated_class()
192 ->inline_deinitialize_class_slot( $self->slots() );
195 around _inline_weaken_value => sub {
201 return unless $self->is_weak_ref();
204 $self->associated_class->inline_weaken_class_slot_value(
205 $self->slots(), $value
207 'if ref ' . $value . ';',
214 # ABSTRACT: A trait for class attributes
222 This role modifies the behavior of class attributes in various
223 ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
224 if it were then it couldn't be combined with other attribute
225 metaclasses, like C<MooseX::AttributeHelpers>.
227 There are no new public methods implemented by this role. All it does
228 is change the behavior of a number of existing methods.
232 See L<MooseX::ClassAttribute> for details.