NAME
MojoX::Authentication::Model::Role::Remap;
SYNOPSIS
package Some::Class;
use Moo;
with 'MojoX::Authentication::Model::Role::Remap';
sub some_method ($self) {
my $record = { ... };
my $mappings = [ ... ];
$self->remap($record, $mappings);
}
DESCRIPTION
Role providing a method "remap" to perform smart remapping over a record, e.g. to adapt keys from columns in a database over to what is expected in some other component, or to produce new keys based on what is available.
INTERFACE
remap
$provider->remap($record, $mappings); # OR
$provider->remap($record, $mappings, $is_backwards);
Perform the mapping as explained below. In case $is_backwards is present and true, the mapping is applied backwards.
The $record argument is the hash reference where remapping will be applied. It is changed, so make sure to copy it if you need to preserve the original.
The $mappings argument is an array reference that can contain three kind of items:
code reference: the expected signature is as follows:
sub ($record, $is_backwards)and is called passing the corresponding arguments (
$is_backwardsis set to0if missing in the call to theremapfunction).The called function is supposed to know what to do over
$recordin both the forward and the backwards cases.array reference: it is expected to contain an even list of keys, arranged as output/input key pairs considering the forward direction. Mapping will take the value corresponding to the input key, if present, and assigning it to the output key.
Examples:
$record = { foo => 'hey!', bar => 'not for much' }; $map = [ qw< bar foo galook baz > ]; # pair #1 is output(bar) input(foo). The input key exists so: $record->{bar} = $record->{foo} # pair #2 is output(galook) input(baz). The input key does *not* # exist so it is skipped. # final record after the mapping: $record = { foo => 'hey!', bar => 'hey!' };When
$is_backwardsis true, the same algorithm is applied to the reversed list (hence roles of output and input keys are reversed and traversed in reverse order).hash reference: it is expected to contain the following keys:
okey(required) the output key in the forward direction (becomes the input key in the backwards direction)ikey(required) the input key in the forward direction (becomes the output key in the backwards direction)op/backwards_op(optional) an operation to be applied over the value before it is set for the output key. If set it must be a sub reference.opis applied when the mapping is in the forward direction ($is_backwardsmissing or false),backwards_opotherwise.
The two operations are optional; in case they are missing, it's just a different way to copy one value from the
ikeyto theokey.Examples:
$map = { okey => 'bar', ikey => 'foo', fwd_op => sub ($x) { join(', ', $x->@*) }, bwd_op => sub ($x) { [ split m{,\ }mxs, $x ] }, }; # forward direction $record = { foo => [ 1, 2, 3 ] }; # --> { foo => [ 1, 2, 3 ], bar => '1, 2, 3' } # backwards direction $record = { bar => '5, 6, 7' } # --> { foo => [ 5, 6, 7 ], bar => '5, 6, 7' }It is up to the operations to make sure that the forward and backwards operations are actually one the reverse of the other.
It is also possible to set
okeythe same asikeyto perform in-place marshalling of the parameters:$map = { okey => 'foo', ikey => 'foo', op => sub ($x) { join(', ', $x->@*) }, backwards_op => sub ($x) { [ split m{,\ }mxs, $x ] }, }; # forward direction $record = { foo => [ 1, 2, 3 ] }; # --> { foo => '1, 2, 3' } # backwards direction $record = { foo => '5, 6, 7' } # --> { foo => [ 5, 6, 7 ] }
ANYTHING ELSE (INCLUDING AUTHOR, COPYRIGHT AND LICENSE)
See documentation for MojoX::Authentication.