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