From: Tom Lanyon Date: Wed, 19 Sep 2007 14:35:26 +0000 (+0000) Subject: added default attr options to Counter X-Git-Tag: 0.18_01~61 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=38abf7878684188d8f7109cc8d475766d09df3da;p=gitmo%2FMooseX-AttributeHelpers.git added default attr options to Counter --- diff --git a/ChangeLog b/ChangeLog index 2063a29..04abbb4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,20 @@ 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 ~~ @@ -24,4 +39,4 @@ Revision history for Perl extension MooseX-AttributeHelpers 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 diff --git a/README b/README index 96d99d3..86f70e6 100644 --- a/README +++ b/README @@ -7,10 +7,10 @@ INSTALLATION To install this module type the following: - perl Makefile.PL - make - make test - make install + perl Build.PL + ./Build + ./Build test + ./Build install DEPENDENCIES diff --git a/lib/MooseX/AttributeHelpers.pm b/lib/MooseX/AttributeHelpers.pm index 4bf4606..d1e6d47 100644 --- a/lib/MooseX/AttributeHelpers.pm +++ b/lib/MooseX/AttributeHelpers.pm @@ -120,6 +120,8 @@ Chris (perigrin) Prather Robert (phaylon) Sedlacek +Tom (dec) Lanyon + =head1 COPYRIGHT AND LICENSE Copyright 2007 by Infinity Interactive, Inc. diff --git a/lib/MooseX/AttributeHelpers/Base.pm b/lib/MooseX/AttributeHelpers/Base.pm index 95f8b72..72cf870 100644 --- a/lib/MooseX/AttributeHelpers/Base.pm +++ b/lib/MooseX/AttributeHelpers/Base.pm @@ -76,10 +76,7 @@ sub process_options_for_provides { 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 diff --git a/lib/MooseX/AttributeHelpers/Counter.pm b/lib/MooseX/AttributeHelpers/Counter.pm index f9f267c..32bc897 100644 --- a/lib/MooseX/AttributeHelpers/Counter.pm +++ b/lib/MooseX/AttributeHelpers/Counter.pm @@ -14,7 +14,25 @@ has '+method_provider' => ( ); 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 ... @@ -39,12 +57,13 @@ MooseX::AttributeHelpers::Counter 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', } ); @@ -57,6 +76,14 @@ MooseX::AttributeHelpers::Counter This module provides a simple counter attribute, which can be incremented and decremeneted. +If your attribute definition does not include any of I, I, +I or I but does use the C metaclass, +then this module applies defaults as in the L +above. This allows for a very basic counter definition: + + has 'foo' => (metaclass => 'Counter'); + $obj->inc_foo; + =head1 METHODS =over 4 @@ -67,6 +94,10 @@ incremented and decremeneted. =item B +=item B + +Run before its superclass method. + =back =head1 PROVIDED METHODS @@ -109,4 +140,4 @@ L 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