| 1 | package Data::Transfigure::Type 1.03; | ||
| 2 | 2 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # ABSTRACT: a transfigurator that filters by reference type | ||
| 6 | |||
| 7 | =encoding UTF-8 | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Data::Transfigure::Type - a transfigurator that filters by reference type | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | C<Data::Transfigure::Type> is a transfigurator that applies to one or more value | ||
| 16 | types. It detects both exact type matches and inherited type matches (including | ||
| 17 | role-implementing), giving priority to the former. | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | 0 | ▶ | use Object::Pad; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 22 | |||
| 23 | class Data::Transfigure::Type : does(Data::Transfigure::Node) { | ||
| 24 | use Data::Transfigure::Constants; | ||
| 25 | |||
| 26 | use Scalar::Util qw(blessed); | ||
| 27 | |||
| 28 | =head1 FIELDS | ||
| 29 | |||
| 30 | =head2 type (required param) | ||
| 31 | |||
| 32 | The type to check against. To check for multiple types, provide an arrayref of | ||
| 33 | the type names. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | field $type : param; | ||
| 38 | |||
| 39 | ADJUST { | ||
| 40 | foreach my $t (grep {defined} $self->types()) { | ||
| 41 | die("$t cannot be used with Data::Transfigure::Type - use Data::Transfigure::Schema") | ||
| 42 | if ($t eq 'ARRAY' || $t eq 'HASH'); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | =head1 METHODS | ||
| 47 | |||
| 48 | =head2 types() | ||
| 49 | |||
| 50 | Returns a list of the types to be checked against | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | method types() { | ||
| 55 | return ref($type) eq 'ARRAY' ? $type->@* : $type; | ||
| 56 | } | ||
| 57 | |||
| 58 | =head2 applies_to( %params ) | ||
| 59 | |||
| 60 | Requires C<$params{value}> to exist | ||
| 61 | |||
| 62 | If C<$params{value}>'s type is exactly any of C<types()>, returns | ||
| 63 | C<$MATCH_EXACT_TYPE>. | ||
| 64 | |||
| 65 | Otherwise, if the value's type is a subclass of any of C<types()>, returns | ||
| 66 | C<$MATCH_INHERITED_TYPE>. | ||
| 67 | |||
| 68 | Otherwise returns C<$NO_MATCH>. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | method applies_to (%params) { | ||
| 73 | die('value is a required parameter for Data::Transfigure::Type->applies_to') unless (exists($params{value})); | ||
| 74 | my $node = $params{value}; | ||
| 75 | |||
| 76 | my $rv = $NO_MATCH; | ||
| 77 | if (my $r = ref($node)) { | ||
| 78 | foreach ($self->types()) { | ||
| 79 | return $MATCH_EXACT_TYPE if ($r eq $_); | ||
| 80 | $rv = $MATCH_INHERITED_TYPE if (blessed($node) && ($node->isa($_) || $node->DOES($_))); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return $rv; | ||
| 84 | } | ||
| 85 | |||
| 86 | } | ||
| 87 | |||
| 88 | =pod | ||
| 89 | |||
| 90 | =head1 AUTHOR | ||
| 91 | |||
| 92 | Mark Tyrrell C<< <mark@tyrrminal.dev> >> | ||
| 93 | |||
| 94 | =head1 LICENSE | ||
| 95 | |||
| 96 | Copyright (c) 2024 Mark Tyrrell | ||
| 97 | |||
| 98 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 99 | of this software and associated documentation files (the "Software"), to deal | ||
| 100 | in the Software without restriction, including without limitation the rights | ||
| 101 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 102 | copies of the Software, and to permit persons to whom the Software is | ||
| 103 | furnished to do so, subject to the following conditions: | ||
| 104 | |||
| 105 | The above copyright notice and this permission notice shall be included in all | ||
| 106 | copies or substantial portions of the Software. | ||
| 107 | |||
| 108 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 109 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 110 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 111 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 112 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 113 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 114 | SOFTWARE. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ | ||