package MooseX::AttributeHelpers::Base;
use Moose;
use Moose::Util::TypeConstraints;
+use MooseX::AttributeHelpers::Meta::Method::Provided;
our $VERSION = '0.04';
our $AUTHORITY = 'cpan:STEVAN';
default => sub {{}}
);
-
# these next two are the possible methods
# you can use in the 'provides' map.
## Methods called prior to instantiation
-sub helper_type { () }
+# (overridden by Sugar or plain subclasses)
+sub helper_type {()}
+sub default_options {}
+sub auto_provide {0}
sub process_options_for_provides {
my ($self, $options) = @_;
+ if (my $defaults = $self->default_options) {
+ foreach my $key (keys %$defaults) {
+ $options->{$key} = $defaults->{$key}
+ unless exists $options->{$key};
+ }
+ }
+
if (my $type = $self->helper_type) {
- (exists $options->{isa})
- || confess "You must define a type with the $type metaclass";
+ $options->{isa} = $type unless exists $options->{isa};
my $isa = $options->{isa};
(exists $method_constructors->{$key})
|| confess "$key is an unsupported method type";
}
+
+ my $provides = $self->provides;
+ if (keys %$provides == 0 and $self->auto_provide) {
+ my $attr_name = $self->name;
+
+ foreach my $method (keys %$method_constructors) {
+ $provides->{$method} = "${method}_${attr_name}";
+ }
+ }
}
after 'install_accessors' => sub {
=head1 DESCRIPTION
-Documentation to come.
+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.
=head1 ATTRIBUTES
=item B<provides>
+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
=item B<meta>
+=item B<auto_provide>
+
+If this method returns a true value, all available method constructors will be
+provided in the format $method_$attribute_name e.g. inc_counter. This is
+intended to be overridden in subclasses.
+
+=item B<default_options>
+
+Returns a Maybe[Hashref] of attribution specifications to fill in if they are
+not overridden by the implementing attribute. This is intended to be
+overridden in subclasses.
+
=item B<helper_type>
-=item B<check_provides_values>
+This forces all attributes using this metaclass to be a subtype of
+helper_type. This is intended to be overridden in subclasses.
-=item B<has_default>
+=item B<check_provides_values>
=item B<has_method_provider>
-=item B<has_type_constraint>
-
=item B<install_accessors>
=item B<remove_accessors>
package MooseX::AttributeHelpers::Counter;
use Moose;
+use MooseX::AttributeHelpers::Sugar;
+
+extends 'MooseX::AttributeHelpers::Base';
our $VERSION = '0.03';
our $AUTHORITY = 'cpan:STEVAN';
-use MooseX::AttributeHelpers::MethodProvider::Counter;
-
-extends 'MooseX::AttributeHelpers::Base';
+define_attribute_helper (
+ default_options => {
+ is => 'ro',
+ default => 0,
+ },
-has '+method_provider' => (
- default => 'MooseX::AttributeHelpers::MethodProvider::Counter'
+ helper_type => 'Num',
+ method_provider => 'MooseX::AttributeHelpers::MethodProvider::Counter',
+ auto_provide => 1,
+ shortcut => 'Counter',
);
-sub helper_type { 'Num' }
-
-before 'process_options_for_provides' => sub {
- my ($self, $options, $name) = @_;
-
- # Set some default attribute options here unless already defined
- if ((my $type = $self->helper_type) && !exists $options->{isa}){
- $options->{isa} = $type;
- }
-
- $options->{is} = 'ro' unless exists $options->{is};
- $options->{default} = 0 unless exists $options->{default};
-};
-
-after 'check_provides_values' => sub {
- my $self = shift;
- my $provides = $self->provides;
-
- unless (scalar keys %$provides) {
- my $method_constructors = $self->method_constructors;
- my $attr_name = $self->name;
-
- foreach my $method (keys %$method_constructors) {
- $provides->{$method} = ($method . '_' . $attr_name);
- }
- }
-};
-
no Moose;
-
-# register the alias ...
-package # hide me from search.cpan.org
- Moose::Meta::Attribute::Custom::Counter;
-sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
+no MooseX::AttributeHelpers::Sugar;
1;
=item B<meta>
-=item B<method_provider>
-
-=item B<has_method_provider>
-
-=item B<helper_type>
-
-=item B<process_options_for_provides>
-
-Run before its superclass method.
-
-=item B<check_provides_values>
-
-Run after its superclass method.
-
-=back
-
=head1 PROVIDED METHODS
It is important to note that all those methods do in place
package MooseX::AttributeHelpers::Number;
use Moose;
+use MooseX::AttributeHelpers::Sugar;
+
+extends 'MooseX::AttributeHelpers::Base';
our $VERSION = '0.02';
our $AUTHORITY = 'cpan:STEVAN';
-extends 'MooseX::AttributeHelpers::Base';
-
-sub helper_type { 'Num' }
-
-# NOTE:
-# we don't use the method provider for this
-# module since many of the names of the provied
-# methods would conflict with keywords
-# - SL
-
-has '+method_constructors' => (
- default => sub {
- return +{
- set => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $writer->($_[0], $_[1]) };
- },
- add => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $writer->($_[0], $reader->($_[0]) + $_[1]) };
- },
- sub => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $writer->($_[0], $reader->($_[0]) - $_[1]) };
- },
- mul => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $writer->($_[0], $reader->($_[0]) * $_[1]) };
- },
- div => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $writer->($_[0], $reader->($_[0]) / $_[1]) };
- },
- mod => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $writer->($_[0], $reader->($_[0]) % $_[1]) };
- },
- abs => sub {
- my ($attr, $reader, $writer) = @_;
- return sub { $writer->($_[0], abs($reader->($_[0])) ) };
- },
- }
- }
+define_attribute_helper (
+ helper_type => 'Num',
+ method_provider => 'MooseX::AttributeHelpers::MethodProvider::Number',
+ shortcut => 'Number',
);
-
-no Moose;
-# register the alias ...
-package # hide me from search.cpan.org
- Moose::Meta::Attribute::Custom::Number;
-sub register_implementation { 'MooseX::AttributeHelpers::Number' }
+no Moose;
+no MooseX::AttributeHelpers::Sugar;
1;
+__END__
+
=pod
=head1 NAME
This provides a simple numeric attribute, which supports most of the
basic math operations.
-=head1 METHODS
-
-=over 4
-
-=item B<meta>
-
-=item B<helper_type>
-
-=item B<method_constructors>
-
-=back
-
-=head1 PROVIDED METHODS
-
-It is important to note that all those methods do in place
-modification of the value stored in the attribute.
-
-=over 4
-
-=item I<set ($value)>
-
-Alternate way to set the value.
-
-=item I<add ($value)>
-
-Adds the current value of the attribute to C<$value>.
-
-=item I<sub ($value)>
-
-Subtracts the current value of the attribute to C<$value>.
-
-=item I<mul ($value)>
-
-Multiplies the current value of the attribute to C<$value>.
-
-=item I<div ($value)>
-
-Divides the current value of the attribute to C<$value>.
-
-=item I<mod ($value)>
-
-Modulus the current value of the attribute to C<$value>.
-
-=item I<abs>
-
-Sets the current value of the attribute to its absolute value.
+=head1 METHOD PROVIDER
-=back
+The methods for this metaclass are provided by
+L<MooseX::AttributeHelpers::MethodProvider::String>.
=head1 BUGS
package MooseX::AttributeHelpers::String;
use Moose;
+use MooseX::AttributeHelpers::Sugar;
+
+extends 'MooseX::AttributeHelpers::Base';
our $VERSION = '0.01';
our $AUTHORITY = 'cpan:STEVAN';
-use MooseX::AttributeHelpers::MethodProvider::String;
-
-extends 'MooseX::AttributeHelpers::Base';
+define_attribute_helper (
+ default_options => {
+ is => 'rw',
+ default => '',
+ },
-has '+method_provider' => (
- default => 'MooseX::AttributeHelpers::MethodProvider::String'
+ helper_type => 'Str',
+ method_provider => 'MooseX::AttributeHelpers::MethodProvider::String',
+ auto_provide => 1,
+ shortcut => 'String',
);
-sub helper_type { 'Str' }
-
-before 'process_options_for_provides' => sub {
- my ($self, $options, $name) = @_;
-
- # Set some default attribute options here unless already defined
- if ((my $type = $self->helper_type) && !exists $options->{isa}){
- $options->{isa} = $type;
- }
-
- $options->{is} = 'rw' unless exists $options->{is};
- $options->{default} = '' unless exists $options->{default};
-};
-
-after 'check_provides_values' => sub {
- my $self = shift;
- my $provides = $self->provides;
-
- unless (scalar keys %$provides) {
- my $method_constructors = $self->method_constructors;
- my $attr_name = $self->name;
-
- foreach my $method (keys %$method_constructors) {
- $provides->{$method} = ($method . '_' . $attr_name);
- }
- }
-};
-
no Moose;
-
-# register the alias ...
-package # hide me from search.cpan.org
- Moose::Meta::Attribute::Custom::String;
-sub register_implementation { 'MooseX::AttributeHelpers::String' }
+no MooseX::AttributeHelpers::Sugar;
1;
If your attribute definition does not include any of I<is>, I<isa>,
I<default> or I<provides> but does use the C<String> metaclass,
then this module applies defaults as in the L</SYNOPSIS>
-above. This allows for a very basic counter definition:
+above. This allows for a very basic attribute definition:
has 'foo' => (metaclass => 'String');
$obj->append_foo;
=item B<meta>
-=item B<method_provider>
-
-=item B<has_method_provider>
-
-=item B<helper_type>
-
-=item B<process_options_for_provides>
-
-Run before its superclass method.
-
-=item B<check_provides_values>
-
-Run after its superclass method.
-
=back
=head1 PROVIDED METHODS