line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of MooseX-Attribute-Chained |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2017 by Tom Hukins. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package MooseX::Attribute::Chained; |
10
|
|
|
|
|
|
|
$MooseX::Attribute::Chained::VERSION = '1.0.3'; |
11
|
|
|
|
|
|
|
# ABSTRACT: Attribute that returns the instance to allow for chaining |
12
|
2
|
|
|
2
|
|
1062
|
use Moose::Util; |
|
2
|
|
|
|
|
18
|
|
|
2
|
|
|
|
|
24
|
|
13
|
|
|
|
|
|
|
Moose::Util::meta_attribute_alias( |
14
|
|
|
|
|
|
|
Chained => 'MooseX::Traits::Attribute::Chained' ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Loading this class now prevents it from loading later and emitting a |
17
|
|
|
|
|
|
|
# warning. |
18
|
2
|
|
|
2
|
|
1056
|
use Moose::Meta::Attribute::Custom::Trait::Chained (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
66
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
package MooseX::Traits::Attribute::Chained; |
21
|
|
|
|
|
|
|
$MooseX::Traits::Attribute::Chained::VERSION = '1.0.3'; |
22
|
2
|
|
|
2
|
|
373
|
use Moose::Role; |
|
2
|
|
|
|
|
4891
|
|
|
2
|
|
|
|
|
17
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
override accessor_metaclass => sub { |
25
|
|
|
|
|
|
|
'MooseX::Attribute::Chained::Method::Accessor'; |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
package MooseX::Attribute::Chained::Method::Accessor; |
29
|
|
|
|
|
|
|
$MooseX::Attribute::Chained::Method::Accessor::VERSION = '1.0.3'; |
30
|
2
|
|
|
2
|
|
13464
|
use Carp qw(confess); |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
156
|
|
31
|
2
|
|
|
2
|
|
16
|
use Try::Tiny; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
114
|
|
32
|
2
|
|
|
2
|
|
13
|
use base 'Moose::Meta::Method::Accessor'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1067
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _generate_accessor_method_inline { |
35
|
3
|
|
|
3
|
|
2540
|
my $self = shift; |
36
|
3
|
|
|
|
|
12
|
my $attr = $self->associated_attribute; |
37
|
3
|
50
|
|
|
|
27
|
my $clone |
38
|
|
|
|
|
|
|
= $attr->associated_class->has_method("clone") |
39
|
|
|
|
|
|
|
? '$_[0]->clone' |
40
|
|
|
|
|
|
|
: 'bless { %{$_[0]} }, ref $_[0]'; |
41
|
|
|
|
|
|
|
|
42
|
3
|
50
|
|
|
|
107
|
if ( $Moose::VERSION >= 1.9900 ) { |
43
|
|
|
|
|
|
|
return try { |
44
|
3
|
|
|
3
|
|
140
|
$self->_compile_code( |
45
|
|
|
|
|
|
|
[ 'sub {', |
46
|
|
|
|
|
|
|
'if (@_ > 1) {', |
47
|
|
|
|
|
|
|
$attr->_inline_set_value( '$_[0]', '$_[1]' ), |
48
|
|
|
|
|
|
|
'return $_[0];', |
49
|
|
|
|
|
|
|
'}', |
50
|
|
|
|
|
|
|
$attr->_inline_get_value('$_[0]'), |
51
|
|
|
|
|
|
|
'}', |
52
|
|
|
|
|
|
|
] |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
catch { |
56
|
0
|
|
|
0
|
|
0
|
confess "Could not generate inline accessor because : $_"; |
57
|
3
|
|
|
|
|
26
|
}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
0
|
return $self->next::method(@_); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _generate_writer_method_inline { |
65
|
2
|
|
|
2
|
|
713
|
my $self = shift; |
66
|
2
|
|
|
|
|
18
|
my $attr = $self->associated_attribute; |
67
|
2
|
50
|
|
|
|
16
|
my $clone |
68
|
|
|
|
|
|
|
= $attr->associated_class->has_method("clone") |
69
|
|
|
|
|
|
|
? '$_[0]->clone' |
70
|
|
|
|
|
|
|
: 'bless { %{$_[0]} }, ref $_[0]'; |
71
|
2
|
50
|
|
|
|
65
|
if ( $Moose::VERSION >= 1.9900 ) { |
72
|
|
|
|
|
|
|
return try { |
73
|
2
|
|
|
2
|
|
88
|
$self->_compile_code( |
74
|
|
|
|
|
|
|
[ 'sub {', $attr->_inline_set_value( '$_[0]', '$_[1]' ), |
75
|
|
|
|
|
|
|
'$_[0]', '}', |
76
|
|
|
|
|
|
|
] |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
catch { |
80
|
0
|
|
|
0
|
|
|
confess "Could not generate inline writer because : $_"; |
81
|
2
|
|
|
|
|
16
|
}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
else { |
84
|
0
|
|
|
|
|
|
return $self->next::method(@_); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _inline_post_body { |
89
|
0
|
|
|
0
|
|
|
return 'return $_[0] if (scalar(@_) >= 2);' . "\n"; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__END__ |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=pod |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=encoding UTF-8 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 NAME |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
MooseX::Attribute::Chained - Attribute that returns the instance to allow for chaining |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 VERSION |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
version 1.0.3 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SYNOPSIS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
package Test; |
111
|
|
|
|
|
|
|
use Moose; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
has debug => ( |
114
|
|
|
|
|
|
|
traits => [ 'Chained' ], |
115
|
|
|
|
|
|
|
is => 'rw', |
116
|
|
|
|
|
|
|
isa => 'Bool', |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub complex_method |
120
|
|
|
|
|
|
|
{ |
121
|
|
|
|
|
|
|
my $self = shift; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
#... |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
print "helper message" if $self->debug; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
#... |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
1; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Which allows for: |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my $test = Test->new; |
136
|
|
|
|
|
|
|
$test->debug(1)->complex_method; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
$test->debug(1); # returns $test |
139
|
|
|
|
|
|
|
$test->debug; # returns 1 |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 DESCRIPTION |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
MooseX::Attribute::Chained is a Moose Trait which allows for method chaining |
144
|
|
|
|
|
|
|
on accessors by returning $self on write/set operations. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 AUTHORS |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=over 4 |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Tom Hukins <tom@eborcom.com> |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Moritz Onken <onken@netcubed.de> |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
David McLaughlin <david@dmclaughlin.com> |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=back |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Tom Hukins. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
169
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 BUGS |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website |
174
|
|
|
|
|
|
|
L<http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Attribute-Chained> |
175
|
|
|
|
|
|
|
or by email to L<bug-moosex-attribute-chained at |
176
|
|
|
|
|
|
|
rt.cpan.org|mailto:bug-moosex-attribute-chained at rt.cpan.org>. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
179
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
180
|
|
|
|
|
|
|
feature. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |