The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Data::Transfigure::Position - a compound transfigurator that specifies one or more locations within the data structure to apply to

SYNOPSIS

    Data::Transfigure::Position->new(
      position => '/*/author',
      transfigurator => Data::Transfigure::Type->new(
        type => 'Result::Person',
        handler => sub ($data) {
          sprintf("%s, %s", $data->lastname, $data->firstname)
        }
      )
    ); # applies to any 2nd-level hash key "author", but only if that value's
       # type is 'Result::Person', and then performs a custom stringification

    Data::Transfigure::Position->new(
      position => '/book/author',
      transfigurator => Data::Transfigure::Default->new(
        handler => sub ($data ) {
          {
            firstname => $data->names->{first} // '',
            lastname  => $data->names->{last} // '',
          }
        }
      )
    ); # applies only to the node at $data->{book}->{author}, and tries to 
       # hash-ify the value there, regardless of its type.

DESCRIPTION

Data::Transfigure::Position is a compound transfigurator, meaning that it both is, and has, a transfigurator. The transfigurator you give it at construction can be of any type, with its own handler and match criteria. The Data::Transfigure::Position's handler will become the one from the supplied transfigurator, so handler should not be specified when creating this transfigurator.

This construction is used so that transfigurators can be treated like building blocks and in some cases inserted to apply to the entire tree, but in other scenarios, used much more specifically.

FIELDS

position (required parameter)

Ex. "/book/author", "/*/*/title", "/values/0/id"

A position specifier for a location in the data structure. The forward slash character is used to delineate levels, which are hashes or arrays. The asterisk character can be used to represent "anything" at that level, whether hash key or array index.

Cam be an arrayref of position specifiers to match any of them.

transfigurator (required parameter)

A Data::Transfigure transfigurator conforming to the Data::Transfigure::Node role. Weird things will happen if you provide a Data::Transfigure::Tree -type transfigurator, so you probably shouldn't do that.

applies_to( %params )

$params{position} must exist, as well as any params required by the supplied transfigurator.

Passes %params to the instance's transfigurator's applies_to method - if that results in $NO_MATCH, then that value is returned by this method.

Then, the position(s) is/are checked against $params{position}. If any matches exactly, returns $MATCH_EXACT_POSITION. Otherwise, if any matches with wildcard evaluation, returns $MATCH_WILDCARD_POSITION.

If no positions match, returns $NO_MATCH

AUTHOR

Mark Tyrrell <mark@tyrrminal.dev>

LICENSE

Copyright (c) 2024 Mark Tyrrell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.