NAME

RapidApp::DefaultOverride

SYNOPSIS

package ParentClass;
use Moose;
has 'foo' => ( is => 'rw', isa => 'Int', default => -3 );
has 'bar' => ( is => 'rw', isa => 'HashRef', default => sub {{ a => 1, b => 2 }} );
has 'baz' => ( is => 'rw', isa => 'HashRef', lazy_build => 1 );
has 'complicated' => ( is => 'rw', isa => 'HashRef', default => { something => 'whatever' } );

1;

package SubClass;
use Moose;
extends 'ParentClass';

override_defaults({
  foo => 37
});
merge_defaults({
  bar => { a => 12, b => 16 },
  baz => { x => -1, y => 0, z => 1 },
  complicated => sub { my ($self, $v)= @_; return $self->complex_processing($v); },
});

1;

DESCRIPTION

These utility functions handle the various mechanisms required to properly override or merge the default value of an attribute which was defined in a parent class. (actually, it can override defaults for attributes defined in the current class too, but that isn't recommended)

An 'override' causes the default to be computed in an entirely new way. The original code used to create the default is ignored and not executed.

The new default can be anything that would notmally be valid for a default.

A 'merge' finds out what the original default was, and then either (if you passed a coderef) calls the given coderef to calculate the new value, or (if you passed anything else) merges the specified value with the original value using

our $merge= Hash::Merge->new('LEFT_PRECENDENT');
$merge->merge($given, $orig)

If you want behavior different than LEFT_PRECEDENT, just pass your own merge method, which can call Hash::Merge with your own parameters.

These methods will work with any defined Moose attributes, including those form Roles.

These methods might also work inside Roles, as well, but this isn't tested.

METHODS

override_defaults \%attrSpecs

override_defaults( {
  attr1 => $val1,
  attr2 => sub { my $self= shift; return $self->method; },
} );

The package name of the caller is found using the 'caller' method, so this should be considered a sugar method. For automated use, call 'override_default' directly.

override_default( $class, $attrName, $newDefault )

override_default( $class, $attrName, $refToSomeValue )
override_default( $class, $attrName, sub { my $self= shift; return $self->whatever(); } )

merge_defaults \%attrSpecs

merge_defaults( {
  attr1 => { hash => 'keys', to => 'merge', with => '' },
  attr2 => sub { my ($self, $v)= @_; $self->my_merge_routine($v); },
} );

The package name of the caller is found using the 'caller' method, so this should be considered a sugar method. For automated use, call 'merge_default' directly.

merge_default( $class, $attrName, $newDefault )

override_default( $class, $attrName, { hash => 'of', keys => 'to', merge => '' } )
override_default( $class, $attrName, sub { my ($self, $prevVal)= @_; return $self->whatever($prevVal); )