Commit | Line | Data |
d26633fc |
1 | |
2 | package MooseX::AttributeHelpers::Base; |
3 | use Moose; |
8ba40fb0 |
4 | use Moose::Util::TypeConstraints; |
d26633fc |
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 | |
8881a8d3 |
23 | # extend the parents stuff to make sure |
24 | # certain bits are now required ... |
d26633fc |
25 | has '+$!default' => (required => 1); |
26 | has '+type_constraint' => (required => 1); |
27 | |
8ba40fb0 |
28 | ## Methods called prior to instantiation |
29 | |
30 | sub helper_type { () } |
d26633fc |
31 | |
8ba40fb0 |
32 | sub process_options_for_provides { |
d26633fc |
33 | my ($self, $options) = @_; |
21c7045b |
34 | |
8ba40fb0 |
35 | if (my $type = $self->helper_type) { |
36 | (exists $options->{isa}) |
37 | || confess "You must define a type with the $type metaclass"; |
38 | |
8c651099 |
39 | my $isa = $options->{isa}; |
8ba40fb0 |
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 | } |
d26633fc |
48 | } |
49 | |
50 | before '_process_options' => sub { |
88aaf2bd |
51 | my ($self, $name, $options) = @_; |
52 | if (exists $options->{provides}) { |
8ba40fb0 |
53 | $self->process_options_for_provides($options); |
d26633fc |
54 | } |
55 | }; |
56 | |
8ba40fb0 |
57 | ## methods called after instantiation |
58 | |
59 | # this confirms that provides has |
60 | # all valid possibilities in it |
61 | sub check_provides_values { |
62 | my $self = shift; |
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"; |
67 | } |
68 | } |
69 | |
d26633fc |
70 | after 'install_accessors' => sub { |
71 | my $attr = shift; |
72 | my $class = $attr->associated_class; |
73 | |
88aaf2bd |
74 | # before we install them, lets |
75 | # make sure they are valid |
8ba40fb0 |
76 | $attr->check_provides_values; |
88aaf2bd |
77 | |
d26633fc |
78 | my $method_constructors = $attr->method_constructors; |
79 | |
80 | foreach my $key (keys %{$attr->provides}) { |
8f7951c9 |
81 | |
82 | my $method_name = $attr->provides->{$key}; |
83 | my $method_body = $method_constructors->{$key}->($attr); |
84 | |
85 | if ($class->has_method($method_name)) { |
86 | confess "The method ($method_name) already exists in class (" . $class->name . ")"; |
87 | } |
88 | |
89 | $class->add_method($method_name => |
90 | MooseX::AttributeHelpers::Meta::Method::Provided->wrap( |
91 | $method_body, |
92 | ) |
d26633fc |
93 | ); |
94 | } |
95 | }; |
96 | |
97 | no Moose; |
8ba40fb0 |
98 | no Moose::Util::TypeConstraints; |
d26633fc |
99 | |
100 | 1; |
101 | |
102 | __END__ |
103 | |
104 | =pod |
105 | |
106 | =head1 NAME |
107 | |
108 | MooseX::AttributeHelpers::Base |
109 | |
110 | =head1 SYNOPSIS |
111 | |
112 | =head1 DESCRIPTION |
113 | |
114 | =head1 METHODS |
115 | |
116 | =head1 BUGS |
117 | |
118 | All complex software has bugs lurking in it, and this module is no |
119 | exception. If you find a bug please either email me, or add the bug |
120 | to cpan-RT. |
121 | |
122 | =head1 AUTHOR |
123 | |
124 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
125 | |
126 | =head1 COPYRIGHT AND LICENSE |
127 | |
128 | Copyright 2007 by Infinity Interactive, Inc. |
129 | |
130 | L<http://www.iinteractive.com> |
131 | |
132 | This library is free software; you can redistribute it and/or modify |
133 | it under the same terms as Perl itself. |
134 | |
8a9cea9b |
135 | =cut |