| 1 | package MooX::ChainedAttributes; | ||||||||||
| 2 | 1 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | our $VERSION = '0.08'; | |||||||||
| 5 | |||||||||||
| 6 | =encoding utf8 | ||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | MooX::ChainedAttributes - Make your attributes chainable. | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | package Foo; | ||||||||||
| 15 | use Moo; | ||||||||||
| 16 | use MooX::ChainedAttributes; | ||||||||||
| 17 | |||||||||||
| 18 | has name => ( | ||||||||||
| 19 | is => 'rw', | ||||||||||
| 20 | chained => 1, | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | has age => ( | ||||||||||
| 24 | is => 'rw', | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | chain('age'); | ||||||||||
| 28 | |||||||||||
| 29 | sub who { | ||||||||||
| 30 | my ($self) = @_; | ||||||||||
| 31 | print "My name is " . $self->name() . "!\n"; | ||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | my $foo = Foo->new(); | ||||||||||
| 35 | $foo->name('Fred')->who(); # My name is Fred! | ||||||||||
| 36 | |||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||
| 38 | |||||||||||
| 39 | This module exists for your method chaining enjoyment. It | ||||||||||
| 40 | was originally developed in order to support the porting of | ||||||||||
| 41 | L<MooseX::Attribute::Chained> using classes to L<Moo>. | ||||||||||
| 42 | |||||||||||
| 43 | In L<Moose> you would write: | ||||||||||
| 44 | |||||||||||
| 45 | package Bar; | ||||||||||
| 46 | use Moose; | ||||||||||
| 47 | use MooseX::Attribute::Chained; | ||||||||||
| 48 | has baz => ( is=>'rw', traits=>['Chained'] ); | ||||||||||
| 49 | |||||||||||
| 50 | To port the above to L<Moo> just change it to: | ||||||||||
| 51 | |||||||||||
| 52 | package Bar; | ||||||||||
| 53 | use Moo; | ||||||||||
| 54 | use MooX::ChainedAttributes; | ||||||||||
| 55 | has baz => ( is=>'rw', chained=>1 ); | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | 1 | ▶ | use Moo (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 1 | ▶ | use Moo::Role (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 1 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 62 | |||||||||||
| 63 | 1 | my $role = 'MooX::ChainedAttributes::Role::GenerateAccessor'; | |||||||||
| 64 | |||||||||||
| 65 | 2 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=3 SCAR=11.1
| |||||||||||
| 66 | 2 | my $class = shift; | |||||||||
| 67 | 2 | my $target = caller; | |||||||||
| 68 | |||||||||||
| 69 | 2 | ▶ | if (my $acc = Moo->_accessor_maker_for($target)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 2 | ▶ | Moo::Role->apply_roles_to_object($acc, $role) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | unless $acc->does($role); | ||||||||||
| 72 | } | ||||||||||
| 73 | else { | ||||||||||
| 74 | 0 | croak "MooX::ChainedAttributes can only be used in Moo classes."; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 2 | my $has = $target->can('has'); | |||||||||
| 78 | |||||||||||
| 79 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 2 | ▶ | *{"${target}::chain"} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 81 | 1 | my $attr = shift; | |||||||||
| 82 | 1 | $has->("+$attr", (chained => 1)); | |||||||||
| 83 | 1 | return; | |||||||||
| 84 | 2 | }; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 1 | 1; | |||||||||
| 88 | __END__ | ||||||||||