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() );
83 override '_inline_instance_get' => sub {
86 return $self->associated_class()
87 ->inline_get_class_slot_value( $self->slots() );
91 override '_inline_weaken_value' => sub {
93 my ($instance, $value) = @_;
94 return unless $self->is_weak_ref;
96 my $mi = $self->associated_class->get_meta_instance;
98 $self->associated_class->inline_weaken_class_slot_value( $self->slots(), $value ),
99 'if ref ' . $value . ';',
103 override '_inline_instance_set' => sub {
108 my $meta = $self->associated_class();
111 = $meta->inline_set_class_slot_value( $self->slots(), $value ) . ";";
116 override '_inline_instance_has' => sub {
119 return $self->associated_class()
120 ->inline_is_class_slot_initialized( $self->slots() );
123 override '_inline_clear_value' => sub {
126 return $self->associated_class()
127 ->inline_deinitialize_class_slot( $self->slots() );
132 # ABSTRACT: A trait for class attributes
140 This role modifies the behavior of class attributes in various
141 ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
142 if it were then it couldn't be combined with other attribute
143 metaclasses, like C<MooseX::AttributeHelpers>.
145 There are no new public methods implemented by this role. All it does
146 is change the behavior of a number of existing methods.
150 See L<MooseX::ClassAttribute> for details.