| 1 | package Data::Focus; | ||||||||||||||||||||||||||||
| 2 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use Data::Focus::Lens::Composite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 29 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 29 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 29 | our $VERSION = "0.03"; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 29 | our @EXPORT_OK = qw(focus); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 22483 | ▶ | sub focus { | ||||||||||||||||||||||||||
|
Subroutine
focus: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 22483 | my ($target, @lenses) = @_; | |||||||||||||||||||||||||||
| 15 | 22483 | return __PACKAGE__->new(target => $target, lens => \@lenses); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 22493 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 22493 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 20 | 22493 | ▶ | croak "target param is mandatory" if !exists($args{target}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 22493 | my $target = $args{target}; | |||||||||||||||||||||||||||
| 22 | my $lenses = exists($args{lens}) ? | ||||||||||||||||||||||||||||
| 23 | 22493 | ▶ | (ref($args{lens}) eq "ARRAY" ? $args{lens} : [$args{lens}]) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 24 | : []; | ||||||||||||||||||||||||||||
| 25 | 22493 | $_ = $class->coerce_to_lens($_) foreach @$lenses; | |||||||||||||||||||||||||||
| 26 | 22493 | my $self = bless { | |||||||||||||||||||||||||||
| 27 | target => $target, | ||||||||||||||||||||||||||||
| 28 | lenses => $lenses | ||||||||||||||||||||||||||||
| 29 | }, $class; | ||||||||||||||||||||||||||||
| 30 | 22493 | return $self; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 22584 | ▶ | sub coerce_to_lens { | ||||||||||||||||||||||||||
|
Subroutine
coerce_to_lens: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 22584 | my (undef, $maybe_lens) = @_; | |||||||||||||||||||||||||||
| 35 | 22584 | ▶ | if(Scalar::Util::blessed($maybe_lens) && $maybe_lens->isa("Data::Focus::Lens")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($maybe_lens) and $maybe_lens->isa("Data::Focus::Lens")
MC/DC50%1/2
builtin::blessed($maybe_lens) and $maybe_lens->isa("Data::Focus::Lens")
builtin::blessed($maybe_lens)
$maybe_lens->isa("Data::Focus::Lens")
Truth table66%2/3
A and B
A= builtin::blessed($maybe_lens)B= $maybe_lens->isa("Data::Focus::Lens")
| |||||||||||||||||||||||||||||
| 36 | 22538 | return $maybe_lens; | |||||||||||||||||||||||||||
| 37 | }else { | ||||||||||||||||||||||||||||
| 38 | 46 | require Data::Focus::Lens::Dynamic; | |||||||||||||||||||||||||||
| 39 | 46 | return Data::Focus::Lens::Dynamic->new($maybe_lens); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 6 | ▶ | sub into { | ||||||||||||||||||||||||||
|
Subroutine
into: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 6 | my ($self, @lenses) = @_; | |||||||||||||||||||||||||||
| 45 | 6 | unshift @lenses, @{$self->{lenses}}; | |||||||||||||||||||||||||||
| 46 | my $deeper = ref($self)->new( | ||||||||||||||||||||||||||||
| 47 | target => $self->{target}, | ||||||||||||||||||||||||||||
| 48 | 6 | lens => \@lenses, | |||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | 6 | return $deeper; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 22528 | ▶ | sub _apply_lenses_to_target { | ||||||||||||||||||||||||||
|
Subroutine
_apply_lenses_to_target: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 22528 | my ($self, $app_class, $updater, @additional_lenses) = @_; | |||||||||||||||||||||||||||
| 55 | 22528 | my @lenses = (@{$self->{lenses}}, map { $self->coerce_to_lens($_) } @additional_lenses); | |||||||||||||||||||||||||||
| 56 | 22528 | ▶ | if(@lenses == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | return $lenses[0]->apply_lens( | ||||||||||||||||||||||||||||
| 58 | $app_class, $app_class->create_part_mapper($updater), $self->{target} | ||||||||||||||||||||||||||||
| 59 | 22443 | ); | |||||||||||||||||||||||||||
| 60 | }else { | ||||||||||||||||||||||||||||
| 61 | return Data::Focus::Lens::Composite->apply_composite_lens( | ||||||||||||||||||||||||||||
| 62 | \@lenses, | ||||||||||||||||||||||||||||
| 63 | $app_class, | ||||||||||||||||||||||||||||
| 64 | $app_class->create_part_mapper($updater), | ||||||||||||||||||||||||||||
| 65 | $self->{target} | ||||||||||||||||||||||||||||
| 66 | 85 | ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 642 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 642 | my ($self, @lenses) = @_; | |||||||||||||||||||||||||||
| 72 | 642 | require Data::Focus::Applicative::Const::First; | |||||||||||||||||||||||||||
| 73 | 642 | my $ret = $self->_apply_lenses_to_target( | |||||||||||||||||||||||||||
| 74 | "Data::Focus::Applicative::Const::First", undef, @lenses | ||||||||||||||||||||||||||||
| 75 | )->get_const; | ||||||||||||||||||||||||||||
| 76 | 639 | ▶ | return defined($ret) ? $$ret : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1011 | ▶ | sub list { | ||||||||||||||||||||||||||
|
Subroutine
list: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 1011 | my ($self, @lenses) = @_; | |||||||||||||||||||||||||||
| 81 | 1011 | require Data::Focus::Applicative::Const::List; | |||||||||||||||||||||||||||
| 82 | 1011 | my $traversed_list = $self->_apply_lenses_to_target( | |||||||||||||||||||||||||||
| 83 | "Data::Focus::Applicative::Const::List", undef, @lenses | ||||||||||||||||||||||||||||
| 84 | )->get_const; | ||||||||||||||||||||||||||||
| 85 | 1011 | ▶ | return wantarray ? @$traversed_list : $traversed_list->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 20875 | ▶ | sub over { | ||||||||||||||||||||||||||
|
Subroutine
over: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 20875 | my $updater = pop; | |||||||||||||||||||||||||||
| 90 | 20875 | my ($self, @lenses) = @_; | |||||||||||||||||||||||||||
| 91 | 20875 | ▶ | croak "updater param must be a code-ref" if ref($updater) ne "CODE"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 20875 | require Data::Focus::Applicative::Identity; | |||||||||||||||||||||||||||
| 93 | 20875 | return $self->_apply_lenses_to_target( | |||||||||||||||||||||||||||
| 94 | "Data::Focus::Applicative::Identity", $updater, @lenses | ||||||||||||||||||||||||||||
| 95 | )->run_identity; | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 20852 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 20852 | my $datum = pop; | |||||||||||||||||||||||||||
| 100 | 20852 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 20852 | ▶ | return $self->over(@_, sub { $datum }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 29 | 1; | |||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||