From: Stevan Little Date: Thu, 20 Sep 2007 12:50:56 +0000 (+0000) Subject: tweaking decs stuff just a bit X-Git-Tag: 0.18_01~60 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=829736f940bf63730acfa8166eb033222629d879;p=gitmo%2FMooseX-AttributeHelpers.git tweaking decs stuff just a bit --- diff --git a/ChangeLog b/ChangeLog index 04abbb4..9ea1aad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,8 @@ 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. @@ -13,10 +13,7 @@ Revision history for Perl extension MooseX-AttributeHelpers 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 ~~ diff --git a/README b/README index 86f70e6..d6c08d3 100644 --- a/README +++ b/README @@ -7,6 +7,13 @@ INSTALLATION To install this module type the following: + perl Makefile.PL + make + make test + make install + +or: + perl Build.PL ./Build ./Build test diff --git a/lib/MooseX/AttributeHelpers/Counter.pm b/lib/MooseX/AttributeHelpers/Counter.pm index 32bc897..70fe41a 100644 --- a/lib/MooseX/AttributeHelpers/Counter.pm +++ b/lib/MooseX/AttributeHelpers/Counter.pm @@ -2,7 +2,7 @@ package MooseX::AttributeHelpers::Counter; use Moose; -our $VERSION = '0.01'; +our $VERSION = '0.02'; our $AUTHORITY = 'cpan:STEVAN'; use MooseX::AttributeHelpers::MethodProvider::Counter; @@ -19,17 +19,25 @@ 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}){ + 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); + } } }; @@ -98,6 +106,10 @@ above. This allows for a very basic counter definition: Run before its superclass method. +=item B + +Run after its superclass method. + =back =head1 PROVIDED METHODS diff --git a/t/011_counter_with_defaults.t b/t/011_counter_with_defaults.t new file mode 100644 index 0000000..5a5e74d --- /dev/null +++ b/t/011_counter_with_defaults.t @@ -0,0 +1,56 @@ +#!/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'); +