<commit_before>package Mojolicious::Routes;
use Mojo::Base -base;

use Mojo::Exception;
use Mojo::Loader;
use Mojo::URL;
use Mojo::Util 'camelize';
use Mojolicious::Routes::Match;
use Mojolicious::Routes::Pattern;
use Scalar::Util 'weaken';

has [qw/block inline parent partial namespace/];
has [qw/children conditions/] => sub { [] };
has controller_base_class => 'Mojolicious::Controller';
has [qw/dictionary shortcuts/] => sub { {} };
has hidden  => sub { [qw/new app attr has render req res stash tx/] };
has pattern => sub { Mojolicious::Routes::Pattern->new };

# "Yet thanks to my trusty safety sphere,
#  I sublibed with only tribial brain dablage."
sub AUTOLOAD {
    my $self = shift;

    # Method
    my ($package, $method) = our $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;

    # Shortcuts
    Carp::croak(qq/Can't locate object method "$method" via "$package"/)
      unless my $shortcut = $self->shortcuts->{$method};

    # Run
    return $self->$shortcut(@_);
}

sub DESTROY { }

sub new {
    my $self = shift->SUPER::new();

    # Parse
    $self->parse(@_);

    # Method condition
    $self->add_condition(
        method => sub {
            my ($r, $c, $captures, $methods) = @_;

            # Methods
            return unless $methods && ref $methods eq 'ARRAY';

            # Match
            my $m = lc $c->req->method;
            $m = 'get' if $m eq 'head';
            for my $method (@$methods) {
                return 1 if $method eq $m;
            }

            # Nothing
            return;
        }
    );

    # WebSocket condition
    $self->add_condition(
        websocket => sub {
            my ($r, $c, $captures) = @_;

            # WebSocket
            return 1 if $c->tx->is_websocket;

            # Not a WebSocket
            return;
        }
    );

    return $self;
}

sub add_child {
    my ($self, $route) = @_;

    # We are the parent
    $route->parent($self);
    weaken $route->{parent};

    # Shortcuts
    $route->shortcuts($self->shortcuts);

    # Add to tree
    push @{$self->children}, $route;

    return $self;
}

sub add_condition {
    my ($self, $name, $condition) = @_;

    # Add
    $self->dictionary->{$name} = $condition;

    return $self;
}

sub add_shortcut {
    my ($self, $name, $cb) = @_;
    $self->shortcuts->{$name} = $cb;
    return $self;
}

sub any {
    shift->_generate_route((ref $_[0] || '') eq 'ARRAY' ? shift : [], @_);
}

# "Hey. What kind of party is this? There's no booze and only one hooker."
sub auto_render {
    my ($self, $c) = @_;

    # Transaction
    my $tx = $c->tx;

    # Rendering
    my $success = eval {<commit_msg>small cleanup<commit_after>package Mojolicious::Routes;
use Mojo::Base -base;

use Mojo::Exception;
use Mojo::Loader;
use Mojo::URL;
use Mojo::Util 'camelize';
use Mojolicious::Routes::Match;
use Mojolicious::Routes::Pattern;
use Scalar::Util 'weaken';

has [qw/block inline parent partial namespace/];
has [qw/children conditions/] => sub { [] };
has controller_base_class => 'Mojolicious::Controller';
has [qw/dictionary shortcuts/] => sub { {} };
has hidden  => sub { [qw/new app attr has render req res stash tx/] };
has pattern => sub { Mojolicious::Routes::Pattern->new };

# "Yet thanks to my trusty safety sphere,
#  I sublibed with only tribial brain dablage."
sub AUTOLOAD {
    my $self = shift;

    # Method
    my ($package, $method) = our $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;

    # Shortcuts
    Carp::croak(qq/Can't locate object method "$method" via "$package"/)
      unless my $shortcut = $self->shortcuts->{$method};

    # Run
    return $self->$shortcut(@_);
}

sub DESTROY { }

sub new {
    my $self = shift->SUPER::new();

    # Parse
    $self->parse(@_);

    # Method condition
    $self->add_condition(
        method => sub {
            my ($r, $c, $captures, $methods) = @_;

            # Methods
            return unless $methods && ref $methods eq 'ARRAY';

            # Match
            my $method = lc $c->req->method;
            $method = 'get' if $method eq 'head';
            for my $m (@$methods) { return 1 if $m eq $method }

            # Nothing
            return;
        }
    );

    # WebSocket condition
    $self->add_condition(
        websocket => sub {
            my ($r, $c, $captures) = @_;

            # WebSocket
            return 1 if $c->tx->is_websocket;

            # Not a WebSocket
            return;
        }
    );

    return $self;
}

sub add_child {
    my ($self, $route) = @_;

    # We are the parent
    $route->parent($self);
    weaken $route->{parent};

    # Shortcuts
    $route->shortcuts($self->shortcuts);

    # Add to tree
    push @{$self->children}, $route;

    return $self;
}

sub add_condition {
    my ($self, $name, $cb) = @_;
    $self->dictionary->{$name} = $cb;
    return $self;
}

sub add_shortcut {
    my ($self, $name, $cb) = @_;
    $self->shortcuts->{$name} = $cb;
    return $self;
}

sub any {
    shift->_generate_route((ref $_[0] || '') eq 'ARRAY' ? shift : [], @_);
}

# "Hey. What kind of party is this? There's no booze and only one hooker."
sub auto_render {
    my ($self, $c) = @_;

    # Transaction
    my $tx = $c->tx;

    # Rendering
    my $success = eval {
{'fraction_non_alphanumeric': 0.16439931021268442, 'fraction_numerical': 0.0011496455259628281, 'mean_word_length': 2.3418693982074266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5799165', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1744, 'n_words': 484}