make github the primary repository
[gitmo/Moose.git] / lib / Class / MOP / Mixin / AttributeCore.pm
CommitLineData
38bf2a25 1package Class::MOP::Mixin::AttributeCore;
2
3use strict;
4use warnings;
5
38bf2a25 6use Scalar::Util 'blessed';
7
8use base 'Class::MOP::Mixin';
9
10sub has_accessor { defined $_[0]->{'accessor'} }
11sub has_reader { defined $_[0]->{'reader'} }
12sub has_writer { defined $_[0]->{'writer'} }
13sub has_predicate { defined $_[0]->{'predicate'} }
14sub has_clearer { defined $_[0]->{'clearer'} }
15sub has_builder { defined $_[0]->{'builder'} }
16sub has_init_arg { defined $_[0]->{'init_arg'} }
17sub has_default { exists $_[0]->{'default'} }
18sub has_initializer { defined $_[0]->{'initializer'} }
19sub has_insertion_order { defined $_[0]->{'insertion_order'} }
20
21sub _set_insertion_order { $_[0]->{'insertion_order'} = $_[1] }
22
23sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
24sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
25
26sub is_default_a_coderef {
27 # Uber hack because it is called from CMOP::Attribute constructor as
28 # $class->is_default_a_coderef(\%options)
29 my ($value) = ref $_[0] ? $_[0]->{'default'} : $_[1]->{'default'};
30
31 return unless ref($value);
32
33 return ref($value) eq 'CODE'
34 || ( blessed($value) && $value->isa('Class::MOP::Method') );
35}
36
37sub default {
38 my ( $self, $instance ) = @_;
39 if ( defined $instance && $self->is_default_a_coderef ) {
40 # if the default is a CODE ref, then we pass in the instance and
41 # default can return a value based on that instance. Somewhat crude,
42 # but works.
43 return $self->{'default'}->($instance);
44 }
45 $self->{'default'};
46}
47
481;
49
50# ABSTRACT: Core attributes shared by attribute metaclasses
51
52__END__
53
54=pod
55
56=head1 DESCRIPTION
57
58This class implements the core attributes (aka properties) shared by all
59attributes. See the L<Class::MOP::Attribute> documentation for API details.
60
61=cut