1 package MooseX::ClassAttribute::Trait::Attribute;
8 use MooseX::ClassAttribute::Meta::Method::Accessor;
10 use namespace::autoclean;
13 # This is the worst role evar! Really, this should be a subclass,
14 # because it overrides a lot of behavior. However, as a subclass it
15 # won't cooperate with _other_ subclasses.
17 around 'accessor_metaclass' => sub {
18 return 'MooseX::ClassAttribute::Meta::Method::Accessor';
21 around '_process_options' => sub {
27 confess 'A class attribute cannot be required'
28 if $options->{required};
30 return $class->$orig( $name, $options );
33 around attach_to_class => sub {
40 $self->_initialize($meta)
41 unless $self->is_lazy();
44 around 'detach_from_class' => sub {
49 $self->clear_value($meta);
56 my $metaclass = shift;
58 if ( $self->has_default() ) {
59 $self->set_value( undef, $self->default() );
61 elsif ( $self->has_builder() ) {
62 $self->set_value( undef, $self->_call_builder( $metaclass->name() ) );
66 around 'default' => sub {
70 my $default = $self->$orig();
72 if ( $self->is_default_a_coderef() ) {
73 return $default->( $self->associated_class() );
79 around '_call_builder' => sub {
84 my $builder = $self->builder();
86 return $class->$builder()
87 if $class->can( $self->builder );
89 confess( "$class does not support builder method '"
96 around 'set_value' => sub {
99 shift; # ignoring instance or class name
102 $self->associated_class()
103 ->set_class_attribute_value( $self->name() => $value );
106 around 'get_value' => sub {
110 return $self->associated_class()
111 ->get_class_attribute_value( $self->name() );
114 around 'has_value' => sub {
118 return $self->associated_class()
119 ->has_class_attribute_value( $self->name() );
122 around 'clear_value' => sub {
126 return $self->associated_class()
127 ->clear_class_attribute_value( $self->name() );
138 MooseX::ClassAttribute::Trait::Attribute - A trait for class attributes
142 This role modifies the behavior of class attributes in various
143 ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
144 if it were then it couldn't be combined with other attribute
145 metaclasses, like C<MooseX::AttributeHelpers>.
147 There are no new public methods implemented by this role. All it does
148 is change the behavior of a number of existing methods.
152 Dave Rolsky, C<< <autarch@urth.org> >>
156 See L<MooseX::ClassAttribute> for details.
158 =head1 COPYRIGHT & LICENSE
160 Copyright 2007-2010 Dave Rolsky, All Rights Reserved.
162 This program is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.