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