Edward J. Allen III > MooseX-Role-AttributeOverride-0.0.9 > MooseX::Role::AttributeOverride

Download:
MooseX-Role-AttributeOverride-0.0.9.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: v0.0.9   Source  

NAME ^

MooseX::Role::AttributeOverride - Allow roles to modify attributes

VERSION ^

  This document describes v0.0.9 of MooseX::Role::AttributeOverride - released June 29, 2011 as part of MooseX-Role-AttributeOverride.

SYNOPSIS ^

    {
        package MyApp::Role;
        use Moose::Role;
        use MooseX::Role::AttributeOverride;

        has_plus 'fun' => ( default => 'yep', );

        has_plus 'alive' => (
            default => 'yep',
            override_ignore_missing => 1,
        );
    }
    {
        package MyApp::Trait;
        use Moose::Role;
        use MooseX::Role::AttributeOverride;

        has_plus default => (
            default => sub {
                my $attr = shift;
                return sub { $attr->name }
            }
        );
    }
    {
        package MyApp;
        use Moose 1.9900;

        has nolife => (
            is     => 'rw',
            isa    => 'Str',
            traits => ['MyApp::Trait'],
        );

        has 'fun' => (
            is  => 'rw',
            isa => 'Str'
        );

        with qw(MyApp::Role);
    }
    {
        package main;
        use feature 'say';

        my $test = MyApp->new();

        say "I have " . $test->nolife;
        # Says I have nolife
        say "Are you having fun? " . $test->fun;
        # Says Are you having fun? yep

    }

DESCRIPTION ^

Moose doesn't allow roles to override attributes using the has '+attr' method. There are several good reasons for that. Basically, "that's not what a role is for." A role is a set of requirements with defaults. A class should always be able to override a role.

But sometimes you want a role to add features to a class. This is why Moose has method modifiers. This extension adds attribute modifiers.

INTERFACE ^

has_plus

This has exactly the same syntax as the Moose has command, except you should not use a plus to indicate you are overriding an attribute.

has_plus options
override_ignore_missing

Setting this to a true value will allow your role to have modifications to attributes that may not exist in the class it is applied to. The default is to die in these cases.

For example:

    package MyApp::Role;
    use Moose::Role;
    use MooseX::Role::AttributeOverride;

    has_plus 'alive' => (
        default => 'yep',
        override_ignore_missing => 1,
    );

    package MyApp;
    use Moose;

    with qw(MyApp::Role);
    # I'm not dead yet.

The above would not die, even though the MyApp package has no attribute named 'fun.'

IMPORTANT NOTE ^

Always apply a role that uses this module after defining attributes.

META USAGE ^

This role can be used in traits. For example, the following works:

    {
        package MyApp::Trait;
        use Moose::Role;
        use MooseX::Role::AttributeOverride;

        has_plus default => (
            default => sub {
                my $attr = shift;
                return sub { $attr->name }
            }
        );
    }
    {
        package MyApp;
        use Moose 1.9900;

        has nolife => (
            is     => 'rw',
            isa    => 'Str',
            traits => ['MyApp::Trait'],
        );

        with qw(MyApp::Role);
    }
    {
        package main;
        use feature 'say';

        my $test = MyApp->new();

        say "I have " . $test->nolife;
        # Says I have nolife
    }

DIAGNOSTICS ^

Can't find attribute $attr required by $role

You will see this error if your role has an attribute modification for an attribute that is not in the class. You can squash this by setting the 'override_ignore_missing' option in your 'has_plus' command.

Attempt to call has_plus on an invalid object

You really should never see this. Please file a bug report if you do. A test case would be nice as well.

Illegal inherited options

Moose will throw this error if you try to change an accessor option. See the Moose manual for more details.

Do not use a plus prefix with the has_plus sugar

There is no need for a plus sign on your attribute:

    # Good
    has 'children', trait => ['good']

    # Bad. Will die.
    has '+children', trait => ['naughty']

DEPENDENCIES ^

Moose 1.9900 or newer. Older versions may be supported in a future version of this module.

INCOMPATIBILITIES ^

I am sure that there are some MooseX modules that will not work with this. Please let me know, and I will at least document them.

BUGS AND LIMITATIONS ^

This is not the intended use of roles. As a result, take into account the following:

I am relatively new to Moose. I had an itch, and wrote this Module to scratch it. Please let me know how to make this module better.

For bugs, test cases are great!

SEE ALSO ^

Please see those modules/websites for more information related to this module.

SUPPORT ^

Websites

The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.

Email

You can email the author of this module at EALLENIII at cpan.org asking for help with any problems you have.

Bugs / Feature Requests

Please report any bugs or feature requests by email to bug-moosex-role-attributeoverride at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Role-AttributeOverride. You will be automatically notified of any progress on the request by the system.

Source Code

The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)

http://github.com/riemann42/MooseX-Role-AttributeOverride

  git clone git://github.com/riemann42/MooseX-Role-AttributeOverride.git

AUTHOR ^

Edward Allen <ealleniii_at_cpan_dot_org>

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2011 by Edward J. Allen III.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTY ^

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

syntax highlighting: