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