NAME

Meta::Builder::Base - Base class for Meta::Builder Meta Objects.

DESCRIPTION

Base class for all Meta::Builder Meta objects. This is where the methods used to define new metrics and actions live. This class allows for the creation of dynamic meta objects.

SYNOPSIS

My/Meta.pm:

package My::Meta;
use strict;
use warnings;

use base 'Meta::Builder::Base';

# Name the accessor that will be defined in the class that uses the meta object
# It is used to retrieve the classes meta object.
__PACKAGE__->set_accessor( "mymeta" );

# Add a metric with two actions
__PACKAGE__->add_metric(
    mymetric => sub { [] },
    pop => sub {
        my $self = shift;
        my ( $data ) = @_;
        pop @$data;
    },
    push => sub {
        my $self = shift;
        my ( $data, $metric, $action, @args ) = @_;
        push @$data => @args;
    }
);

# Add an additional action to the metric
__PACKAGE__->add_action( 'mymetric', get_ref => sub { shift });

# Add some predefined metric types + actions
__PACKAGE__->add_hash_metric( 'my_hashmetric' );
__PACKAGE__->add_lists_metric( 'my_listsmetric' );

My.pm:

package My;
use strict;
use warnings;

use My::Meta;

My::Meta->new( __PACKAGE__ );

# My::Meta defines mymeta() as the accessor we use to get our meta object.
# this is the ONLY way to get the meta object for this class.

mymeta()->mymetric_push( "some data" );
mymeta()->my_hashmetric_add( key => 'value' );
mymeta()->my_listsmetric_push( list => qw/valueA valueB/ );

# It works fine as an object/class method as well.
__PACKAGE__->mymeta->do_thing(...);

...;

PACKAGE METRIC

Whenever you create a new instance of a meta-object you must provide the name of the package to which the meta-object belongs. The 'package' metric will be set to this package name, and can be retrieved via the 'package' method: $meta-package()>.

HASH METRICS

Hash metrics are metrics that hold key/value pairs. A hash metric is defined using either the hash_metric() function, or the $meta-add_hash_metric()> method. The following actions are automatically defined for hash metrics:

$meta->add_METRIC( $key, $value )

Add a key/value pair to the metric. Will throw an exception if the metric already has a value for the specified key.

$value = $meta->get_METRIC( $key )

Get the value for a specified key.

$bool = $meta->has_METRIC( $key )

Check that the metric has the specified key defined.

$meta->clear_METRIC( $key )

Clear the specified key/value pair in the metric. (returns nothing)

$value = $meta->pull_METRIC( $key )

Get the value for the specified key, then clear the pair form the metric.

LISTS METRICS

$meta->push_METRIC( $key, @values )

Push values into the specified list for the given metric.

@values = $meta->get_METRIC( $key )

Get the values for a specified key.

$bool = $meta->has_METRIC( $key )

Check that the metric has the specified list.

$meta->clear_METRIC( $key )

Clear the specified list in the metric. (returns nothing)

@values = $meta->pull_METRIC( $key )

Get the values for the specified list in the metric, then clear the list.

CLASS METHODS

$meta = $class->new( $package, %metrics )

Create a new instance of the meta-class, and apply it to $package.

$metadata = $class->meta_meta()

Get the meta data for the meta-class itself. (The meta-class is build using meta-data)

$new_hashref = $class->gen_hash()

Generate a new empty hashref.

$name = $class->action_method_name( $metric, $action )

Generate the name of the method for the given metric and action. Override this if you do not like the METRIC_ACTION() method names.

OBJECT METHODS

$package = $meta->package()

Get the name of the package to which this meta-class applies.

$meta->set_accessor( $name )

Set the accessor that is used to retrieve the meta-object from the class to which it applies.

$meta->add_hash_metric( $metric, %actions )

Add a hash metric (see "HASH METRICS").

%actions should contain action =<gt sub {...}> pairs for constructing actions (See add_action()).

$meta->add_lists_metric( $metric, %actions )

Add a lists metric (see "LISTS METRICS")

%actions should contain action =<gt sub {...}> pairs for constructing actions (See add_action()).

$meta->add_metric( $metric, \&generator, %actions )

Add a custom metric. The second argument should be a sub that generates a default value for the metric.

%actions should contain action =<gt sub {...}> pairs for constructing actions (See add_action()).

$meta->add_action( $metric, $action => sub { ... } )

Add an action for the specified metric. See "ACTION AND HOOK METHODS" for details on how to write an action coderef.

$meta->hook_before( $metric, $action, sub { ... })

Add a hook for the specified metric. See "ACTION AND HOOK METHODS" for details on how to write a hook coderef.

$meta->hook_after( $metric, $action, sub { ... })

Add a hook for the specified metric. See "ACTION AND HOOK METHODS" for details on how to write a hook coderef.

ACTION AND HOOK METHODS

sub {
    my $self = shift;
    my ( $data, $metric, $action, @args ) = @_;
    ...;
}

Action and hook methods are called when someone calls $meta-<gtmetric_action(...)>. First all before hooks will be called, the action itself, and finally the after hooks will be called. All methods in the chain get the exact same unaltered arguments. Only the main action sub can return anything.

Arguments are:

0: $self

These are methods, so the first argument is the meta object itself.

1: $data

This is the data structure stored for the metric. This is the same as calling $meta->metric()

2: $metric

Name of the metric

3: $action

Name of the action

4+: @args

Arguments that metric_action() was called with.

DEFAULT ACTION METHODS

There are the default action methods used by hashmetrics and listsmetrics.

$meta->default_hash_add( $data, $metric, $action, $item, $value )
$value = $meta->default_hash_get( $data, $metric, $action, $item )
$bool = $meta->default_hash_has( $data, $metric, $action, $item )
$meta->default_hash_clear( $data, $metric, $action, $item )
$value = $meta->default_hash_pull( $data, $metric, $action, $item )
$meta->default_list_push( $data, $metric, $action, $item, @values )
@values = $meta->default_list_get( $data, $metric, $action, $item )
$bool = $meta->default_list_has( $data, $metric, $action, $item )
$meta->default_list_clear( $data, $metric, $action, $item )
@values = $meta->default_list_pull( $data, $metric, $action, $item )

AUTHORS

Chad Granum exodist7@gmail.com

COPYRIGHT

Copyright (C) 2010 Chad Granum

Meta-Builder is free software; Standard perl licence.

Meta-Builder is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.