Skip to content

Commit

Permalink
Fix composing classes with multiple Mojo::Base roles, closes #1170 #1171
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Dec 20, 2017
1 parent 722f46a commit a9d214d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
18 changes: 6 additions & 12 deletions lib/Mojo/Base.pm
Expand Up @@ -66,24 +66,18 @@ sub import {
# Base
if ($flags[0] eq '-base') { $flags[0] = $class }

# Strict
elsif ($flags[0] eq '-strict') { $flags[0] = undef }

# Role
elsif ($flags[0] eq '-role') {
if ($flags[0] eq '-role') {
Carp::croak 'Role::Tiny 2.000001+ is required for roles' unless ROLES;
Mojo::Util::monkey_patch($caller, 'has', sub { attr($caller, @_) });
eval "package $caller; use Role::Tiny; 1" or die $@;
}

# Module
elsif ($flags[0] && !$flags[0]->can('new')) {
require(Mojo::Util::class_to_path($flags[0]));
}

# "has" and possibly ISA
if ($flags[0]) {
# Module and not -strict
elsif ($flags[0] !~ /^-/) {
no strict 'refs';
push @{"${caller}::ISA"}, $flags[0] unless $flags[0] eq '-role';
require(Mojo::Util::class_to_path($flags[0])) unless $flags[0]->can('new');
push @{"${caller}::ISA"}, $flags[0];
Mojo::Util::monkey_patch($caller, 'has', sub { attr($caller, @_) });
}

Expand Down
7 changes: 7 additions & 0 deletions t/mojo/roles.t
Expand Up @@ -97,6 +97,13 @@ is $obj7->name, 'Joel', 'base attribute';
is $obj7->whisper, 'psst, joel', 'method from first role';
is $obj7->hello, 'HEY! JOEL!!!', 'method from second role';

# Multiple Mojo::Base roles
my $obj8 = Mojo::RoleTest->with_roles('+quiet', 'Mojo::RoleTest::Hello')
->new(name => 'Joel');
is $obj8->name, 'Joel', 'base attribute';
is $obj8->whisper, 'psst, joel', 'method from first role';
is $obj8->hello, 'hello mojo!', 'method from second role';

# Classes that are not subclasses of Mojo::Base
my $stream = Mojo::ByteStream->with_roles('Mojo::RoleTest::Hello')->new;
is $stream->hello, 'hello mojo!', 'right result';
Expand Down

0 comments on commit a9d214d

Please sign in to comment.