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