extends 'Moose::Meta::Attribute';
-has 'method_constructors' => (
- is => 'ro',
- isa => 'HashRef',
- default => sub { {} }
-);
-
+# this is the method map you define ...
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
# certain bits are now required ...
has '+$!default' => (required => 1);
# 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";
our $VERSION = '0.01';
our $AUTHORITY = 'cpan:STEVAN';
-extends 'MooseX::AttributeHelpers::Base';
+use MooseX::AttributeHelpers::MethodProvider::Counter;
-sub helper_type { 'Num' }
+extends 'MooseX::AttributeHelpers::Base';
-has '+method_constructors' => (
- default => sub {
- return +{
- inc => sub {
- my $attr = shift;
- return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) };
- },
- dec => sub {
- my $attr = shift;
- return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) };
- },
- }
- }
+has '+method_provider' => (
+ default => 'MooseX::AttributeHelpers::MethodProvider::Counter'
);
+
+sub helper_type { 'Num' }
no Moose;
--- /dev/null
+
+package MooseX::AttributeHelpers::MethodProvider::Counter;
+use Moose::Role;
+
+sub inc {
+ my $attr = shift;
+ return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) };
+}
+
+sub dec {
+ my $attr = shift;
+ return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) };
+}
+
+1;
+
+__END__
+
+=pod
+
+=cut
\ No newline at end of file