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