Revision history for Perl extension MooseX-AttributeHelpers
+0.04
+ ~~ changed 'make' references in README to 'Build' ~~
+
+ * MooseX::AttributeHelpers::Counter
+ - now provides default attribute options for 'is',
+ 'isa', 'provides', and 'default' if not specified.
+
+ * MooseX::AttributeHelpers::Base
+ - added attribute $name to the params passed to
+ process_options_or_provides(), which gives us more
+ flexibility when writing additional helpers
+ - removed check for 'provides' and 'isa' attr
+ options before _process_options. It should be
+ called always.
+
0.03
~~ more misc. doc updates ~~
derived from parts of the old Collection::Array
0.01 Mon. Aug. 13, 2007
- - module released to CPAN
\ No newline at end of file
+ - module released to CPAN
before '_process_options' => sub {
my ($self, $name, $options) = @_;
- if (exists $options->{provides} ||
- exists $options->{isa} && $options->{isa} =~ /^.*?\[.*?\]$/) {
- $self->process_options_for_provides($options);
- }
+ $self->process_options_for_provides($options, $name);
};
## methods called after instantiation
);
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 and not exists $options->{isa}){
+ $options->{isa} = $self->helper_type;
+ }
+ $options->{is} = 'ro' unless exists $options->{is};
+ $options->{default} = 0 unless exists $options->{default};
+ # If no provides are specified we'll default to all of them
+ unless ( exists $options->{provides} and
+ grep { exists $options->{provides}{$_} } qw( inc dec reset )
+ ){
+ @{$options->{provides}}{qw(inc dec reset)} = ("inc_$name", "dec_$name", "reset_$name");
+ }
+};
+
no Moose;
# register the alias ...
has 'counter' => (
metaclass => 'Counter',
- is => 'rw',
- isa => 'Int',
+ is => 'ro',
+ isa => 'Num',
default => sub { 0 },
provides => {
inc => 'inc_counter',
dec => 'dec_counter',
+ reset => 'reset_counter',
}
);
This module provides a simple counter attribute, which can be
incremented and decremeneted.
+If your attribute definition does not include any of I<is>, I<isa>,
+I<default> or I<provides> but does use the C<Counter> metaclass,
+then this module applies defaults as in the L</SYNOPSIS>
+above. This allows for a very basic counter definition:
+
+ has 'foo' => (metaclass => 'Counter');
+ $obj->inc_foo;
+
=head1 METHODS
=over 4
=item B<helper_type>
+=item B<process_options_for_provides>
+
+Run before its superclass method.
+
=back
=head1 PROVIDED METHODS
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
-=cut
\ No newline at end of file
+=cut