1 package MooseX::ClassAttribute::Role::Meta::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() );
136 MooseX::ClassAttribute::Role::Meta::Attribute - An attribute role for classes with 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 Dave Rolsky, C<< <autarch@urth.org> >>
154 See L<MooseX::ClassAttribute> for details.
156 =head1 COPYRIGHT & LICENSE
158 Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
160 This program is free software; you can redistribute it and/or modify
161 it under the same terms as Perl itself.