| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of MooX-ProtectedAttributes | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2013 by celogeek <me@celogeek.com>. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package MooX::ProtectedAttributes; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Create attribute only usable inside your package | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | our $VERSION = '0.03'; # VERSION | |||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 16 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 19 | 16 | my $target = caller; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | return | ||||||||||||||||||||||||||||
| 22 | 16 | ▶ | if $target->can('protected_has') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$target->can("protected_has") and $target->can("protected_with_deprecated_has")
MC/DC0%0/2
$target->can("protected_has") and $target->can("protected_with_deprecated_has")
$target->can("protected_has")
$target->can("protected_with_deprecated_has")
Truth table33%1/3
A and B
A= $target->can("protected_has")B= $target->can("protected_with_deprecated_has")
| |||||||||||||||||||||||||||||
| 23 | && $target->can('protected_with_deprecated_has'); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 16 | my $around = $target->can('around'); | |||||||||||||||||||||||||||
| 26 | 16 | my $has = $target->can('has'); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 24 | ▶ | my $ensure_call_in_target = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 24 | my ( $name, $deprecated_mode, $unless_method ) = @_; | |||||||||||||||||||||||||||
| 30 | 112 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 31 | 112 | my $orig = shift; | |||||||||||||||||||||||||||
| 32 | 112 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 112 | my @params = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 112 | ▶ | return $self->$orig(@params) if @params; #write is permitted | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 88 | ▶ | if ( defined $unless_method ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 24 | ▶ | return $self->$orig(@params) if $unless_method->(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 80 | my $caller = caller(2); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 80 | ▶ | return $self->$orig if $caller->DOES($target); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 24 | ▶ | if ($deprecated_mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 12 | carp | |||||||||||||||||||||||||||
| 46 | "DEPRECATED: You can't use the attribute <$name> outside the package <$target> or anyone that consume it!"; | ||||||||||||||||||||||||||||
| 47 | 12 | return $self->$orig; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||
| 50 | 12 | croak | |||||||||||||||||||||||||||
| 51 | "You can't use the attribute <$name> outside the package <$target> or anyone that consume it!"; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 16 | }; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 12 | ▶ | my $protected_has = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 57 | 12 | my ( $name, %attributes ) = @_; | |||||||||||||||||||||||||||
| 58 | 12 | my $unless_method = delete $attributes{'unless'}; | |||||||||||||||||||||||||||
| 59 | 12 | ▶ | croak "unless option should be a CODE REF" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $unless_method and ref $unless_method ne "CODE"
MC/DC0%0/2
defined $unless_method and ref $unless_method ne "CODE"
defined $unless_method
ref $unless_method ne "CODE"
Truth table66%2/3
A and B
A= defined $unless_methodB= ref $unless_method ne "CODE"
| |||||||||||||||||||||||||||||
| 60 | if defined $unless_method && ref $unless_method ne 'CODE'; | ||||||||||||||||||||||||||||
| 61 | 12 | $has->( $name, %attributes ); | |||||||||||||||||||||||||||
| 62 | 12 | $around->( | |||||||||||||||||||||||||||
| 63 | $name, $ensure_call_in_target->( $name, 0, $unless_method ) | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | 16 | }; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 12 | ▶ | my $protected_with_deprecated_has = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 68 | 12 | my ( $name, %attributes ) = @_; | |||||||||||||||||||||||||||
| 69 | 12 | my $unless_method = delete $attributes{'unless'}; | |||||||||||||||||||||||||||
| 70 | 12 | ▶ | croak "unless option should be a CODE REF" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $unless_method and ref $unless_method ne "CODE"
MC/DC0%0/2
defined $unless_method and ref $unless_method ne "CODE"
defined $unless_method
ref $unless_method ne "CODE"
Truth table66%2/3
A and B
A= defined $unless_methodB= ref $unless_method ne "CODE"
| |||||||||||||||||||||||||||||
| 71 | if defined $unless_method && ref $unless_method ne 'CODE'; | ||||||||||||||||||||||||||||
| 72 | 12 | $has->( $name, %attributes ); | |||||||||||||||||||||||||||
| 73 | 12 | $around->( | |||||||||||||||||||||||||||
| 74 | $name, $ensure_call_in_target->( $name, 1, $unless_method ) | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | 16 | }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 16 | ▶ | if ( my $info = $Role::Tiny::INFO{$target} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 4 | $info->{not_methods}{$protected_has} = $protected_has; | |||||||||||||||||||||||||||
| 80 | 4 | $info->{not_methods}{$protected_with_deprecated_has} | |||||||||||||||||||||||||||
| 81 | = $protected_with_deprecated_has; | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | { no strict 'refs'; *{"${target}::protected_has"} = $protected_has } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 16 | *{"${target}::protected_with_deprecated_has"} | |||||||||||||||||||||||||||
| 88 | = $protected_with_deprecated_has | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 16 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | 1; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||