« blib/lib/SQL/Translator/Parser/Storable.pm
↑ Summary
blib/lib/SQL/Translator/Schema/Role/Extra.pm »
| 1 | package SQL::Translator::Role::ListAttr; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 77 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 77 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | SQL::Translator::Role::ListAttr - context-sensitive list attributes | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package Foo; | ||||||||||||||||||||||||||||
| 13 | use Moo; | ||||||||||||||||||||||||||||
| 14 | use SQL::Translator::Role::ListAttr; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | with ListAttr foo => ( uniq => 1, append => 1 ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | This package provides a variable L<Moo::Role> for context-sensitive list | ||||||||||||||||||||||||||||
| 21 | attributes. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 77 | ▶ | use SQL::Translator::Utils qw(parse_list_arg ex2err uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 77 | ▶ | use Sub::Quote qw(quote_sub); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 77 | ▶ | use Package::Variant ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 77 | importing => { | |||||||||||||||||||||||||||
| 30 | 'Moo::Role' => [], | ||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | subs => [qw(has around)], | ||||||||||||||||||||||||||||
| 33 | 77 | ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 ListAttr $name => %parameters; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Returns a L<Moo::Role> providing an arrayref attribute named C<$name>, | ||||||||||||||||||||||||||||
| 40 | and wrapping the accessor to provide context-sensitivity both for | ||||||||||||||||||||||||||||
| 41 | setting and getting. If no C<builder> or C<default> is provided, the | ||||||||||||||||||||||||||||
| 42 | default value is the empty list. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | On setting, the arguments are parsed using | ||||||||||||||||||||||||||||
| 45 | L<SQL::Translator::Utils/parse_list_arg>, and the accessor will return | ||||||||||||||||||||||||||||
| 46 | an array reference or a list, depending on context. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head3 Parameters | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =over | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item append | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | If true, the setter will append arguments to the existing ones, rather | ||||||||||||||||||||||||||||
| 55 | than replacing them. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =item uniq | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | If true, duplicate items will be removed, keeping the first one seen. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =item may_throw | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | If accessing the attribute might L<throw|SQL::Translator::Utils/throw> | ||||||||||||||||||||||||||||
| 64 | an exception (e.g. from a C<builder> or C<isa> check), this should be | ||||||||||||||||||||||||||||
| 65 | set to make the accessor store the exception using | ||||||||||||||||||||||||||||
| 66 | L<SQL::Translator::Role::Error> and return undef. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =item undef_if_empty | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | If true, and the list is empty, the accessor will return C<undef> | ||||||||||||||||||||||||||||
| 71 | instead of a reference to an empty in scalar context. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =back | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Unknown parameters are passed through to the L<has|Moo/has> call for | ||||||||||||||||||||||||||||
| 76 | the attribute. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 754 | ▶ | sub make_variant { | ||||||||||||||||||||||||||
|
Subroutine
make_variant: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 754 | my ($class, $target_package, $name, %arguments) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 754 | my $may_throw = delete $arguments{may_throw}; | |||||||||||||||||||||||||||
| 84 | 754 | my $undef_if_empty = delete $arguments{undef_if_empty}; | |||||||||||||||||||||||||||
| 85 | 754 | my $append = delete $arguments{append}; | |||||||||||||||||||||||||||
| 86 | my $coerce | ||||||||||||||||||||||||||||
| 87 | = delete $arguments{uniq} | ||||||||||||||||||||||||||||
| 88 | 933 | ▶ | ? sub { [ uniq @{ parse_list_arg($_[0]) } ] } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 754 | ▶ | : \&parse_list_arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | has($name => ( | ||||||||||||||||||||||||||||
| 92 | is => 'rw', | ||||||||||||||||||||||||||||
| 93 | 754 | ▶ | (!$arguments{builder} ? (default => quote_sub(q{ [] }),) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | coerce => $coerce, | ||||||||||||||||||||||||||||
| 95 | %arguments, | ||||||||||||||||||||||||||||
| 96 | )); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | around( | ||||||||||||||||||||||||||||
| 99 | 4898 | ▶ | $name => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 100 | 4898 | my ($orig, $self) = (shift, shift); | |||||||||||||||||||||||||||
| 101 | 4898 | my $list = parse_list_arg(@_); | |||||||||||||||||||||||||||
| 102 | 4898 | ▶ | $self->$orig([ @{ $append ? $self->$orig : [] }, @$list ]) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 103 | if @$list; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4898 | my $return; | |||||||||||||||||||||||||||
| 106 | 4898 | ▶ | if ($may_throw) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 571 | ▶ | $return = ex2err($orig, $self) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||
| 109 | 4327 | $return = $self->$orig; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 4884 | ▶ | my $scalar_return = !@{$return} && $undef_if_empty ? undef : $return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @{$return;} and $undef_if_empty
MC/DC100%2/2
not @{$return;} and $undef_if_empty
not @{$return;}
$undef_if_empty
Truth table100%3/3
not(A) and B
A= not @{$return;}B= $undef_if_empty
| |||||||||||||||||||||||||||||
| 112 | 4884 | ▶ | return wantarray ? @{$return} : $scalar_return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 754 | ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =over | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =item L<SQL::Translator::Utils> | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =item L<SQL::Translator::Role::Error> | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =back | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 77 | 1; | |||||||||||||||||||||||||||