line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooX::ChainedAttributes; |
2
|
1
|
|
|
1
|
|
200858
|
use 5.008001; |
|
1
|
|
|
|
|
3
|
|
3
|
1
|
|
|
1
|
|
5
|
use strictures 2; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
29
|
|
4
|
|
|
|
|
|
|
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 using classes to L. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
In L 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 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
|
|
|
1
|
|
163
|
use Moo (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
12
|
|
60
|
1
|
|
|
1
|
|
488
|
use Moo::Role (); |
|
1
|
|
|
|
|
10391
|
|
|
1
|
|
|
|
|
23
|
|
61
|
1
|
|
|
1
|
|
5
|
use Carp qw( croak ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
106
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $role = 'MooX::ChainedAttributes::Role::GenerateAccessor'; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub import { |
66
|
2
|
|
|
2
|
|
528
|
my $class = shift; |
67
|
2
|
|
|
|
|
5
|
my $target = caller; |
68
|
|
|
|
|
|
|
|
69
|
2
|
50
|
|
|
|
13
|
if (my $acc = Moo->_accessor_maker_for($target)) { |
70
|
2
|
50
|
|
|
|
17747
|
Moo::Role->apply_roles_to_object($acc, $role) |
71
|
|
|
|
|
|
|
unless $acc->does($role); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
else { |
74
|
0
|
|
|
|
|
0
|
croak "MooX::ChainedAttributes can only be used in Moo classes."; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
|
|
3073
|
my $has = $target->can('has'); |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
1
|
|
7
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
85
|
|
80
|
2
|
|
|
|
|
2079
|
*{"${target}::chain"} = sub { |
81
|
1
|
|
|
1
|
|
185
|
my $attr = shift; |
82
|
1
|
|
|
|
|
5
|
$has->("+$attr", (chained => 1)); |
83
|
1
|
|
|
|
|
493
|
return; |
84
|
2
|
|
|
|
|
9
|
}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
__END__ |