some POD advances
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Base.pm
CommitLineData
d26633fc 1
2package MooseX::AttributeHelpers::Base;
3use Moose;
8ba40fb0 4use Moose::Util::TypeConstraints;
d26633fc 5
6our $VERSION = '0.01';
7our $AUTHORITY = 'cpan:STEVAN';
8
9extends 'Moose::Meta::Attribute';
10
8e3fab6d 11# this is the method map you define ...
d26633fc 12has 'provides' => (
9810162d 13 is => 'ro',
14 isa => 'HashRef',
15 default => sub {{}}
d26633fc 16);
17
8e3fab6d 18
19# these next two are the possible methods
20# you can use in the 'provides' map.
21
22# provide a Class or Role which we can
23# collect the method providers from
24has 'method_provider' => (
25 is => 'ro',
26 isa => 'ClassName',
27 predicate => 'has_method_provider',
28);
29
30# or you can provide a HASH ref of anon subs
31# yourself. This will also collect and store
32# the methods from a method_provider as well
33has 'method_constructors' => (
34 is => 'ro',
35 isa => 'HashRef',
36 lazy => 1,
37 default => sub {
38 my $self = shift;
39 return +{} unless $self->has_method_provider;
40 # or grab them from the role/class
41 my $method_provider = $self->method_provider->meta;
42 return +{
43 map {
44 $_ => $method_provider->get_method($_)
45 } $method_provider->get_method_list
46 };
e295d072 47 },
8e3fab6d 48);
49
8881a8d3 50# extend the parents stuff to make sure
51# certain bits are now required ...
d26633fc 52has '+$!default' => (required => 1);
53has '+type_constraint' => (required => 1);
54
8ba40fb0 55## Methods called prior to instantiation
56
57sub helper_type { () }
d26633fc 58
8ba40fb0 59sub process_options_for_provides {
d26633fc 60 my ($self, $options) = @_;
21c7045b 61
8ba40fb0 62 if (my $type = $self->helper_type) {
63 (exists $options->{isa})
64 || confess "You must define a type with the $type metaclass";
65
8c651099 66 my $isa = $options->{isa};
8ba40fb0 67
68 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
69 $isa = find_type_constraint($isa);
70 }
71
72 ($isa->is_a_type_of($type))
73 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
74 }
d26633fc 75}
76
77before '_process_options' => sub {
88aaf2bd 78 my ($self, $name, $options) = @_;
9810162d 79 if (exists $options->{provides} ||
80 exists $options->{isa} && $options->{isa} =~ /^.*?\[.*?\]$/) {
8ba40fb0 81 $self->process_options_for_provides($options);
d26633fc 82 }
83};
84
8ba40fb0 85## methods called after instantiation
86
87# this confirms that provides has
88# all valid possibilities in it
89sub check_provides_values {
90 my $self = shift;
8e3fab6d 91
8ba40fb0 92 my $method_constructors = $self->method_constructors;
8e3fab6d 93
8ba40fb0 94 foreach my $key (keys %{$self->provides}) {
95 (exists $method_constructors->{$key})
96 || confess "$key is an unsupported method type";
97 }
98}
99
d26633fc 100after 'install_accessors' => sub {
101 my $attr = shift;
102 my $class = $attr->associated_class;
103
88aaf2bd 104 # before we install them, lets
105 # make sure they are valid
8ba40fb0 106 $attr->check_provides_values;
88aaf2bd 107
d26633fc 108 my $method_constructors = $attr->method_constructors;
109
110 foreach my $key (keys %{$attr->provides}) {
8f7951c9 111
112 my $method_name = $attr->provides->{$key};
113 my $method_body = $method_constructors->{$key}->($attr);
114
115 if ($class->has_method($method_name)) {
116 confess "The method ($method_name) already exists in class (" . $class->name . ")";
117 }
118
119 $class->add_method($method_name =>
120 MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
121 $method_body,
122 )
d26633fc 123 );
124 }
125};
126
127no Moose;
8ba40fb0 128no Moose::Util::TypeConstraints;
d26633fc 129
1301;
131
132__END__
133
134=pod
135
136=head1 NAME
137
138MooseX::AttributeHelpers::Base
139
140=head1 SYNOPSIS
141
142=head1 DESCRIPTION
143
e295d072 144Base class for attribute helpers.
145
146=head1 ATTRIBUTES
147
148=head2 provides
149
150=head2 method_provider
151
152=head2 method_constructors
153
154=head1 EXTENDED ATTRIBUTES
155
156=head2 $!default
157
158C<$!default> is now required.
159
160=head2 type_constraint
161
162C<type_constraint> is now required.
163
d26633fc 164=head1 METHODS
165
e295d072 166=head2 helper_type
167
d26633fc 168=head1 BUGS
169
170All complex software has bugs lurking in it, and this module is no
171exception. If you find a bug please either email me, or add the bug
172to cpan-RT.
173
174=head1 AUTHOR
175
176Stevan Little E<lt>stevan@iinteractive.comE<gt>
177
178=head1 COPYRIGHT AND LICENSE
179
180Copyright 2007 by Infinity Interactive, Inc.
181
182L<http://www.iinteractive.com>
183
184This library is free software; you can redistribute it and/or modify
185it under the same terms as Perl itself.
186
8a9cea9b 187=cut