2 package MooseX::AttributeHelpers::Trait::Base;
4 use Moose::Util::TypeConstraints;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
10 requires 'helper_type';
12 # this is the method map you define ...
25 # these next two are the possible methods
26 # you can use in the 'provides' map.
28 # provide a Class or Role which we can
29 # collect the method providers from
31 # requires_attr 'method_provider'
33 # or you can provide a HASH ref of anon subs
34 # yourself. This will also collect and store
35 # the methods from a method_provider as well
36 has 'method_constructors' => (
42 return +{} unless $self->has_method_provider;
43 # or grab them from the role/class
44 my $method_provider = $self->method_provider->meta;
47 $_ => $method_provider->get_method($_)
48 } $method_provider->get_method_list
53 # extend the parents stuff to make sure
54 # certain bits are now required ...
55 has '+default' => (required => 1);
56 has '+type_constraint' => (required => 1);
58 ## Methods called prior to instantiation
60 sub process_options_for_provides {
61 my ($self, $options) = @_;
63 if (my $type = $self->helper_type) {
64 (exists $options->{isa})
65 || confess "You must define a type with the $type metaclass";
67 my $isa = $options->{isa};
69 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
70 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
73 ($isa->is_a_type_of($type))
74 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
78 before '_process_options' => sub {
79 my ($self, $name, $options) = @_;
80 $self->process_options_for_provides($options, $name);
83 ## methods called after instantiation
85 # this confirms that provides (and curries) has
86 # all valid possibilities in it
87 sub check_provides_values {
90 my $method_constructors = $self->method_constructors;
92 foreach my $key (keys %{$self->provides}) {
93 (exists $method_constructors->{$key})
94 || confess "$key is an unsupported method type";
97 foreach my $key (keys %{$self->curries}) {
98 (exists $method_constructors->{$key})
99 || confess "$key is an unsupported method type";
110 $code->($self, @args, @_)
121 $code->($self, $body, @_)
125 after 'install_accessors' => sub {
127 my $class = $attr->associated_class;
129 # grab the reader and writer methods
130 # as well, this will be useful for
131 # our method provider constructors
132 my $attr_reader = $attr->get_read_method_ref;
133 my $attr_writer = $attr->get_write_method_ref;
136 # before we install them, lets
137 # make sure they are valid
138 $attr->check_provides_values;
140 my $method_constructors = $attr->method_constructors;
142 my $class_name = $class->name;
144 while (my ($constructor, $constructed) = each %{$attr->curries}) {
146 while (my ($curried_name, $curried_arg) = each(%$constructed)) {
147 if ($class->has_method($curried_name)) {
149 "The method ($curried_name) already ".
150 "exists in class (" . $class->name . ")";
152 my $body = $method_constructors->{$constructor}->(
158 if (ref $curried_arg eq 'ARRAY') {
159 $method_code = $attr->_curry($body, @$curried_arg);
161 elsif (ref $curried_arg eq 'CODE') {
162 $method_code = $attr->_curry_sub($body, $curried_arg);
165 confess "curries parameter must be ref type HASH or CODE";
168 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
170 package_name => $class_name,
171 name => $curried_name,
174 $attr->associate_method($method);
175 $class->add_method($curried_name => $method);
179 foreach my $key (keys %{$attr->provides}) {
181 my $method_name = $attr->provides->{$key};
183 if ($class->has_method($method_name)) {
184 confess "The method ($method_name) already exists in class (" . $class->name . ")";
187 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
188 $method_constructors->{$key}->(
193 package_name => $class_name,
194 name => $method_name,
197 $attr->associate_method($method);
198 $class->add_method($method_name => $method);
202 after 'remove_accessors' => sub {
204 my $class = $attr->associated_class;
207 foreach my $key (keys %{$attr->provides}) {
208 my $method_name = $attr->provides->{$key};
209 my $method = $class->get_method($method_name);
210 $class->remove_method($method_name)
211 if blessed($method) &&
212 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
216 foreach my $key (keys %{$attr->curries}) {
217 my $method_name = $attr->curries->{$key};
218 my $method = $class->get_method($method_name);
219 $class->remove_method($method_name)
220 if blessed($method) &&
221 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
226 no Moose::Util::TypeConstraints;