MooseX::AttributeDefaults - Role to provide default option for your attribute


MooseX-AttributeDefaults documentation  | view source Contained in the MooseX-AttributeDefaults distribution.

Index


NAME

Top

MooseX::AttributeDefaults - Role to provide default option for your attribute metaclasses

VERSION

Top

Version 0.02

SYNOPSIS

Top

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',
      },
    );

REQUIRED METHODS

Top

default_options

Return a list of options to default to. This is called as a class method with the attribute name as its only argument.

AUTHOR

Top

Paul Driver, <frodwith at cpan.org>

COPYRIGHT & LICENSE

Top


MooseX-AttributeDefaults documentation  | view source Contained in the MooseX-AttributeDefaults distribution.