Handle uninlinable instance in native trait code generation
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait.pm
1
2 package Moose::Meta::Attribute::Native::Trait;
3 use Moose::Role;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '1.14';
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 requires '_helper_type';
11
12 before '_process_options' => sub {
13     my ( $self, $name, $options ) = @_;
14
15     $self->_check_helper_type( $options, $name );
16
17     $options->{is} = $self->_default_is
18         if !exists $options->{is} && $self->can('_default_is');
19
20     $options->{default} = $self->_default_default
21         if !exists $options->{default} && $self->can('_default_default');
22 };
23
24 sub _check_helper_type {
25     my ( $self, $options, $name ) = @_;
26
27     my $type = $self->_helper_type;
28
29     $options->{isa} = $type
30         unless exists $options->{isa};
31
32     my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
33         $options->{isa} );
34
35     ( $isa->is_a_type_of($type) )
36         || confess
37         "The type constraint for $name must be a subtype of $type but it's a $isa";
38 }
39
40 before 'install_accessors' => sub { (shift)->_check_handles_values };
41
42 sub _check_handles_values {
43     my $self = shift;
44
45     my %handles = $self->_canonicalize_handles;
46
47     for my $original_method ( values %handles ) {
48         my $name = $original_method->[0];
49
50         my $accessor_class = $self->_native_accessor_class_for($name);
51
52         ( $accessor_class && $accessor_class->can('new') )
53             || confess
54             "$name is an unsupported method type - $accessor_class";
55     }
56 }
57
58 around '_canonicalize_handles' => sub {
59     shift;
60     my $self    = shift;
61     my $handles = $self->handles;
62
63     return unless $handles;
64
65     unless ( 'HASH' eq ref $handles ) {
66         $self->throw_error(
67             "The 'handles' option must be a HASH reference, not $handles");
68     }
69
70     return map {
71         my $to = $handles->{$_};
72         $to = [$to] unless ref $to;
73         $_ => $to
74     } keys %$handles;
75 };
76
77 around '_make_delegation_method' => sub {
78     my $next = shift;
79     my ( $self, $handle_name, $method_to_call ) = @_;
80
81     my ( $name, @curried_args ) = @$method_to_call;
82
83     my $accessor_class = $self->_native_accessor_class_for($name);
84
85     die "Cannot find an accessor class for $name"
86         unless $accessor_class && $accessor_class->can('new');
87
88     return $accessor_class->new(
89         name              => $handle_name,
90         package_name      => $self->associated_class->name,
91         attribute         => $self,
92         is_inline         => 1,
93         curried_arguments => \@curried_args,
94         root_types        => [ $self->_root_types ],
95     );
96 };
97
98 sub _root_types {
99     return $_[0]->_helper_type;
100 }
101
102 sub _native_accessor_class_for {
103     my ( $self, $suffix ) = @_;
104
105     return 'Moose::Meta::Method::Accessor::Native::' . $self->_native_type . '::' . $suffix;
106 }
107
108 sub _build_native_type {
109     my $self = shift;
110
111     for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
112         return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
113     }
114
115     die "Cannot calculate native type for " . ref $self;
116 }
117
118 has '_native_type' => (
119     is      => 'ro',
120     isa     => 'Str',
121     lazy    => 1,
122     builder => '_build_native_type',
123 );
124
125 no Moose::Role;
126 no Moose::Util::TypeConstraints;
127
128 1;
129
130 __END__
131
132 =head1 NAME
133
134 Moose::Meta::Attribute::Native::Trait - Base role for helpers
135
136 =head1 BUGS
137
138 See L<Moose/BUGS> for details on reporting bugs.
139
140 =head1 SEE ALSO
141
142 Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native>
143
144 =head1 AUTHORS
145
146 Yuval Kogman
147
148 Shawn M Moore
149
150 Jesse Luehrs
151
152 =head1 COPYRIGHT AND LICENSE
153
154 Copyright 2007-2009 by Infinity Interactive, Inc.
155
156 L<http://www.iinteractive.com>
157
158 This library is free software; you can redistribute it and/or modify
159 it under the same terms as Perl itself.
160
161 =cut