MooseX::Meta::Attribute::Index - Provides index meta attribute trait


MooseX-Meta-Attribute-Index documentation  | view source Contained in the MooseX-Meta-Attribute-Index distribution.

Index


NAME

Top

MooseX::Meta::Attribute::Index - Provides index meta attribute trait

SYNOPSIS

Top

    package App;
        use Moose;
            with 'MooseX::Meta::Attribute::Index';

        has attr_1 => ( 
            traits  => [ qw/Index/ ] ,
            is      => 'rw'     , 
            isa     => 'Str'    ,
            index   => 0
        );

        has attr_2 => (
            traits  => [ qw/Index/ ] ,
            is      => 'rw'     ,
            isa     => 'Int'    ,
            index   => 1
        ) ;




    package main;
        my $app = App->new( attr_1 => 'foo', attr_2 => 42 );

        $app->get_attribute_index( "attr_1" );  # 0
        $app->get_attribute_index( "attr_2" );  # 1

        $app->get_attribute_by_index(0); # returns attr_1 object
        $app->get_attribute_by_index(1); # returns attr_2 object

        $app->get_attribute_name_by_index(0); # returns attr_1 object
        $app->get_attribute_name_by_index(1); # returns attr_2 object




DESCRIPTION

Top

This module is a Moose role which implements a meta-attribute, index , using traits. The index meta attribute is useful for ordered of attributes. In standard Moose, attributes are implemented via hash references and order is not preserved. This module implements a meta-attribute that can be used to track the order of attributes where the order of attributes matters. For example, see ODG::Record where maintaining of the order of attributes allows for a Moose class to use an array ref for storage rather than a hash ref.

The indexes must be defined and provided manually. The indexs are checked to ensure that negative indices are not used.

In addition to the meta-attribute, several methods are introduced to work with the indexed attributes. See #methods below. If you just want the meta attribute without the added methods, have your class use the role 'MooseX::Meta::Attribute::Trait::Index'.

METHODS

Top

The following methods are loaded into your class when this role is used.

get_attribute_index( $attr_name )

Returns the index for the attribute c<$attr_name>

get_attribute_by_index( $index )

Returns the attribute associated with the index

get_attribute_name_by_index( $index )

Returns the attribute name associated with $index

SEE ALSO

Top

ODG::Record, Moose

AUTHOR

Top

Christopher Brown, cbrown -at- opendatagroup.com

http://www.opendatagroup,com

COPYRIGHT AND LICENSE

Top


MooseX-Meta-Attribute-Index documentation  | view source Contained in the MooseX-Meta-Attribute-Index distribution.