foo
[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
11has 'method_constructors' => (
12 is => 'ro',
13 isa => 'HashRef',
14 default => sub { {} }
15);
16
17has 'provides' => (
18 is => 'ro',
19 isa => 'HashRef',
20 required => 1,
21);
22
8881a8d3 23# extend the parents stuff to make sure
24# certain bits are now required ...
d26633fc 25has '+$!default' => (required => 1);
26has '+type_constraint' => (required => 1);
27
8ba40fb0 28## Methods called prior to instantiation
29
30sub helper_type { () }
d26633fc 31
8ba40fb0 32sub process_options_for_provides {
d26633fc 33 my ($self, $options) = @_;
8ba40fb0 34 if (my $type = $self->helper_type) {
35 (exists $options->{isa})
36 || confess "You must define a type with the $type metaclass";
37
38 my $isa = $options->{isa};
39
40 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
41 $isa = find_type_constraint($isa);
42 }
43
44 ($isa->is_a_type_of($type))
45 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
46 }
47
48 # this can be augmented by subclasses ..
49 inner();
d26633fc 50}
51
52before '_process_options' => sub {
88aaf2bd 53 my ($self, $name, $options) = @_;
54 if (exists $options->{provides}) {
8ba40fb0 55 $self->process_options_for_provides($options);
d26633fc 56 }
57};
58
8ba40fb0 59## methods called after instantiation
60
61# this confirms that provides has
62# all valid possibilities in it
63sub check_provides_values {
64 my $self = shift;
65 my $method_constructors = $self->method_constructors;
66 foreach my $key (keys %{$self->provides}) {
67 (exists $method_constructors->{$key})
68 || confess "$key is an unsupported method type";
69 }
70}
71
d26633fc 72after 'install_accessors' => sub {
73 my $attr = shift;
74 my $class = $attr->associated_class;
75
88aaf2bd 76 # before we install them, lets
77 # make sure they are valid
8ba40fb0 78 $attr->check_provides_values;
88aaf2bd 79
d26633fc 80 my $method_constructors = $attr->method_constructors;
81
82 foreach my $key (keys %{$attr->provides}) {
83 $class->add_method(
84 $attr->provides->{$key},
85 $method_constructors->{$key}->($attr)
86 );
87 }
88};
89
90no Moose;
8ba40fb0 91no Moose::Util::TypeConstraints;
d26633fc 92
931;
94
95__END__
96
97=pod
98
99=head1 NAME
100
101MooseX::AttributeHelpers::Base
102
103=head1 SYNOPSIS
104
105=head1 DESCRIPTION
106
107=head1 METHODS
108
109=head1 BUGS
110
111All complex software has bugs lurking in it, and this module is no
112exception. If you find a bug please either email me, or add the bug
113to cpan-RT.
114
115=head1 AUTHOR
116
117Stevan Little E<lt>stevan@iinteractive.comE<gt>
118
119=head1 COPYRIGHT AND LICENSE
120
121Copyright 2007 by Infinity Interactive, Inc.
122
123L<http://www.iinteractive.com>
124
125This library is free software; you can redistribute it and/or modify
126it under the same terms as Perl itself.
127
8a9cea9b 128=cut