Christopher J. Madsen > MooseX-AttributeTree-0.04 > MooseX::AttributeTree

Download:
MooseX-AttributeTree-0.04.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.04   Source   Latest Release: MooseX-AttributeTree-0.05

NAME ^

MooseX::AttributeTree - Inherit attribute values like HTML+CSS does

VERSION ^

This document describes version 0.04 of MooseX::AttributeTree, released October 11, 2011 as part of MooseX-AttributeTree version 0.04.

SYNOPSIS ^

  package MyClass;
  use Moose;
  use MooseX::AttributeTree ();

  has parent => (
    is       => 'rw',
    isa      => 'Object',
    weak_ref => 1,
  );

  has value => (
    is     => 'rw',
    traits => [qw/TreeInherit/],
  );

DESCRIPTION ^

Classes can inherit attributes from their parent classes. But sometimes you want an attribute to be able to inherit its value from a parent object. For example, that's how CSS styles work in HTML.

MooseX::AttributeTree allows you to apply the TreeInherit trait to any attribute in your class. This changes the way the attribute's accessor method works. When reading the attribute's value, if no value has been set for the attribute in this object, the accessor will return the value from the parent object (which might itself be inherited).

The parent object does not need to be the same type as the child object, but it must have a method with the same name as the attribute's accessor method (unless you supply a fetch_method). (The parent's method may be an attribute accessor method, but it doesn't have to be.) If the parent doesn't have the right method, you'll get a runtime error if the child tries to call it.

By default, MooseX::AttributeTree expects to get the parent object by calling the object's parent method. However, you can use any method to retrieve the link by passing the appropriate parent_link to the TreeInherit trait:

  has ancestor => (
    is       => 'rw',
    isa      => 'Object',
    weak_ref => 1,
  );

  has value => (
    is     => 'ro',
    traits => [ TreeInherit => { parent_link => 'ancestor' } ],
  );

If the method returns undef, then inheritance stops and the accessor will behave like a normal accessor. (Normally, parent_link will be the name of an attribute accessor method, but it doesn't have to be.)

Sometimes it's not convenient for the parent object to have a separate method for each attribute that a child object might want to inherit. In that case, you can supply a fetch_method to the TreeInherit trait.

  has other_value => (
    is     => 'ro',
    traits => [ TreeInherit => { fetch_method => 'get_inherited' } ],
  );

With fetch_method, the inherited value will come from

  $self->parent->get_inherited('other_value');

instead of the usual

  $self->parent->other_value();

If your attribute has a predicate method, it reports whether the attribute has been set on that object. The predicate has no knowledge of any value that might be inherited from a parent. This means that $object->has_value may return false even though $object->value would return a value (inherited from the parent).

Likewise, the attribute's clearer method (if any) would clear the attribute only on this object, and would never affect a parent object.

ATTRIBUTES ^

default

This attribute will provide the default value for the inherited attribute when no value has been set on this object and no value could be inherited from the parent. It has the same semantics as Moose's standard default option, in that it can be either a Value or a CodeRef to call as a method on the object with no parameters.

The difference is that the default value is not stored in the object. If you provide a CodeRef, it will be called every time the default value is needed.

fetch_method

This is the name of the method to call in the parent object to ask for the value of an attribute. The method is passed the name of this attribute as its sole argument. This allows attributes to be inherited without requiring the parent object to know about every possible attribute.

If fetch_method is not set, then the parent's value is fetched by calling the method with the same name as the attribute's read accessor method. In that case, no parameters are passed to the parent method.

parent_link

This is the name of the method to call to retrieve the object's parent. The default is parent.

CONFIGURATION AND ENVIRONMENT ^

MooseX::AttributeTree requires no configuration files or environment variables.

DEPENDENCIES ^

MooseX::AttributeTree depends on MooseX::Role::Parameterized, which can be found on CPAN.

INCOMPATIBILITIES ^

None reported.

BUGS AND LIMITATIONS ^

There is no inline version of the accessor methods, so using the TreeInherit trait will slow down access to that attribute. But in practice, it hasn't been slow enough to be noticeable.

No attempt is made to detect circular dependencies, which may cause an infinite loop. (This should not be an issue in a proper tree structure, which should not have circular dependencies.)

If an accessor returns undef, there's no way to tell whether no ancestor had the attribute set, or one of them explicitly set it to undef. (Well, you could walk the inheritance tree yourself and call the predicate method of each ancestor.)

AUTHOR ^

Christopher J. Madsen <perl AT cjmweb.net>

Please report any bugs or feature requests to <bug-MooseX-AttributeTree AT rt.cpan.org>, or through the web interface at http://rt.cpan.org/Public/Bug/Report.html?Queue=MooseX-AttributeTree

You can follow or contribute to MooseX-AttributeTree's development at http://github.com/madsen/moosex-attributetree.

ACKNOWLEDGMENTS ^

I'd like to thank Jesse Luehrs, who explained what I needed to do to get this module to work, and Micro Technology Services, Inc. http://www.mitsi.com, who sponsored its development.

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2011 by Christopher J. Madsen.

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 LICENSE, 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: