line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of MooseX-Util |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2012 by Chris Weyl. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software, licensed under: |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# The GNU Lesser General Public License, Version 2.1, February 1999 |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
package MooseX::Traitor; |
11
|
|
|
|
|
|
|
BEGIN { |
12
|
1
|
|
|
1
|
|
697
|
$MooseX::Traitor::AUTHORITY = 'cpan:RSRCHBOY'; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
$MooseX::Traitor::VERSION = '0.003'; |
15
|
|
|
|
|
|
|
# ABSTRACT: An alternate way to compose your classes with traits |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
1
|
|
9
|
use Moose::Role; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
11
|
|
18
|
1
|
|
|
1
|
|
7494
|
use namespace::autoclean; |
|
1
|
|
|
|
|
1476
|
|
|
1
|
|
|
|
|
7
|
|
19
|
1
|
|
|
1
|
|
935
|
use MooseX::AttributeShortcuts; |
|
1
|
|
|
|
|
383472
|
|
|
1
|
|
|
|
|
9
|
|
20
|
1
|
|
|
1
|
|
34702
|
use MooseX::Util (); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
151
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub with_traits { |
24
|
1
|
|
|
1
|
1
|
2978
|
my ($thing, @traits) = @_; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
33
|
|
|
12
|
my $class = blessed $thing || $thing; |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
|
|
9
|
return MooseX::Util::with_traits($class => @traits); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
!!42; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
__END__ |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=pod |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=encoding UTF-8 |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=for :stopwords Chris Weyl composable CLOS behaviour behaviours |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=for :stopwords Wishlist flattr flattr'ed gittip gittip'ed |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 NAME |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
MooseX::Traitor - An alternate way to compose your classes with traits |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 VERSION |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
This document describes version 0.003 of MooseX::Traitor - released May 22, 2014 as part of MooseX-Util. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 SYNOPSIS |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# in your class definition... |
54
|
|
|
|
|
|
|
package MyClass; |
55
|
|
|
|
|
|
|
use Moose; |
56
|
|
|
|
|
|
|
use namespace::autoclean; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
with 'MooseX::Traitor'; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# somewhere else in Gotham... |
61
|
|
|
|
|
|
|
my $thinger = MyClass->with_traits('Thinger::Trait1')->new(...); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 DESCRIPTION |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
One of the most powerful things about L<Moose> is that with roles and easy |
66
|
|
|
|
|
|
|
"anonymous" class creation we are blessed with a fantastic new way of |
67
|
|
|
|
|
|
|
creating classes, often on the fly, out of other classes and those composable |
68
|
|
|
|
|
|
|
bits of behaviour, roles. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Even better, this application of discrete chunks of behaviours enables people |
71
|
|
|
|
|
|
|
simply using a class to extend and tweak its behaviour in new ways -- possibly |
72
|
|
|
|
|
|
|
ways never contemplated by the authors of the classes being altered. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 METHODS |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 with_traits(<trait1>, ...) |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
This method builds an anonymous class from the consuming class and any traits |
79
|
|
|
|
|
|
|
specified. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
You may use the full trait specification syntax, e.g.: |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
MyClass->with_traits('My::Trait' => { -excludes => ... }) |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Calling this routine with no traits specified will simply return the name of |
86
|
|
|
|
|
|
|
the class. This is not considered an error. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Note that we handle being called directly against a package (e.g. |
89
|
|
|
|
|
|
|
C<MyClass->with_traits(...)>) and against an instance (e.g. |
90
|
|
|
|
|
|
|
C<$self->with_traits(...)>) identically; in each instance the class |
91
|
|
|
|
|
|
|
referenced is subclassed. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 ROLES OR TRAITS? |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
There are many different definitions of what a role is vs a trait, ranging |
96
|
|
|
|
|
|
|
from "hey man, it's all cool" to "CLOS calls them all traits SO TRAITS IS THE |
97
|
|
|
|
|
|
|
ONE TRUE NAME", it seems that most people tend to think of them this way: |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Roles are traits that a class knowingly consumes (e.g. via with()). |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Traits are roles that are applied without the class' consent (e.g. anonymous |
102
|
|
|
|
|
|
|
subclass composition or C<$trait_meta->apply('ClassThinger')>). |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Or maybe that's just what this author is imposing on everyone else. Either |
105
|
|
|
|
|
|
|
way, that's what we'll be using here if the definition ever becomes important. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 SEE ALSO |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Please see those modules/websites for more information related to this module. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=over 4 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item * |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
L<MooseX::Util|MooseX::Util> |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=back |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 SOURCE |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
The development version is on github at L<http://https://github.com/RsrchBoy/moosex-util> |
122
|
|
|
|
|
|
|
and may be cloned from L<git://https://github.com/RsrchBoy/moosex-util.git> |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 BUGS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website |
127
|
|
|
|
|
|
|
https://github.com/RsrchBoy/moosex-util/issues |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
130
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
131
|
|
|
|
|
|
|
feature. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 AUTHOR |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Chris Weyl <cweyl@alumni.drew.edu> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 I'm a material boy in a material world |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=begin html |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
<a href="https://www.gittip.com/RsrchBoy/"><img src="https://raw.githubusercontent.com/gittip/www.gittip.com/master/www/assets/%25version/logo.png" /></a> |
142
|
|
|
|
|
|
|
<a href="http://bit.ly/rsrchboys-wishlist"><img src="http://wps.io/wp-content/uploads/2014/05/amazon_wishlist.resized.png" /></a> |
143
|
|
|
|
|
|
|
<a href="https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Fmoosex-util&title=RsrchBoy's%20CPAN%20MooseX-Util&tags=%22RsrchBoy's%20MooseX-Util%20in%20the%20CPAN%22"><img src="http://api.flattr.com/button/flattr-badge-large.png" /></a> |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=end html |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Please note B<I do not expect to be gittip'ed or flattr'ed for this work>, |
148
|
|
|
|
|
|
|
rather B<it is simply a very pleasant surprise>. I largely create and release |
149
|
|
|
|
|
|
|
works like this because I need them or I find it enjoyable; however, don't let |
150
|
|
|
|
|
|
|
that stop you if you feel like it ;) |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
L<Flattr this|https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Fmoosex-util&title=RsrchBoy's%20CPAN%20MooseX-Util&tags=%22RsrchBoy's%20MooseX-Util%20in%20the%20CPAN%22>, |
153
|
|
|
|
|
|
|
L<gittip me|https://www.gittip.com/RsrchBoy/>, or indulge my |
154
|
|
|
|
|
|
|
L<Amazon Wishlist|http://bit.ly/rsrchboys-wishlist>... If you so desire. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
This software is Copyright (c) 2012 by Chris Weyl. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
This is free software, licensed under: |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
The GNU Lesser General Public License, Version 2.1, February 1999 |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |