Skip to content

Commit

Permalink
update Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 10, 2017
1 parent 492e6c4 commit 5e46ffa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
5 changes: 4 additions & 1 deletion Changes
@@ -1,5 +1,8 @@

7.40 2017-08-04
7.40 2017-08-10
- Added support for Role::Tiny extensions to all classes based on Mojo::Base.
(dotan)
- Added can_roles and with_roles methods to Mojo::Base. (dotan)

7.39 2017-08-03
- Removed experimental close_idle_connections method from
Expand Down
30 changes: 16 additions & 14 deletions lib/Mojo/Base.pm
Expand Up @@ -11,14 +11,14 @@ use Carp ();
# Only Perl 5.14+ requires it on demand
use IO::Handle ();

# Supported on Perl 5.22+
my $NAME
= eval { require Sub::Util; Sub::Util->can('set_subname') } || sub { $_[1] };

# Role support requires Role::Tiny 2.000001+
use constant ROLES =>
!!(eval { require Role::Tiny; Role::Tiny->VERSION('2.000001'); 1 });

# Supported on Perl 5.22+
my $NAME
= eval { require Sub::Util; Sub::Util->can('set_subname') } || sub { $_[1] };

# Protect subclasses using AUTOLOAD
sub DESTROY { }

Expand Down Expand Up @@ -108,8 +108,8 @@ sub tap {
}

sub with_roles {
return Role::Tiny->create_class_with_roles(@_) if (ROLES);
Carp::croak "Role::Tiny 2.000001+ is required for with_roles method";
return Role::Tiny->create_class_with_roles(@_) if ROLES;
Carp::croak 'Role::Tiny 2.000001+ is required for roles';
}

1;
Expand Down Expand Up @@ -223,10 +223,10 @@ means they return their invocant when they are called with an argument.
=head2 can_roles
my $bool = Mojo::Base->can_roles();
my $bool = Mojo::Base->can_roles;
True if L<Role::Tiny> 2.000001+ is installed, indicates that roles are supported in L<Mojo::Base>
derived classes.
True if L<Role::Tiny> 2.000001+ is installed and roles are supported in
L<Mojo::Base> derived classes.
=head2 new
Expand Down Expand Up @@ -258,12 +258,14 @@ spliced or tapped into) a chained set of object method calls.
=head2 with_roles
my $new_class = Class->with_roles('Foo::Role1', 'Bar::Role2');
my $object = $new_class->new();
my $new_class = SubClass->with_roles('Foo::Role1', 'Bar::Role2');
Create and return a new class that extends the given class with the list of
roles using L<Role::Tiny>.
Create and return a new class that extends the given class with the
list of roles composed in order, using L<Role::Tiny>'s method
C<create_class_with_roles>.
# Create a new class and instantiate it
my $new_class = SubClass->with_roles('Foo::Role1', 'Foo::Role2');
my $object = $new_class->new;
=head1 SEE ALSO
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -909,6 +909,8 @@ Dominik Jarmulowicz
Dominique Dumont
Dotan Dimet
Douglas Christopher Wilson
Eugen Konkov
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Guides/FAQ.pod
Expand Up @@ -35,7 +35,8 @@ L<Mojolicious::Guides::Contributing> that forbid dependencies, we do currently
discourage adding non-optional ones in favor of a faster and more painless
installation process. And we do in fact already use several optional CPAN
modules such as L<EV>, L<IO::Socket::Socks>, L<IO::Socket::SSL>,
L<Net::DNS::Native> and L<Plack> to provide advanced functionality if possible.
L<Net::DNS::Native>, L<Plack> and L<Role::Tiny> to provide advanced
functionality if possible.

=head2 Why reinvent wheels?

Expand Down
21 changes: 12 additions & 9 deletions t/mojo/roles.t → t/mojo/base_roles.t
Expand Up @@ -15,7 +15,7 @@ sub yell {'HEY!'}
requires 'name';

sub hello {
my ($self) = @_;
my $self = shift;
return $self->yell . ' ' . uc($self->name) . '!!!';
}

Expand All @@ -25,7 +25,7 @@ use Role::Tiny;
requires 'name';

sub whisper {
my ($self) = @_;
my $self = shift;
return 'psst, ' . lc($self->name);
}

Expand All @@ -41,19 +41,22 @@ sub hello {

package main;

# Plain class
my $obj = Mojo::RoleTest::Base->new(name => 'Ted');
is($obj->name, 'Ted', 'attr works');
is($obj->hello, 'hello Ted', 'class method');
is $obj->name, 'Ted', 'attribute';
is $obj->hello, 'hello Ted', 'method';

# Single role
my $obj2 = Mojo::RoleTest::Base->with_roles('Mojo::RoleTest::LOUD')->new;
is($obj2->hello, 'HEY! BOB!!!', 'method from role overrides base method');
is($obj2->yell, 'HEY!', 'new method from role');
is $obj2->hello, 'HEY! BOB!!!', 'role method';
is $obj2->yell, 'HEY!', 'another role method';

# Multiple roles
my $obj3 = Mojo::RoleTest::Base->with_roles('Mojo::RoleTest::quiet',
'Mojo::RoleTest::LOUD')->new(name => 'Joel');
is($obj3->name, 'Joel', 'attr from base class');
is($obj3->whisper, 'psst, joel', 'method from role1');
is($obj3->hello, 'HEY! JOEL!!!', 'method override from role2');
is $obj3->name, 'Joel', 'base attribute';
is $obj3->whisper, 'psst, joel', 'method from first role';
is $obj3->hello, 'HEY! JOEL!!!', 'method from second role';

done_testing();

0 comments on commit 5e46ffa

Please sign in to comment.