| 1 | package MooX::Thunking; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | our $VERSION = '0.08'; | |||||||||
| 4 | |||||||||||
| 5 | # this bit would be MooX::Utils but without initial _ on func name | ||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use Moo (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use Moo::Role (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | #use base qw(Exporter); | ||||||||||
| 12 | #our @EXPORT = qw(override_function); | ||||||||||
| 13 | 4 | ▶ | sub _override_function { | ||||||||
|
Subroutine
_override_function: called CC=3 SCAR=11.0
| |||||||||||
| 14 | 4 | my ($target, $name, $func) = @_; | |||||||||
| 15 | 4 | ▶ | my $orig = $target->can($name) or croak "Override '$target\::$name': not found"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 4 | ▶ | my $install_tracked = Moo::Role->is_role($target) ? \&Moo::Role::_install_tracked : \&Moo::_install_tracked; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 4 | ▶ | $install_tracked->($target, $name, sub { $func->($orig, @_) }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | } | ||||||||||
| 19 | # end MooX::Utils; | ||||||||||
| 20 | |||||||||||
| 21 | 4 | ▶ | use Types::TypeTiny -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 4 | ▶ | use Type::Utils -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 4 | ▶ | use Class::Method::Modifiers qw(install_modifier); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 4 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 4 | my $target = scalar caller; | |||||||||
| 26 | 4 | ▶ | _override_function($target, 'has', sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 27 | 4 | my ($orig, $namespec, %opts) = @_; | |||||||||
| 28 | 4 | ▶ | $orig->($namespec, %opts), return if $opts{is} ne 'thunked'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 4 | $opts{is} = 'rwp'; | |||||||||
| 30 | 4 | ▶ | $opts{isa} = union [ CodeLike, $opts{isa} ] if $opts{isa}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 4 | $orig->($namespec, %opts); # so we have method to modify | |||||||||
| 32 | 4 | ▶ | for my $name (ref $namespec ? @$namespec : $namespec) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 5 | my $resolved_name = "_${name}_resolved"; | |||||||||
| 34 | 5 | $orig->($resolved_name, is => 'rw'); # cache whether resolved | |||||||||
| 35 | 9 | ▶ | install_modifier $target, 'before', $name => sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 36 | 9 | my $self = shift; | |||||||||
| 37 | 9 | ▶ | return if @_; # attempt at setting, hand to auto | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 9 | ▶ | return if $self->$resolved_name; # already resolved | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 9 | $self->$resolved_name(1); | |||||||||
| 40 | 9 | local $@; | |||||||||
| 41 | 9 | ▶ | return if !eval { CodeLike->($self->{$name}); 1 }; # not a thunk | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 6 | my $setter = "_set_$name"; | |||||||||
| 43 | 6 | $self->$setter($self->{$name}->()); | |||||||||
| 44 | 5 | }; | |||||||||
| 45 | } | ||||||||||
| 46 | 4 | }); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | =head1 NAME | ||||||||||
| 50 | |||||||||||
| 51 | MooX::Thunking - Allow Moo attributes to be "thunked" | ||||||||||
| 52 | |||||||||||
| 53 | =head1 SYNOPSIS | ||||||||||
| 54 | |||||||||||
| 55 | package Thunking; | ||||||||||
| 56 | use Moo; | ||||||||||
| 57 | use MooX::Thunking; | ||||||||||
| 58 | use Types::TypeTiny -all; | ||||||||||
| 59 | use Types::Standard -all; | ||||||||||
| 60 | has children => ( | ||||||||||
| 61 | is => 'thunked', | ||||||||||
| 62 | isa => ArrayRef[InstanceOf['Thunking']], | ||||||||||
| 63 | required => 1, | ||||||||||
| 64 | ); | ||||||||||
| 65 | |||||||||||
| 66 | package main; | ||||||||||
| 67 | my $obj; | ||||||||||
| 68 | $obj = Thunking->new(children => sub { [$obj] }); | ||||||||||
| 69 | |||||||||||
| 70 | =head1 DESCRIPTION | ||||||||||
| 71 | |||||||||||
| 72 | This is a L<Moo> extension. It allows another value for the C<is> | ||||||||||
| 73 | parameter to L<Moo/has>: "thunked". If used, this will allow you to | ||||||||||
| 74 | transparently provide either a real value for the attribute, or a | ||||||||||
| 75 | L<Types::TypeTiny/CodeLike> that when called will return such a real | ||||||||||
| 76 | value. | ||||||||||
| 77 | |||||||||||
| 78 | Note that in earlier versions of this module (up to 0.06), any C<isa> | ||||||||||
| 79 | had to pass a C<CodeLike>. This is now taken care of by this module. It | ||||||||||
| 80 | will continue to do the right thing if no C<isa> is supplied. | ||||||||||
| 81 | |||||||||||
| 82 | =head1 AUTHOR | ||||||||||
| 83 | |||||||||||
| 84 | Ed J | ||||||||||
| 85 | |||||||||||
| 86 | =head1 LICENCE | ||||||||||
| 87 | |||||||||||
| 88 | The same terms as Perl itself. | ||||||||||
| 89 | |||||||||||
| 90 | =cut | ||||||||||
| 91 | |||||||||||
| 92 | 4 | 1; | |||||||||