| 1 | package MouseX::Types::Enum; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = "2.03"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp qw/confess/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Class::Inspector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | has id => (is => 'ro', isa => 'Str'); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 15 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 16 | 15 | my ($orig, $class, @params) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # This package is abstract class | ||||||||||||||||||||||||||||
| 19 | 15 | ▶ | confess __PACKAGE__ . " is abstract class." if $class eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 14 | return $class->$orig(@params); | |||||||||||||||||||||||||||
| 22 | 3 | }; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | my @EXPORT_MOUSE_METHODS = qw/ | |||||||||||||||||||||||||||
| 25 | extends with has | ||||||||||||||||||||||||||||
| 26 | before after around | ||||||||||||||||||||||||||||
| 27 | override super | ||||||||||||||||||||||||||||
| 28 | augment inner | ||||||||||||||||||||||||||||
| 29 | blessed confess | ||||||||||||||||||||||||||||
| 30 | /; | ||||||||||||||||||||||||||||
| 31 | 3 | my %_ENUM_METAS; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 21 | ▶ | sub _build_enum { | ||||||||||||||||||||||||||
|
Subroutine
_build_enum: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 34 | 21 | my ($child, %build_params) = @_; | |||||||||||||||||||||||||||
| 35 | 21 | my $parent = __PACKAGE__; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | #@type Mouse::Meta::Class | ||||||||||||||||||||||||||||
| 38 | 21 | my $meta = Mouse->init_meta(for_class => $child); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 15 | ▶ | $meta->add_around_method_modifier(BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 15 | my ($orig, $class, @params) = @_; | |||||||||||||||||||||||||||
| 42 | # disallow creating instance | ||||||||||||||||||||||||||||
| 43 | 15 | ▶ | if (caller(2) ne __PACKAGE__) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | confess sprintf("Cannot call $child->new outside of %s (called in %s)", __PACKAGE__, caller(2) . "") | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 14 | return $class->$orig(@params); | |||||||||||||||||||||||||||
| 47 | 21 | }); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # this subroutine should be called as `__PACKAGE__->build_enum`. | ||||||||||||||||||||||||||||
| 50 | 21 | ▶ | unless (caller() eq $child && !ref($child)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
caller() eq $child and not ref $child
MC/DC0%0/2
caller() eq $child and not ref $child
caller() eq $child
not ref $child
Truth table33%1/3
A and not(B)
A= caller() eq $childB= not ref $child
| |||||||||||||||||||||||||||||
| 51 | 0 | confess "Please call as `__PACKAGE__->_build_enum`."; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # check reserved subroutine names | ||||||||||||||||||||||||||||
| 55 | 21 | my @child_subs = @{Class::Inspector->functions($child)}; | |||||||||||||||||||||||||||
| 56 | 21 | my @parent_subs = @{Class::Inspector->functions($parent)}; | |||||||||||||||||||||||||||
| 57 | 21 | my %reserved_subs = map {$_ => undef} @parent_subs; | |||||||||||||||||||||||||||
| 58 | 21 | my %dup_allow_subs = map {$_ => undef} (@EXPORT_MOUSE_METHODS, 'meta', 'BUILDARGS'); | |||||||||||||||||||||||||||
| 59 | 21 | for my $sub_name (@child_subs) { | |||||||||||||||||||||||||||
| 60 | 214 | ▶ | if (exists $reserved_subs{$sub_name} && !exists $dup_allow_subs{$sub_name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $reserved_subs{$sub_name} and not exists $dup_allow_subs{$sub_name}
MC/DC100%2/2
exists $reserved_subs{$sub_name} and not exists $dup_allow_subs{$sub_name}
exists $reserved_subs{$sub_name}
not exists $dup_allow_subs{$sub_name}
Truth table100%3/3
A and not(B)
A= exists $reserved_subs{$sub_name}B= not exists $dup_allow_subs{$sub_name}
| |||||||||||||||||||||||||||||
| 61 | 12 | confess "`$sub_name` is reserved by " . __PACKAGE__ . "."; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | # Overwrite enums | ||||||||||||||||||||||||||||
| 69 | 9 | my @enum_subs = grep {$_ =~ /^[A-Z0-9_]+$/} @child_subs; | |||||||||||||||||||||||||||
| 70 | 9 | my %ignored_subs = map {$_ => undef} ('BUILDARGS', @{$build_params{ignore}}); | |||||||||||||||||||||||||||
| 71 | 9 | for my $sub_name (@enum_subs) { | |||||||||||||||||||||||||||
| 72 | 24 | ▶ | next if exists $ignored_subs{$sub_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 17 | my ($id, @args) = $child->$sub_name; | |||||||||||||||||||||||||||
| 74 | 17 | ▶ | confess "seems to be invalid argument." if scalar(@args) % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 16 | ▶ | confess "unique id is required for $child->$sub_name ." unless defined $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 15 | my %args = @args; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 15 | ▶ | if (exists $child->_enums->{$id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | confess "id `$id` is duplicate." | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 14 | my $instance = $child->new( | |||||||||||||||||||||||||||
| 82 | id => $id, | ||||||||||||||||||||||||||||
| 83 | %args | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | 14 | $child->_enums->{$id} = $instance; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 14 | ▶ | *{"${child}\::${sub_name}"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 88 | 167 | my $class = shift; | |||||||||||||||||||||||||||
| 89 | 167 | ▶ | if ($class && $class ne $child) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$class and $class ne $child
MC/DC50%1/2
$class and $class ne $child
$class
$class ne $child
Truth table66%2/3
A and B
A= $classB= $class ne $child
| |||||||||||||||||||||||||||||
| 90 | 1 | confess "`${child}::$sub_name` can only be called as static method of `$child`. Please call `${child}->${sub_name}`."; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 166 | return $instance; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 14 | } | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 6 | $child->meta->make_immutable; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | use overload | ||||||||||||||||||||||||||||
| 101 | # MouseX::Types::Enum can only be applied following operators | ||||||||||||||||||||||||||||
| 102 | 3 | ▶ | 'eq' => \&_equals, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 'ne' => \&_not_equals, | ||||||||||||||||||||||||||||
| 104 | '==' => \&_equals, | ||||||||||||||||||||||||||||
| 105 | '!=' => \&_not_equals, | ||||||||||||||||||||||||||||
| 106 | '""' => \&_to_string, | ||||||||||||||||||||||||||||
| 107 | 3 | ; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 5 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 5 | my ($class, $id) = @_; | |||||||||||||||||||||||||||
| 111 | 5 | ▶ | confess "this is class method." if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 4 | ▶ | return $class->_enums->{$id} // confess "$id is not found." | ||||||||||||||||||||||||||
|
Condition50%1/2
$class->_enums->{$id} // confess("$id is not found.")
MC/DC0%0/1
$class->_enums->{$id} // confess("$id is not found.")
$class->_enums->{$id}
Truth table50%1/2
A
A= $class->_enums->{$id}
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | sub all { | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 4 | my ($class) = shift; | |||||||||||||||||||||||||||
| 117 | 4 | ▶ | confess "this is class method." if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 3 | return $class->_enums; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 31 | ▶ | sub _to_string { | ||||||||||||||||||||||||||
|
Subroutine
_to_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||
| 123 | 31 | return sprintf("%s[id=%s]", ref($self), $self->id); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 66 | ▶ | sub _equals { | ||||||||||||||||||||||||||
|
Subroutine
_equals: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 127 | 66 | my ($first, $second) = @_; | |||||||||||||||||||||||||||
| 128 | 66 | ▶ | return (ref($first) eq ref($second)) && ($first->id eq $second->id); | ||||||||||||||||||||||||||
|
Condition100%3/3
ref $first eq ref $second && $first->id eq $second->id
MC/DC100%2/2
ref $first eq ref $second && $first->id eq $second->id
ref $first eq ref $second
$first->id eq $second->id
Truth table100%3/3
A && B
A= ref $first eq ref $secondB= $first->id eq $second->id
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 33 | ▶ | sub _not_equals { | ||||||||||||||||||||||||||
|
Subroutine
_not_equals: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | 33 | my ($first, $second) = @_; | |||||||||||||||||||||||||||
| 133 | 33 | return !_equals($first, $second); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 36 | ▶ | sub _enum_meta { | ||||||||||||||||||||||||||
|
Subroutine
_enum_meta: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 137 | 36 | my ($class) = @_; | |||||||||||||||||||||||||||
| 138 | 36 | ▶ | return $_ENUM_METAS{$class} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$_ENUM_METAS{$class} //= {}
MC/DC100%1/1
$_ENUM_METAS{$class} //= {}
$_ENUM_METAS{$class}
Truth table100%2/2
A
A= $_ENUM_METAS{$class}
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 36 | ▶ | sub _enums { | ||||||||||||||||||||||||||
|
Subroutine
_enums: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 142 | 36 | my ($class) = @_; | |||||||||||||||||||||||||||
| 143 | 36 | ▶ | return $class->_enum_meta->{enums} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$class->_enum_meta->{"enums"} //= {}
MC/DC100%1/1
$class->_enum_meta->{"enums"} //= {}
$class->_enum_meta->{"enums"}
Truth table100%2/2
A
A= $class->_enum_meta->{"enums"}
| |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub _overwrite_flg { | ||||||||||||||||||||||||||
|
Subroutine
_overwrite_flg: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 147 | 0 | my ($class) = @_; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | return $class->_enum_meta->{overwrite_flg} //= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$class->_enum_meta->{"overwrite_flg"} //= {}
MC/DC0%0/1
$class->_enum_meta->{"overwrite_flg"} //= {}
$class->_enum_meta->{"overwrite_flg"}
Truth table0%0/2
A
A= $class->_enum_meta->{"overwrite_flg"}
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 3 | 1; | |||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||