Revision history for Perl extension MooseX-AttributeHelpers
-0.04
- ~~ changed 'make' references in README to 'Build' ~~
-
+0.03
+ ~~ more misc. doc updates ~~
+
* MooseX::AttributeHelpers::Counter
- now provides default attribute options for 'is',
'isa', 'provides', and 'default' if not specified.
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 ~~
+ called always.
0.02 Thurs. Sept. 13, 2007
~~ some misc. doc updates ~~
To install this module type the following:
+ perl Makefile.PL
+ make
+ make test
+ make install
+
+or:
+
perl Build.PL
./Build
./Build test
package MooseX::AttributeHelpers::Counter;
use Moose;
-our $VERSION = '0.01';
+our $VERSION = '0.02';
our $AUTHORITY = 'cpan:STEVAN';
use MooseX::AttributeHelpers::MethodProvider::Counter;
my ($self, $options, $name) = @_;
# Set some default attribute options here unless already defined
- if (my $type = $self->helper_type and not exists $options->{isa}){
+ if (my $type = $self->helper_type && !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");
+ $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);
+ }
}
};
Run before its superclass method.
+=item B<check_provides_values>
+
+Run after its superclass method.
+
=back
=head1 PROVIDED METHODS
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More no_plan => 1;
+
+BEGIN {
+ use_ok('MooseX::AttributeHelpers');
+}
+
+{
+ package MyHomePage;
+ use Moose;
+
+ has 'counter' => (metaclass => 'Counter');
+}
+
+my $page = MyHomePage->new();
+isa_ok($page, 'MyHomePage');
+
+can_ok($page, $_) for qw[
+ dec_counter
+ inc_counter
+ reset_counter
+];
+
+is($page->counter, 0, '... got the default value');
+
+$page->inc_counter;
+is($page->counter, 1, '... got the incremented value');
+
+$page->inc_counter;
+is($page->counter, 2, '... got the incremented value (again)');
+
+$page->dec_counter;
+is($page->counter, 1, '... got the decremented value');
+
+$page->reset_counter;
+is($page->counter, 0, '... got the original value');
+
+# check the meta ..
+
+my $counter = $page->meta->get_attribute('counter');
+isa_ok($counter, 'MooseX::AttributeHelpers::Counter');
+
+is($counter->helper_type, 'Num', '... got the expected helper type');
+
+is($counter->type_constraint->name, 'Num', '... got the expected default type constraint');
+
+is_deeply($counter->provides, {
+ inc => 'inc_counter',
+ dec => 'dec_counter',
+ reset => 'reset_counter',
+}, '... got the right default provides methods');
+