MooseX::MutatorAttributes - Moose Role to add a quick set method that returns self


MooseX-MutatorAttributes documentation Contained in the MooseX-MutatorAttributes distribution.

Index


Code Index:

NAME

Top

MooseX::MutatorAttributes - Moose Role to add a quick set method that returns self

VERSION

Top

Version 0.12

SYNOPSIS

Top

I got tired of doing this:

    $obj->attr1($value1);
    $obj->attr2($value2);
    $obj->method_that_uses_attr;

What I wanted to do was:

    with qw{MooseX::MutatorAttributes};
    $obj->set( attr1 => $value1, attr2 => $value2 )->method_that_uses_attr;




Now I can, and so can you.

METHOD

Top

set

    $self->set( HASH );

Set takes a hash, keys are expected to be attributes, if they are not then we Carp::croak. If a key is an acceptable attribute then we attempt to set with $value.

set_only_rw_attr

    my $storage_href = {};
    $self->set_only_rw_attr($storage_href, HASH );

Set takes a hash, keys are expected to be attributes, but unlike set, we don't die. Instead we populate a given storage ref that was passed in. It's up to what to do with it. Post 'set' your $storage_href will hold all the non-writeable-attrs (either was not an attr or was 'ro').

If you wish to by-pass the storage part pass in an annon hashref {}.

AUTHOR

Top

ben hengst, <notbenh at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-moosex-setter at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-MutatorAttributes. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc MooseX::MutatorAttributes




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-MutatorAttributes

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/MooseX-MutatorAttributes

* CPAN Ratings

http://cpanratings.perl.org/d/MooseX-MutatorAttributes

* Search CPAN

http://search.cpan.org/dist/MooseX-MutatorAttributes

ACKNOWLEDGEMENTS

Top

This would not be possible with out stevan, mst, and everyone else who hangs out on #moose.

COPYRIGHT & LICENSE

Top


MooseX-MutatorAttributes documentation Contained in the MooseX-MutatorAttributes distribution.
package MooseX::MutatorAttributes;
use Moose::Role;
use Carp;
use Carp::Assert::More;


our $VERSION = '0.12';


sub set {
   my ($self, %opts) = @_;
   while ( my ($name, $value) = each %opts ) {
      croak sprintf q{[!!!] %s is not an attribute to set for %s}, $name, $self
         unless defined $self->meta->find_attribute_by_name($name);
      
      my $setter = $self->meta->find_attribute_by_name($name)->get_write_method;
      croak sprintf q{[!!!] %s is not writable, no setter defined}, $name
         unless defined $setter;

      $self->$setter($value);
   }
   return $self;
}

sub set_only_rw_attr {
   my ($self, $store, %opts) = @_;
   assert_hashref($store);
   
   while ( my ($name, $value) = each %opts ) {
      my $setter = $self->meta->find_attribute_by_name($name)->get_write_method
         if defined $self->meta->find_attribute_by_name($name) ; 

      if ( defined $setter ) {
         $self->$setter($value);
      } 
      else {
         $store->{$name} = $value;
      }
   }
   return $self;
}






1; # End of MooseX::MutatorAttributes