1 package MooseX::ClassAttribute::Trait::Attribute;
6 use MooseX::ClassAttribute::Meta::Method::Accessor;
8 use namespace::autoclean;
11 # This is the worst role evar! Really, this should be a subclass,
12 # because it overrides a lot of behavior. However, as a subclass it
13 # won't cooperate with _other_ subclasses.
15 around '_process_options' => sub {
21 confess 'A class attribute cannot be required'
22 if $options->{required};
24 return $class->$orig( $name, $options );
27 around attach_to_class => sub {
34 $self->_initialize($meta)
35 unless $self->is_lazy();
38 around 'detach_from_class' => sub {
43 $self->clear_value($meta);
50 my $metaclass = shift;
52 if ( $self->has_default() ) {
53 $self->set_value( undef, $self->default() );
55 elsif ( $self->has_builder() ) {
56 $self->set_value( undef, $self->_call_builder( $metaclass->name() ) );
60 around 'default' => sub {
64 my $default = $self->$orig();
66 if ( $self->is_default_a_coderef() ) {
67 return $default->( $self->associated_class() );
73 around '_call_builder' => sub {
78 my $builder = $self->builder();
80 return $class->$builder()
81 if $class->can( $self->builder );
83 confess( "$class does not support builder method '"
90 around 'set_value' => sub {
93 shift; # ignoring instance or class name
96 $self->associated_class()
97 ->set_class_attribute_value( $self->name() => $value );
100 around 'get_value' => sub {
104 return $self->associated_class()
105 ->get_class_attribute_value( $self->name() );
108 around 'has_value' => sub {
112 return $self->associated_class()
113 ->has_class_attribute_value( $self->name() );
116 around 'clear_value' => sub {
120 return $self->associated_class()
121 ->clear_class_attribute_value( $self->name() );
124 around 'inline_get' => sub {
128 return $self->associated_class()
129 ->inline_get_class_slot_value( $self->slots() );
132 around 'inline_set' => sub {
138 my $meta = $self->associated_class();
141 = $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() );
168 # ABSTRACT: A trait for class attributes
176 This role modifies the behavior of class attributes in various
177 ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
178 if it were then it couldn't be combined with other attribute
179 metaclasses, like C<MooseX::AttributeHelpers>.
181 There are no new public methods implemented by this role. All it does
182 is change the behavior of a number of existing methods.
186 See L<MooseX::ClassAttribute> for details.