-
package MooseX::AttributeHelpers::Base;
use Moose;
use Moose::Util::TypeConstraints;
+use MooseX::AttributeHelpers::MethodProvider;
use MooseX::AttributeHelpers::Meta::Method::Provided;
our $VERSION = '0.04';
default => sub {{}}
);
-# 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);
## Methods called prior to instantiation
-# (overridden by Sugar or plain subclasses)
-sub helper_type {()}
+# For overriding
sub default_options {}
sub auto_provide {0}
+# Do not override both of these things. You will be eaten.
+sub method_provider {}
+sub method_constructors {
+ get_provider_methods($_[0]->method_provider, ':all')
+}
+
sub process_options_for_provides {
my ($self, $options) = @_;
}
}
- if (my $type = $self->helper_type) {
- $options->{isa} = $type unless exists $options->{isa};
-
- my $isa = $options->{isa};
-
- unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
- $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
- }
-
- ($isa->is_a_type_of($type))
- || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
+ return unless $self->method_provider;
+ my $type = get_provider_type($self->method_provider);
+ $options->{isa} = $type unless exists $options->{isa};
+ my $isa = $options->{isa};
+
+ unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
+ $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
+ $isa
+ );
}
+
+ confess "The type constraint for a $type ($options->{isa}) "
+ . "must be a subtype of $type"
+ unless $isa->is_a_type_of($type);
}
before '_process_options' => sub {
}
};
+sub sugar {
+ my ($class, %info) = @_;
+ my $meta = $class->meta;
+
+ $meta->add_method('default_options', sub {$info{default_options}});
+ $meta->add_method('auto_provide', sub {$info{auto_provide} || 0});
+
+ my $provider = $info{method_provider};
+ my $constructors = $info{method_constructors};
+
+ confess "Supply either method_provider or method_constructors"
+ if ($provider && $constructors) || !($provider || $constructors);
+
+ if(my $provider = $info{method_provider}) {
+ $meta->add_method('method_provider' => sub { $provider });
+ }
+ elsif (my $cons = $info{method_constructors}) {
+ $meta->add_method('method_constructors' => sub { $cons });
+ }
+
+ if (my $s = $info{shortcut}) {
+ $meta->create("Moose::Meta::Attribute::Custom::$s",
+ methods => {register_implementation => sub { $class }},
+ );
+ }
+}
+
no Moose;
no Moose::Util::TypeConstraints;
MooseX::AttributeHelpers::Base - Base class for attribute helpers
+SYNOPSIS
+
+ package MooseX::AttributeHelpers::Counter;
+ use Moose;
+ use MooseX::AttributeHelpers::MethodProvider::Counter;
+
+ extends 'MooseX::AttributeHelpers::Base';
+
+ __PACKAGE__->sugar(
+ default_options => {
+ is => 'ro',
+ default => 0,
+ },
+
+ auto_provide => 1,
+ method_provider => 'Counter',
+ shortcut => 'Counter',
+ );
+
+ no Moose;
+
+ 1;
+
=head1 DESCRIPTION
This class is what you inherit from when you want to make a new
-AttributeHelper. Unless you are doing something quite fancy, your needs
-should be met by L<MooseX::AttributeHelpers::Sugar>, which has a nice, terse
-syntax and some convenience, but you should still subclass this class.
+AttributeHelper metaclass. Most of the work is done for you by the class
+method I<sugar> if you're doing something basic.
=head1 ATTRIBUTES
This is the map of metaclass methods to methods that will be installed in your
class, e.g. add => 'add_to_number'.
-=item B<method_provider>
-
-The name of a class or role to be used as source material for the above map.
-In the above example, the method provider's "add" method would be used to
-construct a method to install into the attribute holder's class.
-
-=item B<method_constructors>
-
-You can optionally supply a hashref of names to subs instead of a class to be
-used as method constructors, but by default this is pulled from
-method_provider.
-
=back
=head1 EXTENDED ATTRIBUTES
=over 4
-=item B<meta>
+=item B<method_provider>
+
+The name of a method provider. Usually one L<use|perlfunc/use>s a package
+that defines a method provider in the registry first, but you can just as well
+define one in your own code. See L<MooseX::AttributeHelpers::MethodProvider>
+for details.
+
+=item B<method_constructors>
+
+You can optionally supply a hashref of names to subs instead of a class to be
+used as method constructors. In that case, your methods won't be available
+for use by L<Composite|MooseX::AttributeHelpers::Composite>.
=item B<auto_provide>
not overridden by the implementing attribute. This is intended to be
overridden in subclasses.
-=item B<helper_type>
+=item B<sugar>
-This forces all attributes using this metaclass to be a subtype of
-helper_type. This is intended to be overridden in subclasses.
+A convenience method for subclassing declaratively. See L<"SYNOPSIS"> for an
+example. The shortcut option creates a package under
+Moose::Meta::Attribute::Custom to make it easier for users to find your
+metaclass, but you can do this manually if you desire.
=item B<check_provides_values>
-=item B<has_method_provider>
-
=item B<install_accessors>
=item B<remove_accessors>
=item B<process_options_for_provides>
+These are hooks you can use to change the behavior of the metaclass; read the
+source for inspiration.
+
=back
=head1 BUGS