2 package MooseX::AttributeHelpers::Base;
4 use Moose::Util::TypeConstraints;
7 our $AUTHORITY = 'cpan:STEVAN';
9 extends 'Moose::Meta::Attribute';
11 has 'method_constructors' => (
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);
28 ## Methods called prior to instantiation
30 sub helper_type { () }
32 sub process_options_for_provides {
33 my ($self, $options) = @_;
35 if (my $type = $self->helper_type) {
36 (exists $options->{isa})
37 || confess "You must define a type with the $type metaclass";
39 my $isa = $options->{isa};
41 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
42 $isa = find_type_constraint($isa);
45 ($isa->is_a_type_of($type))
46 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
50 before '_process_options' => sub {
51 my ($self, $name, $options) = @_;
52 if (exists $options->{provides}) {
53 $self->process_options_for_provides($options);
57 ## methods called after instantiation
59 # this confirms that provides has
60 # all valid possibilities in it
61 sub check_provides_values {
63 my $method_constructors = $self->method_constructors;
64 foreach my $key (keys %{$self->provides}) {
65 (exists $method_constructors->{$key})
66 || confess "$key is an unsupported method type";
70 after 'install_accessors' => sub {
72 my $class = $attr->associated_class;
74 # before we install them, lets
75 # make sure they are valid
76 $attr->check_provides_values;
78 my $method_constructors = $attr->method_constructors;
80 foreach my $key (keys %{$attr->provides}) {
82 $attr->provides->{$key},
83 $method_constructors->{$key}->($attr)
89 no Moose::Util::TypeConstraints;
99 MooseX::AttributeHelpers::Base
109 All complex software has bugs lurking in it, and this module is no
110 exception. If you find a bug please either email me, or add the bug
115 Stevan Little E<lt>stevan@iinteractive.comE<gt>
117 =head1 COPYRIGHT AND LICENSE
119 Copyright 2007 by Infinity Interactive, Inc.
121 L<http://www.iinteractive.com>
123 This library is free software; you can redistribute it and/or modify
124 it under the same terms as Perl itself.