NAME

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

SYNOPSIS

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

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.

METHODS

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

ODG::Record, Moose

AUTHOR

Christopher Brown, "cbrown -at- opendatagroup.com"

http://www.opendatagroup,com

COPYRIGHT AND LICENSE

Copyright (C) 2008 by Open Data

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.