| 1 | package Code::Style::Kit; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Data::OptList; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Import::Into; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use mro (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Package::Stash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Module::Runtime qw(use_module); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use constant DEBUG => $ENV{CODE_STYLE_KIT_DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '1.0.3'; # VERSION | |||||||||||||||||||||||||||
| 14 | # ABSTRACT: build composable bulk exporters | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 11 | my $class = shift; | |||||||||||||||||||||||||||
| 19 | 11 | my $caller = caller(); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 11 | my $self = $class->_new($caller,@_); | |||||||||||||||||||||||||||
| 22 | 11 | $self->_export_features; | |||||||||||||||||||||||||||
| 23 | 9 | return; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4 | ▶ | sub is_feature_requested { | ||||||||||||||||||||||||||
|
Subroutine
is_feature_requested: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 4 | my ($self, $feature) = @_; | |||||||||||||||||||||||||||
| 29 | 4 | return !! $self->{requested_features}{$feature}; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | sub also_export { | ||||||||||||||||||||||||||
|
Subroutine
also_export: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 2 | my ($self, $feature, $args) = @_; | |||||||||||||||||||||||||||
| 35 | 2 | ▶ | local $self->{requested_features}{$feature} = $args || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args || []
MC/DC0%0/1
$args || []
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||
| 36 | 2 | $self->_export_one_feature($feature, 1); | |||||||||||||||||||||||||||
| 37 | 2 | return; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 7 | ▶ | sub maybe_also_export { | ||||||||||||||||||||||||||
|
Subroutine
maybe_also_export: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 7 | my ($self, $feature, $args) = @_; | |||||||||||||||||||||||||||
| 43 | 7 | ▶ | local $self->{requested_features}{$feature} = $args || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args || []
MC/DC100%1/1
$args || []
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||
| 44 | 7 | $self->_export_one_feature($feature, 0); | |||||||||||||||||||||||||||
| 45 | 7 | return; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # private constructor, invoked from C<import> | ||||||||||||||||||||||||||||
| 49 | 11 | ▶ | sub _new { | ||||||||||||||||||||||||||
|
Subroutine
_new: called CC=6 SCAR=20.6
| |||||||||||||||||||||||||||||
| 50 | 11 | my ($class,$caller,@args) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # get all the parts in C<@ISA> order | ||||||||||||||||||||||||||||
| 53 | 11 | my $parent_stashes = $class->_get_parent_stashes; | |||||||||||||||||||||||||||
| 54 | # get the features they provide by default | ||||||||||||||||||||||||||||
| 55 | 11 | my %feature_set = map { $_ => [] } $class->_default_features($parent_stashes); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 11 | my $args = Data::OptList::mkopt(\@args,{ | |||||||||||||||||||||||||||
| 58 | moniker => $class, | ||||||||||||||||||||||||||||
| 59 | must_be => 'ARRAY', | ||||||||||||||||||||||||||||
| 60 | require_unique => 1, | ||||||||||||||||||||||||||||
| 61 | }); | ||||||||||||||||||||||||||||
| 62 | # interpret the import arguments | ||||||||||||||||||||||||||||
| 63 | 11 | for my $arg (@{$args}) { | |||||||||||||||||||||||||||
| 64 | 10 | my ($key, $value) = @{$arg}; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 10 | ▶ | if ($key =~ /^-(\w+)$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | croak "providing import arguments (@{$value}) when removing a feature ($1) makes no sense"; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 0 | print STDERR "$class - removing feature $1\n" if DEBUG; | |||||||||||||||||||||||||||
| 71 | 0 | delete $feature_set{$1}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | elsif ($key =~ /^\w+$/) { | ||||||||||||||||||||||||||||
| 74 | 10 | print STDERR "$class - adding feature $key\n" if DEBUG; | |||||||||||||||||||||||||||
| 75 | 10 | ▶ | $feature_set{$key} = $value // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$value // []
MC/DC100%1/1
$value // []
$value
Truth table100%2/2
A
A= $value
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 0 | croak "malformed feature <$key> when importing $class"; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # build the instance | ||||||||||||||||||||||||||||
| 83 | 11 | return bless { | |||||||||||||||||||||||||||
| 84 | caller => $caller, | ||||||||||||||||||||||||||||
| 85 | feature_list => [ $class->_sort_features(keys %feature_set) ], | ||||||||||||||||||||||||||||
| 86 | requested_features => \%feature_set, | ||||||||||||||||||||||||||||
| 87 | exported_features => {}, | ||||||||||||||||||||||||||||
| 88 | # we save this, so ->_export_one_feature doesn't have to scan | ||||||||||||||||||||||||||||
| 89 | # the parts again | ||||||||||||||||||||||||||||
| 90 | parent_stashes => $parent_stashes, | ||||||||||||||||||||||||||||
| 91 | }, $class; | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 11 | ▶ | sub _export_features { | ||||||||||||||||||||||||||
|
Subroutine
_export_features: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 95 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 11 | for my $feature (@{$self->{feature_list}}) { | |||||||||||||||||||||||||||
| 98 | 16 | $self->_export_one_feature($feature, 1); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # all the magic is from here to the end | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # the actual exporting | ||||||||||||||||||||||||||||
| 105 | 25 | ▶ | sub _export_one_feature { | ||||||||||||||||||||||||||
|
Subroutine
_export_one_feature: called CC=12 SCAR=25.0
| |||||||||||||||||||||||||||||
| 106 | 25 | my ($self, $feature, $croak_if_not_implemented) = @_; | |||||||||||||||||||||||||||
| 107 | 25 | my $class = ref($self); | |||||||||||||||||||||||||||
| 108 | 25 | ▶ | my @import_args = @{ $self->{requested_features}{$feature} || [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'requested_features'}{$feature} || []
MC/DC0%0/1
$self->{'requested_features'}{$feature} || []
$self->{'requested_features'}{$feature}
Truth table50%1/2
A
A= $self->{'requested_features'}{$feature}
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 25 | print STDERR "$class - exporting $feature to $self->{caller} with arguments (@import_args)\n" | |||||||||||||||||||||||||||
| 111 | if DEBUG; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # do nothing if we've exported it already | ||||||||||||||||||||||||||||
| 114 | 25 | ▶ | return if $self->{exported_features}{$feature}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 25 | my $list_method = "feature_${feature}_export_list"; | |||||||||||||||||||||||||||
| 117 | 25 | my $direct_method = "feature_${feature}_export"; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 25 | my $arguments_method = "feature_${feature}_takes_arguments"; | |||||||||||||||||||||||||||
| 120 | 25 | ▶ | my $takes_arguments = $self->can($arguments_method) && $self->$arguments_method; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->can($arguments_method) && $self->$arguments_method
MC/DC50%1/2
$self->can($arguments_method) && $self->$arguments_method
$self->can($arguments_method)
$self->$arguments_method
Truth table66%2/3
A && B
A= $self->can($arguments_method)B= $self->$arguments_method
| |||||||||||||||||||||||||||||
| 121 | 25 | ▶ | if (@import_args && !$takes_arguments) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@import_args and not $takes_arguments
MC/DC100%2/2
@import_args and not $takes_arguments
@import_args
not $takes_arguments
Truth table100%3/3
A and not(B)
A= @import_argsB= not $takes_arguments
| |||||||||||||||||||||||||||||
| 122 | 1 | croak "feature $feature does not take arguments, but (@import_args) were provided"; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 24 | my $provided = 0; | |||||||||||||||||||||||||||
| 126 | # loop over the parts in @ISA order | ||||||||||||||||||||||||||||
| 127 | 24 | for my $parent_stash (@{$self->{parent_stashes}}) { | |||||||||||||||||||||||||||
| 128 | 128 | my $parent_class = $parent_stash->name; | |||||||||||||||||||||||||||
| 129 | 128 | my $method_ref; | |||||||||||||||||||||||||||
| 130 | # does this part provide a *_export_list sub? | ||||||||||||||||||||||||||||
| 131 | 128 | ▶ | if ($method_ref = $parent_stash->get_symbol("&$list_method")) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | 1 | print STDERR " calling ${parent_class}->$list_method\n" | |||||||||||||||||||||||||||
| 133 | if DEBUG; | ||||||||||||||||||||||||||||
| 134 | # import all the packages that the sub returns | ||||||||||||||||||||||||||||
| 135 | 1 | for my $module ($self->$method_ref) { | |||||||||||||||||||||||||||
| 136 | 1 | use_module($module)->import::into($self->{caller}, @import_args); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 1 | $provided = 1; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | # does this part provide a *_export sub? | ||||||||||||||||||||||||||||
| 141 | elsif ($method_ref = $parent_stash->get_symbol("&$direct_method")) { | ||||||||||||||||||||||||||||
| 142 | 20 | print STDERR " calling ${parent_class}->$direct_method\n" | |||||||||||||||||||||||||||
| 143 | if DEBUG; | ||||||||||||||||||||||||||||
| 144 | # call it and let it do whatever it needs to | ||||||||||||||||||||||||||||
| 145 | 20 | $self->$method_ref($self->{caller},@import_args); | |||||||||||||||||||||||||||
| 146 | 19 | $provided = 1; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # did we find the feature? | ||||||||||||||||||||||||||||
| 151 | 23 | ▶ | if ($provided) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 152 | # mark it as exported | ||||||||||||||||||||||||||||
| 153 | 20 | $self->{exported_features}{$feature} = 1; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | elsif ($croak_if_not_implemented) { | ||||||||||||||||||||||||||||
| 156 | # croak if asked to | ||||||||||||||||||||||||||||
| 157 | 0 | croak "feature <$feature> is not implemented by $class"; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 23 | return; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # use the *_order subs | ||||||||||||||||||||||||||||
| 164 | 11 | ▶ | sub _sort_features { | ||||||||||||||||||||||||||
|
Subroutine
_sort_features: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 165 | 11 | my ($class, @features) = @_; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my %feature_sort_key = map { | ||||||||||||||||||||||||||||
| 168 | 11 | my $method = "feature_${_}_order"; | |||||||||||||||||||||||||||
| 169 | 18 | ▶ | $_ => ( $class->can($method) ? $class->$method : 100 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | } @features; | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | @features = sort { | ||||||||||||||||||||||||||||
| 173 | 11 | $feature_sort_key{$a} <=> $feature_sort_key{$b} | |||||||||||||||||||||||||||
| 174 | } @features; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 11 | print "$class - sorted features: (@features)\n" if DEBUG; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 11 | return @features; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # use the *_default subs | ||||||||||||||||||||||||||||
| 182 | 11 | ▶ | sub _default_features { | ||||||||||||||||||||||||||
|
Subroutine
_default_features: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 183 | 11 | my ($class, $parent_stashes) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 11 | my @features; | |||||||||||||||||||||||||||
| 186 | # loop over the parts in @ISA order | ||||||||||||||||||||||||||||
| 187 | 11 | for my $parent_stash (@{$parent_stashes}) { | |||||||||||||||||||||||||||
| 188 | 59 | my @subs = $parent_stash->list_all_symbols('CODE'); | |||||||||||||||||||||||||||
| 189 | 59 | for my $sub (@subs) { | |||||||||||||||||||||||||||
| 190 | # we only care about sub names of this form | ||||||||||||||||||||||||||||
| 191 | 286 | ▶ | next unless $sub =~ /^feature_(\w+)_default$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 32 | my $feature = $1; | |||||||||||||||||||||||||||
| 194 | 32 | my $is_default = $class->$sub; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 32 | if (DEBUG) { | |||||||||||||||||||||||||||
| 197 | my $parent_class = $parent_stash->name; | ||||||||||||||||||||||||||||
| 198 | print STDERR "$class - $parent_class provides $feature, by default ", | ||||||||||||||||||||||||||||
| 199 | ($is_default ? 'enabled' : 'disabled' ), | ||||||||||||||||||||||||||||
| 200 | "\n"; | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 32 | ▶ | push @features, $feature if $is_default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 11 | return @features; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 11 | ▶ | sub _get_parent_stashes { | ||||||||||||||||||||||||||
|
Subroutine
_get_parent_stashes: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 211 | 11 | my ($class) = @_; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 11 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 214 | 11 | return [ map { Package::Stash->new($_) } @{ mro::get_linear_isa($class) } ]; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 2 | 1; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | __END__ | ||||||||||||||||||||||||||||