Base class for meta::builder meta objects.
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.
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(...);
...;
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 retirved via the 'package' method: \*(C`$meta-\*(C'package()>.
Hash metrics are metrics that hold key/value pairs. A hash metric is defined using either the \*(C`hash_metric()\*(C' function, or the \*(C`$meta-\*(C'add_hash_metric()> method. The following actions are automatically defined for hash metrics: Add a key/value pair to the metric. Will throw an exception if the metric already has a value for the specified key. Get the value for a specified key. Check that the metric has the specified key defined. Clear the specified key/value pair in the metric. (returns nothing) Get the value for the specified key, then clear the pair form the metric.
Push values into the specified list for the given metric. Get the values for a specified key. Check that the metric has the specified list. Clear the specified list in the metric. (returns nothing) Get the values for the specified list in the metric, then clear the list.
Create a new instance of the meta-class, and apply it to $package. Get the meta data for the meta-class itself. (The meta-class is build using meta-data) Generate a new empty hashref. Generate the name of the method for the given metric and action. Override this if you do not like the \s-1METRIC_ACTION\s0() method names.
Get the name of the package to which this meta-class applies. Set the accessor that is used to retrieve the meta-object from the class to which it applies. Add a hash metric (see \*(L"\s-1HASH\s0 \s-1METRICS\s0\*(R"). %actions should contain \*(C`action =<gt\*(C' sub {...}> pairs for constructing actions (See add_action()). Add a lists metric (see \*(L"\s-1LISTS\s0 \s-1METRICS\s0\*(R") %actions should contain \*(C`action =<gt\*(C' sub {...}> pairs for constructing actions (See add_action()). Add a custom metric. The second argument should be a sub that generates a default value for the metric. %actions should contain \*(C`action =<gt\*(C' sub {...}> pairs for constructing actions (See add_action()). Add an action for the specified metric. See \*(L"\s-1ACTION\s0 \s-1AND\s0 \s-1HOOK\s0 \s-1METHODS\s0\*(R" for details on how to write an action coderef. Add a hook for the specified metric. See \*(L"\s-1ACTION\s0 \s-1AND\s0 \s-1HOOK\s0 \s-1METHODS\s0\*(R" for details on how to write a hook coderef. Add a hook for the specified metric. See \*(L"\s-1ACTION\s0 \s-1AND\s0 \s-1HOOK\s0 \s-1METHODS\s0\*(R" for details on how to write a hook coderef.
sub { my $self = shift; my ( $data, $metric, $action, @args ) = @_; ...; }
Action and hook methods are called when someone calls \*(C`$meta-<gt\*(C'metric_action(...)>. First all before hooks will be called, the 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: These are methods, so the first argument is the meta object itself. This is the data structure stored for the metric. This is the same as calling $meta->metric() Name of the metric Name of the action Arguments that metric_action() was called with.
There are the default action methods used by hashmetrics and listsmetrics.
Chad Granum [email protected]
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 \s-1WITHOUT\s0 \s-1ANY\s0 \s-1WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. See the license for more details.