Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use a different hash merge idiom
  • Loading branch information
kraih committed Jul 30, 2014
1 parent e3d53ac commit 157cea1
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
11 changes: 6 additions & 5 deletions lib/Mojo.pm
Expand Up @@ -23,7 +23,7 @@ has ua => sub {

sub build_tx { Mojo::Transaction::HTTP->new }

sub config { shift->_dict(config => @_) }
sub config { _dict(config => @_) }

sub handler { croak 'Method "handler" not implemented in subclass' }

Expand All @@ -40,19 +40,20 @@ sub new {
}

sub _dict {
my ($self, $name) = (shift, shift);
my ($name, $object) = (shift, shift);

# Hash
my $dict = $self->{$name} ||= {};
my $dict = $object->{$name} ||= {};
return $dict unless @_;

# Get
return $dict->{$_[0]} unless @_ > 1 || ref $_[0];

# Set
%$dict = (%$dict, %{ref $_[0] ? $_[0] : {@_}});
my $values = ref $_[0] ? $_[0] : {@_};
@$dict{keys %$values} = values %$values;

return $self;
return $object;
}

1;
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/DOM.pm
Expand Up @@ -60,7 +60,8 @@ sub attr {
return $attrs->{$_[0]} // '' unless @_ > 1 || ref $_[0];

# Set
%$attrs = (%$attrs, %{ref $_[0] ? $_[0] : {@_}});
my $values = ref $_[0] ? $_[0] : {@_};
@$attrs{keys %$values} = values %$values;

return $self;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -16,7 +16,7 @@ sub begin {
return sub { $self->_step($id, $offset // 1, $len, @_) };
}

sub data { shift->Mojo::_dict(data => @_) }
sub data { Mojo::_dict(data => @_) }

sub pass { $_[0]->begin->(@_) }

Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious.pm
Expand Up @@ -68,7 +68,8 @@ sub build_controller {
$stash->{'mojo.secrets'} //= $self->secrets;

# Build default controller
%$stash = (%$stash, %{$self->defaults});
my $defaults = $self->defaults;
@$stash{keys %$defaults} = values %$defaults;
my $c
= $self->controller_class->new(app => $self, stash => $stash, tx => $tx);
weaken $c->{app};
Expand All @@ -83,7 +84,7 @@ sub build_tx {
return $tx;
}

sub defaults { shift->_dict(defaults => @_) }
sub defaults { Mojo::_dict(defaults => @_) }

sub dispatch {
my ($self, $c) = @_;
Expand Down
10 changes: 6 additions & 4 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base -base;

# No imports, for security reasons!
use Carp ();
use Mojo;
use Mojo::ByteStream;
use Mojo::Exception;
use Mojo::Transaction::HTTP;
Expand Down Expand Up @@ -87,8 +88,8 @@ sub flash {
if @_ == 1 && !ref $_[0];

# Initialize new flash and merge values
my $flash = $session->{new_flash} ||= {};
%$flash = (%$flash, %{@_ > 1 ? {@_} : $_[0]});
my $values = ref $_[0] ? $_[0] : {@_};
@{$session->{new_flash} ||= {}}{keys %$values} = values %$values;

return $self;
}
Expand Down Expand Up @@ -266,7 +267,8 @@ sub session {
return $session->{$_[0]} unless @_ > 1 || ref $_[0];

# Set
%$session = (%$session, %{ref $_[0] ? $_[0] : {@_}});
my $values = ref $_[0] ? $_[0] : {@_};
@$session{keys %$values} = values %$values;

return $self;
}
Expand Down Expand Up @@ -311,7 +313,7 @@ sub signed_cookie {
return wantarray ? @results : $results[0];
}

sub stash { shift->Mojolicious::_dict(stash => @_) }
sub stash { Mojo::_dict(stash => @_) }

sub url_for {
my $self = shift;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Renderer.pm
Expand Up @@ -81,7 +81,7 @@ sub render {
delete @{$stash}{qw(layout extends)} if $ts;

# Merge stash and arguments
%$stash = (%$stash, %$args);
@$stash{keys %$args} = values %$args;

my $options = {
encoding => $self->encoding,
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -34,9 +34,8 @@ sub continue {

# Merge captures into stash
my $stash = $c->stash;
my $captures = $stash->{'mojo.captures'} //= {};
%$captures = (%$captures, %$field);
%$stash = (%$stash, %$field);
@{$stash->{'mojo.captures'} //= {}}{keys %$field} = values %$field;
@$stash{keys %$field} = values %$field;

my $continue;
my $last = !$stack->[++$current];
Expand Down

0 comments on commit 157cea1

Please sign in to comment.