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) = @_;
34 if (my $type = $self->helper_type) {
35 (exists $options->{isa})
36 || confess "You must define a type with the $type metaclass";
38 my $isa = $options->{isa};
40 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
41 $isa = find_type_constraint($isa);
44 ($isa->is_a_type_of($type))
45 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
48 # this can be augmented by subclasses ..
52 before '_process_options' => sub {
53 my ($self, $name, $options) = @_;
54 if (exists $options->{provides}) {
55 $self->process_options_for_provides($options);
59 ## methods called after instantiation
61 # this confirms that provides has
62 # all valid possibilities in it
63 sub check_provides_values {
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";
72 after 'install_accessors' => sub {
74 my $class = $attr->associated_class;
76 # before we install them, lets
77 # make sure they are valid
78 $attr->check_provides_values;
80 my $method_constructors = $attr->method_constructors;
82 foreach my $key (keys %{$attr->provides}) {
84 $attr->provides->{$key},
85 $method_constructors->{$key}->($attr)
91 no Moose::Util::TypeConstraints;
101 MooseX::AttributeHelpers::Base
111 All complex software has bugs lurking in it, and this module is no
112 exception. If you find a bug please either email me, or add the bug
117 Stevan Little E<lt>stevan@iinteractive.comE<gt>
119 =head1 COPYRIGHT AND LICENSE
121 Copyright 2007 by Infinity Interactive, Inc.
123 L<http://www.iinteractive.com>
125 This library is free software; you can redistribute it and/or modify
126 it under the same terms as Perl itself.