From: Shawn M Moore Date: Sun, 25 May 2008 00:57:45 +0000 (+0000) Subject: MooseX::AttributeHelpers::Trait::Counter X-Git-Tag: 0.18_01~29 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7fc7e05479036c7afbf60bf4581af75ddf195fbf;p=gitmo%2FMooseX-AttributeHelpers.git MooseX::AttributeHelpers::Trait::Counter --- diff --git a/lib/MooseX/AttributeHelpers/Trait/Counter.pm b/lib/MooseX/AttributeHelpers/Trait/Counter.pm new file mode 100644 index 0000000..d402f13 --- /dev/null +++ b/lib/MooseX/AttributeHelpers/Trait/Counter.pm @@ -0,0 +1,56 @@ + +package MooseX::AttributeHelpers::Trait::Counter; +use Moose::Role; + +with 'MooseX::AttributeHelpers::Trait::Base' + => { excludes => ['method_provider'] }; + +our $VERSION = '0.03'; +our $AUTHORITY = 'cpan:STEVAN'; + +use MooseX::AttributeHelpers::MethodProvider::Counter; + +has 'method_provider' => ( + is => 'ro', + isa => 'ClassName', + predicate => 'has_method_provider', + default => 'MooseX::AttributeHelpers::MethodProvider::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::Role; + +# register the alias ... +package # hide me from search.cpan.org + Moose::Meta::Attribute::Custom::Trait::Counter; +sub register_implementation { 'MooseX::AttributeHelpers::Trait::Counter' } + +1; +