| 1 | package OX::Meta::Role::HasRoutes; | ||||||||||||||||||||||||||||
| 2 | 43 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 43 | $OX::Meta::Role::HasRoutes::AUTHORITY = 'cpan:STEVAN'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 43 | $OX::Meta::Role::HasRoutes::VERSION = '0.14'; | |||||||||||||||||||||||||||
| 6 | 43 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 43 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 43 | ▶ | use Class::Load 'load_class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 43 | ▶ | use List::MoreUtils 'any'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 43 | ▶ | use OX::Meta::Conflict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 43 | ▶ | use OX::Meta::Mount::App; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 43 | ▶ | use OX::Meta::Mount::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 43 | ▶ | use OX::Meta::Route; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 43 | ▶ | use OX::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has routes => ( | ||||||||||||||||||||||||||||
| 19 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 20 | isa => 'ArrayRef[OX::Meta::Route|OX::Meta::Conflict]', | ||||||||||||||||||||||||||||
| 21 | 143 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 43 | handles => { | |||||||||||||||||||||||||||
| 23 | routes => 'elements', | ||||||||||||||||||||||||||||
| 24 | _add_route => 'push', | ||||||||||||||||||||||||||||
| 25 | _clear_routes => 'clear', | ||||||||||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | has mounts => ( | ||||||||||||||||||||||||||||
| 30 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 31 | isa => 'ArrayRef[OX::Meta::Mount|OX::Meta::Conflict]', | ||||||||||||||||||||||||||||
| 32 | 143 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 43 | handles => { | |||||||||||||||||||||||||||
| 34 | mounts => 'elements', | ||||||||||||||||||||||||||||
| 35 | has_mounts => 'count', | ||||||||||||||||||||||||||||
| 36 | _add_mount => 'push', | ||||||||||||||||||||||||||||
| 37 | _clear_mounts => 'clear', | ||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has mixed_conflicts => ( | ||||||||||||||||||||||||||||
| 42 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 43 | isa => 'ArrayRef[OX::Meta::Conflict]', | ||||||||||||||||||||||||||||
| 44 | 143 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 43 | handles => { | |||||||||||||||||||||||||||
| 46 | mixed_conflicts => 'elements', | ||||||||||||||||||||||||||||
| 47 | _add_mixed_conflict => 'push', | ||||||||||||||||||||||||||||
| 48 | _clear_mixed_conflicts => 'clear', | ||||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 173 | ▶ | sub add_route { | ||||||||||||||||||||||||||
|
Subroutine
add_route: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 173 | my $self = shift; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 173 | ▶ | my $route = OX::Meta::Route->new(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and builtin::blessed($_[0])
MC/DC0%0/2
@_ == 1 and builtin::blessed($_[0])
@_ == 1
builtin::blessed($_[0])
Truth table33%1/3
A and B
A= @_ == 1B= builtin::blessed($_[0])
| |||||||||||||||||||||||||||||
| 56 | unless @_ == 1 && blessed($_[0]); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 173 | my $path = $route->path; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 173 | for my $mount ($self->mounts) { | |||||||||||||||||||||||||||
| 61 | 9 | my $mount_path = $mount->path; | |||||||||||||||||||||||||||
| 62 | 9 | (my $prefix = $mount_path) =~ s{/$}{}; | |||||||||||||||||||||||||||
| 63 | 9 | ▶ | if ($path =~ m{^$prefix/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 2 | warn "The application mounted at $mount_path will shadow the " | |||||||||||||||||||||||||||
| 65 | . "route declared at $path"; | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 173 | $self->_add_route($route); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 29 | ▶ | sub has_route_for { | ||||||||||||||||||||||||||
|
Subroutine
has_route_for: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 29 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | 29 | my ($path) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 29 | my $canonical = OX::Util::canonicalize_path($path); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 29 | ▶ | return any { $_->canonical_path eq $canonical } $self->routes; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 40 | ▶ | sub add_mount { | ||||||||||||||||||||||||||
|
Subroutine
add_mount: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 40 | my $self = shift; | |||||||||||||||||||||||||||
| 83 | 40 | ▶ | my $opts = @_ > 1 ? { @_ } : $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 40 | my $mount; | |||||||||||||||||||||||||||
| 86 | 40 | ▶ | if (exists $opts->{class}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 9 | load_class($opts->{class}); | |||||||||||||||||||||||||||
| 88 | confess "Class $opts->{class} must implement a to_app method" | ||||||||||||||||||||||||||||
| 89 | 9 | ▶ | unless $opts->{class}->can('to_app'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 9 | $mount = OX::Meta::Mount::Class->new($opts); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 31 | $mount = OX::Meta::Mount::App->new($opts); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 40 | my $path = $mount->path; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 40 | (my $prefix = $path) =~ s{/$}{}; | |||||||||||||||||||||||||||
| 99 | 40 | for my $route ($self->routes) { | |||||||||||||||||||||||||||
| 100 | 32 | my $route_path = $route->path; | |||||||||||||||||||||||||||
| 101 | 32 | ▶ | if ($route_path =~ m{^$prefix/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 3 | warn "The application mounted at $path will shadow the " | |||||||||||||||||||||||||||
| 103 | . "route declared at $route_path"; | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 40 | $self->_add_mount($mount); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 41 | ▶ | sub has_mount_for { | ||||||||||||||||||||||||||
|
Subroutine
has_mount_for: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 41 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 41 | my ($path) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 41 | ▶ | return any { $_->path eq $path } $self->mounts; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =for Pod::Coverage | ||||||||||||||||||||||||||||
| 118 | add_route | ||||||||||||||||||||||||||||
| 119 | has_route_for | ||||||||||||||||||||||||||||
| 120 | add_mount | ||||||||||||||||||||||||||||
| 121 | has_mount_for | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 43 | 1; | |||||||||||||||||||||||||||