extends 'Moose::Meta::Attribute';
-has 'method_constructors' => (
+# this is the method map you define ...
+has 'provides' => (
is => 'ro',
isa => 'HashRef',
- default => sub { {} }
+ default => sub {{}}
);
-has 'provides' => (
- is => 'ro',
- isa => 'HashRef',
- required => 1,
+
+# these next two are the possible methods
+# you can use in the 'provides' map.
+
+# provide a Class or Role which we can
+# collect the method providers from
+has 'method_provider' => (
+ is => 'ro',
+ isa => 'ClassName',
+ predicate => 'has_method_provider',
+);
+
+# or you can provide a HASH ref of anon subs
+# yourself. This will also collect and store
+# the methods from a method_provider as well
+has 'method_constructors' => (
+ is => 'ro',
+ isa => 'HashRef',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ return +{} unless $self->has_method_provider;
+ # or grab them from the role/class
+ my $method_provider = $self->method_provider->meta;
+ return +{
+ map {
+ $_ => $method_provider->get_method($_)
+ } $method_provider->get_method_list
+ };
+ }
);
# extend the parents stuff to make sure
before '_process_options' => sub {
my ($self, $name, $options) = @_;
- if (exists $options->{provides}) {
+ if (exists $options->{provides} ||
+ exists $options->{isa} && $options->{isa} =~ /^.*?\[.*?\]$/) {
$self->process_options_for_provides($options);
}
};
# all valid possibilities in it
sub check_provides_values {
my $self = shift;
+
my $method_constructors = $self->method_constructors;
+
foreach my $key (keys %{$self->provides}) {
(exists $method_constructors->{$key})
|| confess "$key is an unsupported method type";
my $method_constructors = $attr->method_constructors;
foreach my $key (keys %{$attr->provides}) {
- $class->add_method(
- $attr->provides->{$key},
- $method_constructors->{$key}->($attr)
+
+ my $method_name = $attr->provides->{$key};
+ my $method_body = $method_constructors->{$key}->($attr);
+
+ if ($class->has_method($method_name)) {
+ confess "The method ($method_name) already exists in class (" . $class->name . ")";
+ }
+
+ $class->add_method($method_name =>
+ MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
+ $method_body,
+ )
);
}
};