MooseX::AttributeDefaults - Role to provide default option for your attribute metaclasses
Version 0.02
Although you can do similar things by overriding attributes in subclasses of Moose::Meta::Attribute, there are a couple of gotchas (as of this writing, for instance, overriding 'is' does nothing at all). This role abstracts the implementation details of the available workarounds.
package My::Custom::Metaclass; use Moose; extends 'Moose::Meta::Attribute'; with 'MooseX::AttributeDefaults'; sub default_options { my ($class, $name) = @_; return ( is => 'ro', isa => 'Str', default => "default value for $name"; ); } package Some::Class; use Moose; has 'attr' => ( metaclass => 'My::Custom::Metaclass', predicate => 'has_attr', ); # 'attr' is a ro string with "default value for attr" as its # default and a 'has_attr' predicate ### Or as a trait instead of a metaclass package Acme::Common::Array; use Moose::Role; with qw(MooseX::AttributeDefaults); sub default_options { is => 'ro', isa => 'ArrayRef', default => sub { [] }, } package Some::Class; use Moose; use MooseX::AttributeHelpers; has attr => ( metaclass => 'Collection::Array', traits => [qw(Acme::Common::Array)], provides => { 'push' => 'add_attr', }, );
Return a list of options to default to. This is called as a class method with the attribute name as its only argument.
Paul Driver, <frodwith at cpan.org>
<frodwith at cpan.org>
Copyright (C) 2008 Paul Driver.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install MooseX::AttributeDefaults, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::AttributeDefaults
CPAN shell
perl -MCPAN -e shell install MooseX::AttributeDefaults
For more information on module installation, please visit the detailed CPAN module installation guide.