slightly better test failures
[gitmo/Moose.git] / lib / Moose / AttributeHelpers / Trait / Base.pm
CommitLineData
e3c07b19 1
2package Moose::AttributeHelpers::Trait::Base;
3use Moose::Role;
4use Moose::Util::TypeConstraints;
5404f169 5use Moose::AttributeHelpers::Meta::Method::Delegation;
e3c07b19 6
7our $VERSION = '0.19';
8$VERSION = eval $VERSION;
9our $AUTHORITY = 'cpan:STEVAN';
10
11requires 'helper_type';
12
e3c07b19 13# these next two are the possible methods
d09498fb 14# you can use in the 'handles' map.
e3c07b19 15
16# provide a Class or Role which we can
17# collect the method providers from
18
19# requires_attr 'method_provider'
20
21# or you can provide a HASH ref of anon subs
22# yourself. This will also collect and store
23# the methods from a method_provider as well
24has 'method_constructors' => (
25 is => 'ro',
26 isa => 'HashRef',
27 lazy => 1,
28 default => sub {
29 my $self = shift;
30 return +{} unless $self->has_method_provider;
31 # or grab them from the role/class
32 my $method_provider = $self->method_provider->meta;
33 return +{
34 map {
35 $_ => $method_provider->get_method($_)
36 } $method_provider->get_method_list
37 };
38 },
39);
40
41# extend the parents stuff to make sure
42# certain bits are now required ...
43has '+default' => (required => 1);
44has '+type_constraint' => (required => 1);
45
46## Methods called prior to instantiation
47
d09498fb 48sub process_options_for_handles {
e3c07b19 49 my ($self, $options) = @_;
50
51 if (my $type = $self->helper_type) {
52 (exists $options->{isa})
53 || confess "You must define a type with the $type metaclass";
54
55 my $isa = $options->{isa};
56
57 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
58 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
59 }
60
61 ($isa->is_a_type_of($type))
62 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
63 }
64}
65
5404f169 66sub delegation_metaclass {
67 'Moose::AttributeHelpers::Meta::Method::Delegation'
68}
69
e3c07b19 70before '_process_options' => sub {
71 my ($self, $name, $options) = @_;
d09498fb 72 $self->process_options_for_handles($options, $name);
e3c07b19 73};
74
5404f169 75around '_canonicalize_handles' => sub {
76 my $next = shift;
77 my $self = shift;
78 my $handles = $self->handles;
79 return unless $handles;
80 unless ('HASH' eq ref $handles) {
81 $self->throw_error(
82 "The 'handles' option must be a HASH reference, not $handles"
83 );
84 }
85 return map {
86 my $to = $handles->{$_};
87 $to = [ $to ] unless ref $to;
88 $_ => $to
89 } keys %$handles;
90};
91
e3c07b19 92## methods called after instantiation
93
5404f169 94before 'install_delegation' => sub { (shift)->check_handles_values };
95
d09498fb 96sub check_handles_values {
e3c07b19 97 my $self = shift;
98
99 my $method_constructors = $self->method_constructors;
100
5404f169 101 my %handles = $self->_canonicalize_handles;
e3c07b19 102
5404f169 103 for my $original_method (values %handles) {
104 my $name = $original_method->[0];
105 (exists $method_constructors->{$name})
106 || confess "$name is an unsupported method type";
e3c07b19 107 }
e3c07b19 108
5404f169 109}
e3c07b19 110
111no Moose::Role;
112no Moose::Util::TypeConstraints;
113
1141;
115
116__END__
117
118=head1 NAME
119
120Moose::AttributeHelpers::Trait::Base - base role for helpers
121
122=head1 METHODS
123
d09498fb 124=head2 check_handles_values
e3c07b19 125
d09498fb 126Confirms that handles has all valid possibilities in it.
e3c07b19 127
d09498fb 128=head2 process_options_for_handles
e3c07b19 129
130Ensures that the type constraint (C<isa>) matches the helper type.
131
132=head1 BUGS
133
134All complex software has bugs lurking in it, and this module is no
135exception. If you find a bug please either email me, or add the bug
136to cpan-RT.
137
138=head1 AUTHORS
139
140Yuval Kogman
141
142Shawn M Moore
143
144Jesse Luehrs
145
146=head1 COPYRIGHT AND LICENSE
147
148Copyright 2007-2009 by Infinity Interactive, Inc.
149
150L<http://www.iinteractive.com>
151
152This library is free software; you can redistribute it and/or modify
153it under the same terms as Perl itself.
154
155=cut