Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added dumper function to Mojo::Util
  • Loading branch information
kraih committed Oct 13, 2013
1 parent fe0f355 commit beff7b5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,4 +1,7 @@

4.47 2013-10-13
- Added dumper function to Mojo::Util.

4.46 2013-10-11
- Changed default name for generated applications from MyMojoliciousApp to
MyApp.
Expand Down
13 changes: 11 additions & 2 deletions lib/Mojo/Util.pm
Expand Up @@ -2,6 +2,7 @@ package Mojo::Util;
use Mojo::Base 'Exporter';

use Carp qw(carp croak);
use Data::Dumper ();
use Digest::MD5 qw(md5 md5_hex);
use Digest::SHA qw(hmac_sha1 sha1 sha1_hex);
use Encode 'find_encoding';
Expand Down Expand Up @@ -38,8 +39,8 @@ my %CACHE;

our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode deprecated encode get_line hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(decode deprecated dumper encode get_line hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_header spurt squish),
qw(steady_time trim unquote url_escape url_unescape xml_escape xor_encode)
);
Expand Down Expand Up @@ -95,6 +96,8 @@ sub deprecated {
$ENV{MOJO_FATAL_DEPRECATIONS} ? croak(@_) : carp(@_);
}
sub dumper { Data::Dumper->new([@_])->Indent(1)->Sortkeys(1)->Terse(1)->Dump }
sub encode { _encoding($_[0])->encode("$_[1]") }
sub get_line {
Expand Down Expand Up @@ -479,6 +482,12 @@ Decode bytes to characters and return C<undef> if decoding failed.
Warn about deprecated feature from perspective of caller. You can also set the
MOJO_FATAL_DEPRECATIONS environment variable to make them die instead.
=head2 dumper
my $str = dumper {some => 'data'};
Dump a Perl data structure with L<Data::Dumper>.
=head2 encode
my $bytes = encode 'UTF-8', $chars;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -43,7 +43,7 @@ has types => sub { Mojolicious::Types->new };
has validator => sub { Mojolicious::Validator->new };

our $CODENAME = 'Top Hat';
our $VERSION = '4.46';
our $VERSION = '4.47';

sub AUTOLOAD {
my $self = shift;
Expand Down
15 changes: 5 additions & 10 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
@@ -1,8 +1,8 @@
package Mojolicious::Plugin::DefaultHelpers;
use Mojo::Base 'Mojolicious::Plugin';

use Data::Dumper ();
use Mojo::ByteStream;
use Mojo::Util 'dumper';

sub register {
my ($self, $app) = @_;
Expand All @@ -29,10 +29,10 @@ sub register {
$app->helper(content => \&_content);
$app->helper(content_for => \&_content_for);
$app->helper(current_route => \&_current_route);
$app->helper(dumper => \&_dumper);
$app->helper(dumper => sub { shift; dumper(@_) });
$app->helper(include => \&_include);
$app->helper(ua => sub { shift->app->ua });
$app->helper(url_with => \&_url_with);
$app->helper(ua => sub { shift->app->ua });
$app->helper(url_with => \&_url_with);
}

sub _content {
Expand Down Expand Up @@ -61,11 +61,6 @@ sub _current_route {
return $endpoint->name eq shift;
}

sub _dumper {
my $self = shift;
return Data::Dumper->new([@_])->Indent(1)->Sortkeys(1)->Terse(1)->Dump;
}

sub _include {
my $self = shift;
my $template = @_ % 2 ? shift : undef;
Expand Down Expand Up @@ -170,7 +165,7 @@ Check or get name of current route.
%= dumper {some => 'data'}
Dump a Perl data structure with L<Data::Dumper>.
Alias for L<Mojo::Util/"dumper">.
=head2 extends
Expand Down
8 changes: 4 additions & 4 deletions lib/ojo.pm
Expand Up @@ -6,7 +6,7 @@ use Mojo::Collection 'c';
use Mojo::DOM;
use Mojo::JSON 'j';
use Mojo::UserAgent;
use Mojo::Util 'monkey_patch';
use Mojo::Util qw(dumper monkey_patch);

# Silent one-liners
$ENV{MOJO_LOG_LEVEL} ||= 'fatal';
Expand Down Expand Up @@ -36,9 +36,9 @@ sub import {
j => \&j,
o => sub { _request($UA->build_tx(OPTIONS => @_)) },
p => sub { _request($UA->build_tx(POST => @_)) },
r => sub { $UA->app->dumper(@_) },
t => sub { _request($UA->build_tx(PATCH => @_)) },
u => sub { _request($UA->build_tx(PUT => @_)) },
r => \&dumper,
t => sub { _request($UA->build_tx(PATCH => @_)) },
u => sub { _request($UA->build_tx(PUT => @_)) },
x => sub { Mojo::DOM->new(@_) };
}

Expand Down
11 changes: 7 additions & 4 deletions t/mojo/util.t
Expand Up @@ -10,10 +10,10 @@ use Mojo::DeprecationTest;

use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_sha1_sum html_unescape md5_bytes md5_sum),
qw(monkey_patch punycode_decode punycode_encode quote secure_compare),
qw(sha1_bytes sha1_sum slurp split_header spurt squish steady_time trim),
qw(unquote url_escape url_unescape xml_escape xor_encode);
qw(decode dumper encode get_line hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_header spurt squish),
qw(steady_time trim unquote url_escape url_unescape xml_escape xor_encode);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -413,4 +413,7 @@ is MojoMonkeyTest::yang(), 'yang', 'right result';
like $die, qr/foo is DEPRECATED at .*util\.t line \d+/, 'right exception';
}

# dumper
is dumper([1, 2]), "[\n 1,\n 2\n]\n", 'right result';

done_testing();

0 comments on commit beff7b5

Please sign in to comment.