Skip to content

Commit

Permalink
tweaked embedding recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 14, 2012
1 parent 62ec604 commit a44739a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 57 deletions.
10 changes: 2 additions & 8 deletions lib/Mojo/Base.pm
Expand Up @@ -64,8 +64,7 @@ sub attr {
if ref $default && ref $default ne 'CODE';

# Create attributes
$attrs = [$attrs] unless ref $attrs eq 'ARRAY';
for my $attr (@$attrs) {
for my $attr (@{ref $attrs eq 'ARRAY' ? $attrs : [$attrs]}) {
Carp::croak(qq/Attribute "$attr" invalid/)
unless $attr =~ /^[a-zA-Z_]\w*$/;

Expand Down Expand Up @@ -95,12 +94,7 @@ sub attr {
# We compile custom attribute code for speed
no strict 'refs';
no warnings 'redefine';
*{"${class}::$attr"} = eval $code;

# This should never happen (hopefully)
Carp::croak("Mojo::Base compiler error: \n$code\n$@\n") if $@;

# Debug mode
*{"${class}::$attr"} = eval $code or Carp::croak("Mojo::Base error: $@");
warn "\nATTRIBUTE: $class->$attr\n$code\n\n" if $ENV{MOJO_BASE_DEBUG};
}
}
Expand Down
92 changes: 45 additions & 47 deletions lib/Mojo/Exception.pm
Expand Up @@ -25,8 +25,7 @@ sub to_string {

# Message
return $self->message unless $self->verbose;
my $string = '';
$string .= $self->message if $self->message;
my $string = $self->message ? $self->message : '';

# Before
$string .= $_->[0] . ': ' . $_->[1] . "\n" for @{$self->lines_before};
Expand All @@ -50,6 +49,47 @@ sub trace {
return $self;
}

# "You killed zombie Flanders!
# He was a zombie?"
sub _context {
my ($self, $line, $lines) = @_;

# Wrong file
return unless defined $lines->[0]->[$line - 1];

# Line
$self->line([$line]);
for my $l (@$lines) {
chomp(my $code = $l->[$line - 1]);
push @{$self->line}, $code;
}

# Before
for my $i (2 .. 6) {
last if ((my $previous = $line - $i) < 0);
if (defined($lines->[0]->[$previous])) {
unshift @{$self->lines_before}, [$previous + 1];
for my $l (@$lines) {
chomp(my $code = $l->[$previous]);
push @{$self->lines_before->[0]}, $code;
}
}
}

# After
for my $i (0 .. 4) {
next if ((my $next = $line + $i) < 0);
if (defined($lines->[0]->[$next])) {
push @{$self->lines_after}, [$next + 1];
for my $l (@$lines) {
next unless defined(my $code = $l->[$next]);
chomp $code;
push @{$self->lines_after->[-1]}, $code;
}
}
}
}

sub _detect {
my $self = shift;

Expand All @@ -71,7 +111,7 @@ sub _detect {
for my $frame (reverse @trace) {
next unless -r $frame->[0];
open my $handle, '<:utf8', $frame->[0];
$self->_parse_context($frame->[1], [[<$handle>]]);
$self->_context($frame->[1], [[<$handle>]]);
return $self;
}

Expand All @@ -98,57 +138,15 @@ sub _detect {
if ($self->message =~ /at\s+\Q$name\E\s+line\s+(\d+)/) { $line = $1 }
else {
for my $frame (@{$self->frames}) {
next unless $frame->[1] =~ /^\(eval\ \d+\)$/;
$line = $frame->[2];
$line = $frame->[1] =~ /^\(eval\ \d+\)$/ ? $frame->[2] : next;
last;
}
}
$self->_parse_context($line, \@lines) if $line;
$self->_context($line, \@lines) if $line;

return $self;
}

# "You killed zombie Flanders!
# He was a zombie?"
sub _parse_context {
my ($self, $line, $lines) = @_;

# Wrong file
return unless defined $lines->[0]->[$line - 1];

# Line
$self->line([$line]);
for my $l (@$lines) {
chomp(my $code = $l->[$line - 1]);
push @{$self->line}, $code;
}

# Before
for my $i (2 .. 6) {
last if ((my $previous = $line - $i) < 0);
if (defined($lines->[0]->[$previous])) {
unshift @{$self->lines_before}, [$previous + 1];
for my $l (@$lines) {
chomp(my $code = $l->[$previous]);
push @{$self->lines_before->[0]}, $code;
}
}
}

# After
for my $i (0 .. 4) {
next if ((my $next = $line + $i) < 0);
if (defined($lines->[0]->[$next])) {
push @{$self->lines_after}, [$next + 1];
for my $l (@$lines) {
next unless defined(my $code = $l->[$next]);
chomp $code;
push @{$self->lines_after->[-1]}, $code;
}
}
}
}

1;
__END__
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -237,7 +237,7 @@ other scripts, with this little mock server you can just embed them.
# Access fully initialized application
say $app->static->root;
say $app->config->{secret_identity};
say $app->home;
say $app->dumper(just => 'a helper test');

=head2 Web server embedding

Expand Down
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -10,7 +10,7 @@ plan skip_all => 'set TEST_POD to enable this test (developer only!)'
# DEPRECATED in Leaf Fluttering In Wind!
my @leaf = (
qw/controller_base_class default_static_class default_template_class drop/,
qw/dictionary max_redirects root unsubscribe/
qw/dictionary max_redirects root/
);

# "Marge, I'm going to miss you so much. And it's not just the sex.
Expand Down

0 comments on commit a44739a

Please sign in to comment.