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 'accessor_metaclass' => sub {
16 return 'MooseX::ClassAttribute::Meta::Method::Accessor';
19 around '_process_options' => sub {
25 confess 'A class attribute cannot be required'
26 if $options->{required};
28 return $class->$orig( $name, $options );
31 around attach_to_class => sub {
38 $self->_initialize($meta)
39 unless $self->is_lazy();
42 around 'detach_from_class' => sub {
47 $self->clear_value($meta);
54 my $metaclass = shift;
56 if ( $self->has_default() ) {
57 $self->set_value( undef, $self->default() );
59 elsif ( $self->has_builder() ) {
60 $self->set_value( undef, $self->_call_builder( $metaclass->name() ) );
64 around 'default' => sub {
68 my $default = $self->$orig();
70 if ( $self->is_default_a_coderef() ) {
71 return $default->( $self->associated_class() );
77 around '_call_builder' => sub {
82 my $builder = $self->builder();
84 return $class->$builder()
85 if $class->can( $self->builder );
87 confess( "$class does not support builder method '"
94 around 'set_value' => sub {
97 shift; # ignoring instance or class name
100 $self->associated_class()
101 ->set_class_attribute_value( $self->name() => $value );
104 around 'get_value' => sub {
108 return $self->associated_class()
109 ->get_class_attribute_value( $self->name() );
112 around 'has_value' => sub {
116 return $self->associated_class()
117 ->has_class_attribute_value( $self->name() );
120 around 'clear_value' => sub {
124 return $self->associated_class()
125 ->clear_class_attribute_value( $self->name() );
130 # ABSTRACT: A trait for class attributes
138 This role modifies the behavior of class attributes in various
139 ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
140 if it were then it couldn't be combined with other attribute
141 metaclasses, like C<MooseX::AttributeHelpers>.
143 There are no new public methods implemented by this role. All it does
144 is change the behavior of a number of existing methods.
148 See L<MooseX::ClassAttribute> for details.