Version 1.12
[gitmo/Class-MOP.git] / lib / Class / MOP / Mixin / AttributeCore.pm
CommitLineData
9b871d79 1package Class::MOP::Mixin::AttributeCore;
2
3use strict;
4use warnings;
5
bd2550f8 6our $VERSION = '1.12';
9b871d79 7$VERSION = eval $VERSION;
8our $AUTHORITY = 'cpan:STEVAN';
9
10use Scalar::Util 'blessed';
11
12use base 'Class::MOP::Mixin';
13
14sub has_accessor { defined $_[0]->{'accessor'} }
15sub has_reader { defined $_[0]->{'reader'} }
16sub has_writer { defined $_[0]->{'writer'} }
17sub has_predicate { defined $_[0]->{'predicate'} }
18sub has_clearer { defined $_[0]->{'clearer'} }
19sub has_builder { defined $_[0]->{'builder'} }
20sub has_init_arg { defined $_[0]->{'init_arg'} }
8343d501 21sub has_default { exists $_[0]->{'default'} }
9b871d79 22sub has_initializer { defined $_[0]->{'initializer'} }
23sub has_insertion_order { defined $_[0]->{'insertion_order'} }
24
25sub accessor { $_[0]->{'accessor'} }
26sub reader { $_[0]->{'reader'} }
27sub writer { $_[0]->{'writer'} }
28sub predicate { $_[0]->{'predicate'} }
29sub clearer { $_[0]->{'clearer'} }
30sub builder { $_[0]->{'builder'} }
31sub init_arg { $_[0]->{'init_arg'} }
32sub initializer { $_[0]->{'initializer'} }
33sub definition_context { $_[0]->{'definition_context'} }
34sub insertion_order { $_[0]->{'insertion_order'} }
35sub _set_insertion_order { $_[0]->{'insertion_order'} = $_[1] }
36
37sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
38sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
39
40sub is_default_a_coderef {
41 # Uber hack because it is called from CMOP::Attribute constructor as
42 # $class->is_default_a_coderef(\%options)
43 my ($value) = ref $_[0] ? $_[0]->{'default'} : $_[1]->{'default'};
44
45 return unless ref($value);
46
47 return ref($value) eq 'CODE'
48 || ( blessed($value) && $value->isa('Class::MOP::Method') );
49}
50
51sub default {
52 my ( $self, $instance ) = @_;
53 if ( defined $instance && $self->is_default_a_coderef ) {
54 # if the default is a CODE ref, then we pass in the instance and
55 # default can return a value based on that instance. Somewhat crude,
56 # but works.
57 return $self->{'default'}->($instance);
58 }
59 $self->{'default'};
60}
61
621;
63
64__END__
65
66=pod
67
68=head1 NAME
69
70Class::MOP::Mixin::AttributeCore - Core attributes shared by attribute metaclasses
71
72=head1 DESCRIPTION
73
74This class implements the core attributes (aka properties) shared by all
75attributes. See the L<Class::MOP::Attribute> documentation for API details.
76
77=head1 AUTHORS
78
79Dave Rolsky E<lt>autarch@urth.orgE<gt>
80
81=head1 COPYRIGHT AND LICENSE
82
3e2c8600 83Copyright 2006-2010 by Infinity Interactive, Inc.
9b871d79 84
85L<http://www.iinteractive.com>
86
87This library is free software; you can redistribute it and/or modify
88it under the same terms as Perl itself.
89
90=cut