NAME

Mojo::Base::Role::PromiseClass - Add promise_class attribute to Mojo class

VERSION

version 0.007

SYNOPSIS

  package MyRole;

  use Mojo::Base -role;
  with 'Mojo::Base::Role::PromiseClass';

  sub give_me_a_promise {
     my $p = $_[0]->promise_class->new;
     #
     # ... do stuff to $p
     #
     return $p;
  }

Elsewhere

  # mix MyRole in to something
  $object = MyThing->new(...)->with_roles('MyRole');

  # get promise_class, add features
  $object->promise_class;            # -> Mojo::Promise
  $object->promise_roles('+Repeat'); # -> $object
  $object->promise_class;            # -> Mojo::Promise__WITH__...Repeat

  # use them
  $object->give_me_a_promise->repeat(sub {...} );

DESCRIPTION

Mojo::Base::Role::PromiseClass is a role that adds a promise_class attribute to a given class.

This role only provides methods to access and manipulate the promise_class; it does not, by itself, have any provision for the class being actually used anywhere or for any particular purpose.

ATTRIBUTES

Mojo::UserAgent::Role::PromiseClass implements the following attributes.

promise_class

  $pclass = $object->promise_class;
  $object = $object->promise_class('Mojo::Promise');

Get or set the $object's preferred promise class.

METHODS

Mojo::UserAgent::Role::PromiseClass supplies the following methods:

promise_roles

  $object->promise_roles(@roles);

This is a shortcut to add the specified @roles to the promise class, returning the original object for method chains, equivalent to

  $object->promise_class($object->promise_class->with_roles(@roles));

For roles following the naming scheme Mojo::Promise::Role::RoleName you can use the shorthand +RoleName.

Note that using this method is slightly safer than setting "promise_class" directly in that if the object's existing promise_class is derived from Mojo::Promise (which it will be by default) then you won't be changing that, which is typically what you want.

SEE ALSO

Mojo::Promise, Mojolicious, Mojolicious::Guides, https://mojolicious.org.

AUTHOR

Roger Crew <wrog@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2019 by Roger Crew.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)