| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of MooseX-RelatedClasses | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2017, 2015, 2014, 2013, 2012 by Chris Weyl. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | package MooseX::RelatedClasses; | ||||||||||||||||||||||||||||
| 11 | 6 | our $AUTHORITY = 'cpan:RSRCHBOY'; | |||||||||||||||||||||||||||
| 12 | # git description: 0.011-2-g61ae6c8 | ||||||||||||||||||||||||||||
| 13 | 6 | $MooseX::RelatedClasses::VERSION = '0.012'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # ABSTRACT: Parameterized role for related class attributes | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use MooseX::Role::Parameterized; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 8 | ▶ | use autobox::Core; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | use autobox::Camelize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | use MooseX::AttributeShortcuts 0.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 8 | ▶ | use MooseX::Types::Common::String ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 8 | ▶ | use MooseX::Types::LoadableClass ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 8 | ▶ | use MooseX::Types::Perl ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 8 | ▶ | use MooseX::Types::Moose ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 8 | ▶ | use MooseX::Util 'with_traits', 'find_meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 8 | ▶ | use Module::Find 'findallmod'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 8 | ▶ | use Class::Load 'load_class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 8 | ▶ | use String::RewritePrefix; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 8 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | package MooseX::RelatedClasses::Exports; | ||||||||||||||||||||||||||||
| 38 | 6 | our $AUTHORITY = 'cpan:RSRCHBOY'; | |||||||||||||||||||||||||||
| 39 | # git description: 0.011-2-g61ae6c8 | ||||||||||||||||||||||||||||
| 40 | 6 | $MooseX::RelatedClasses::Exports::VERSION = '0.012'; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # This is a little awkward, but it resolves the unpleasantness of having | ||||||||||||||||||||||||||||
| 43 | # these functions become part of the role (!!!) | ||||||||||||||||||||||||||||
| 44 | # | ||||||||||||||||||||||||||||
| 45 | # Here we simply create a "dummy" package using Moose exporter, then pull | ||||||||||||||||||||||||||||
| 46 | # its imports in via an "also" in the main package. This resolves things | ||||||||||||||||||||||||||||
| 47 | # nicely without having to do any meta-tinkering. | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | # Sadly. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 8 | ▶ | use MooseX::Util 'find_meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 8 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 6 | Moose::Exporter->setup_import_methods( | |||||||||||||||||||||||||||
| 58 | with_meta => [ qw{ | ||||||||||||||||||||||||||||
| 59 | related_class | ||||||||||||||||||||||||||||
| 60 | related_classes | ||||||||||||||||||||||||||||
| 61 | related_namespace | ||||||||||||||||||||||||||||
| 62 | } ], | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | sub related_class { goto \&related_classes } | ||||||||||||||||||||||||||
|
Subroutine
related_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | sub related_classes { | ||||||||||||||||||||||||||
|
Subroutine
related_classes: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 68 | 6 | my $meta = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | ▶ | if (@_ % 2 == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 4 | ▶ | unshift @_, ref $_[0] ? 'names' : 'name'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | find_meta('MooseX::RelatedClasses')->apply($meta, @_); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub related_namespace { | ||||||||||||||||||||||||||
|
Subroutine
related_namespace: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | 0 | my ($meta, $namespace) = (shift, shift); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my %args = ( | |||||||||||||||||||||||||||
| 81 | all_in_namespace => 1, | ||||||||||||||||||||||||||||
| 82 | namespace => $namespace, | ||||||||||||||||||||||||||||
| 83 | name => $namespace, | ||||||||||||||||||||||||||||
| 84 | @_, | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ### %args | ||||||||||||||||||||||||||||
| 88 | 0 | find_meta('MooseX::RelatedClasses')->apply($meta, %args); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 6 | Moose::Exporter->setup_import_methods( | |||||||||||||||||||||||||||
| 93 | 6 | also => 'MooseX::RelatedClasses::Exports', | |||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 6 | parameter name => ( | |||||||||||||||||||||||||||
| 98 | traits => [Shortcuts], | ||||||||||||||||||||||||||||
| 99 | is => 'ro', | ||||||||||||||||||||||||||||
| 100 | isa => PackageName, | ||||||||||||||||||||||||||||
| 101 | predicate => 1, | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | parameter names => ( | ||||||||||||||||||||||||||||
| 105 | traits => [Shortcuts], | ||||||||||||||||||||||||||||
| 106 | is => 'rwp', | ||||||||||||||||||||||||||||
| 107 | predicate => 1, | ||||||||||||||||||||||||||||
| 108 | lazy => 1, | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | isa => HashRef[Identifier], | ||||||||||||||||||||||||||||
| 111 | 18 | ▶ | constraint => sub { do { is_PackageName($_) or die 'keys must be PackageName' } for $_->keys; 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | coerce => [ | ||||||||||||||||||||||||||||
| 113 | 3 | ▶ | ArrayRef => sub { +{ map { $_ => $_->decamelize } @$_ } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 7 | ▶ | PackageName()->name() => sub { +{ $_ => $_->decamelize } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | ], | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 7 | ▶ | default => sub { confess 'name parameter required!' unless $_[0]->has_name; $_[0]->name }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 6 | ); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | parameter namespace => ( | |||||||||||||||||||||||||||
| 121 | traits => [Shortcuts], | ||||||||||||||||||||||||||||
| 122 | is => 'rwp', | ||||||||||||||||||||||||||||
| 123 | isa => Maybe[PackageName], | ||||||||||||||||||||||||||||
| 124 | predicate => 1, | ||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 6 | parameter all_in_namespace => (isa => Bool, default => 0); | |||||||||||||||||||||||||||
| 128 | 6 | parameter load_all => (isa => Bool, default => 0); | |||||||||||||||||||||||||||
| 129 | 6 | parameter private => (isa => Bool, default => 0); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # TODO use rewrite prefix to look for traits in namespace | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 14 | ▶ | role { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.1
| |||||||||||||||||||||||||||||
| 134 | 14 | my ($p, %opts) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 14 | ▶ | confess 'Cannot specify both the "name" and "names" parameters!' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$p->has_name and $p->has_names
MC/DC0%0/2
$p->has_name and $p->has_names
$p->has_name
$p->has_names
Truth table66%2/3
A and B
A= $p->has_nameB= $p->has_names
| |||||||||||||||||||||||||||||
| 137 | if $p->has_name && $p->has_names; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # check namespace | ||||||||||||||||||||||||||||
| 140 | 14 | ▶ | if (not $p->has_namespace) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | die 'Either a namespace or a consuming metaclass must be supplied!' | ||||||||||||||||||||||||||||
| 143 | 7 | ▶ | unless $opts{consumer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 7 | $p->_set_namespace($opts{consumer}->name); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 14 | ▶ | if ($p->all_in_namespace) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | confess 'Cannot use an empty namespace and all_in_namespace!' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | unless $p->has_namespace; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | my $ns = $p->namespace; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | ### finding for namespace: $ns | ||||||||||||||||||||||||||||
| 156 | my %mod = | ||||||||||||||||||||||||||||
| 157 | 2 | map { s/^${ns}:://; $_ => $_->decamelize } | |||||||||||||||||||||||||||
| 158 | 1 | ▶ | map { load_class($_) if $p->load_all; $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | Module::Find::findallmod $ns | ||||||||||||||||||||||||||||
| 160 | ; | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | ### %mod | ||||||||||||||||||||||||||||
| 163 | 1 | $p->_set_names(\%mod); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 14 | for my $name ($p->names->keys->flatten) { | |||||||||||||||||||||||||||
| 167 | 17 | my $identifier = $p->names->{$name}; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 17 | ▶ | my $full_name | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | = $p->namespace | ||||||||||||||||||||||||||||
| 171 | ? $p->namespace . '::' . $name | ||||||||||||||||||||||||||||
| 172 | : $name | ||||||||||||||||||||||||||||
| 173 | ; | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 17 | ▶ | my $pvt = $p->private ? '_' : q{}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # SomeThing::More -> some_thing__more | ||||||||||||||||||||||||||||
| 178 | 17 | my $local_name = "${identifier}_class"; | |||||||||||||||||||||||||||
| 179 | 17 | my $original_local_name = "original_$local_name"; | |||||||||||||||||||||||||||
| 180 | 17 | my $original_reader = "$pvt$original_local_name"; | |||||||||||||||||||||||||||
| 181 | 17 | my $traitsfor_local_name = $local_name . '_traits'; | |||||||||||||||||||||||||||
| 182 | 17 | my $traitsfor_reader = "$pvt$traitsfor_local_name"; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | ### $full_name | ||||||||||||||||||||||||||||
| 185 | has "$pvt$original_local_name" => ( | ||||||||||||||||||||||||||||
| 186 | traits => [Shortcuts], | ||||||||||||||||||||||||||||
| 187 | is => 'lazy', | ||||||||||||||||||||||||||||
| 188 | isa => LoadableClass, | ||||||||||||||||||||||||||||
| 189 | 76 | ▶ | constraint => sub { $_->isa($full_name) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 190 | coerce => 1, | ||||||||||||||||||||||||||||
| 191 | init_arg => "$pvt$local_name", | ||||||||||||||||||||||||||||
| 192 | 2 | ▶ | builder => sub { $full_name }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_original_one_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | 17 | ); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | has "$pvt$local_name" => ( | ||||||||||||||||||||||||||||
| 196 | traits => [Shortcuts], | ||||||||||||||||||||||||||||
| 197 | is => 'lazy', | ||||||||||||||||||||||||||||
| 198 | isa => LoadableClass, | ||||||||||||||||||||||||||||
| 199 | 60 | ▶ | constraint => sub { $_->isa($full_name) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 200 | coerce => 1, | ||||||||||||||||||||||||||||
| 201 | init_arg => undef, | ||||||||||||||||||||||||||||
| 202 | 30 | ▶ | builder => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_sub__one_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | 30 | my $self = shift @_; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 30 | return with_traits( $self->$original_reader() => | |||||||||||||||||||||||||||
| 206 | $self->$traitsfor_reader()->flatten, | ||||||||||||||||||||||||||||
| 207 | ); | ||||||||||||||||||||||||||||
| 208 | }, | ||||||||||||||||||||||||||||
| 209 | 17 | ); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # XXX do the same original/local init_arg swizzle here too? | ||||||||||||||||||||||||||||
| 212 | has "$pvt$traitsfor_local_name" => ( | ||||||||||||||||||||||||||||
| 213 | traits => [Shortcuts, 'Array'], | ||||||||||||||||||||||||||||
| 214 | is => 'lazy', | ||||||||||||||||||||||||||||
| 215 | isa => ArrayRef[LoadableRole], | ||||||||||||||||||||||||||||
| 216 | 10 | ▶ | builder => sub { [ ] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 217 | 17 | handles => { | |||||||||||||||||||||||||||
| 218 | "${pvt}has_$traitsfor_local_name" => 'count', | ||||||||||||||||||||||||||||
| 219 | }, | ||||||||||||||||||||||||||||
| 220 | ); | ||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 14 | return; | |||||||||||||||||||||||||||
| 224 | 6 | }; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 6 | !!42; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | __END__ | ||||||||||||||||||||||||||||