Abstracts
[gitmo/MooseX-Role-Parameterized.git] / lib / MooseX / Role / Parameterized / Tutorial.pm
CommitLineData
93c2fd30 1package MooseX::Role::Parameterized::Tutorial;
2confess "Don't use this module, read it!";
3
d9e02904 4# ABSTRACT: why and how
93c2fd30 5
d9e02904 6__END__
93c2fd30 7
a4ac31fa 8=head1 MOTIVATION
d2abd756 9
a4ac31fa 10Roles are composable units of behavior. They are useful for factoring out
11functionality common to many classes from any part of your class hierarchy.See
12L<Moose::Cookbook::Roles::Recipe1> for an introduction to L<Moose::Role>.
d2abd756 13
a4ac31fa 14While combining roles affords you a great deal of flexibility, individual roles
15have very little in the way of configurability. Core Moose provides C<alias>
16for renaming methods to avoid conflicts, and C<excludes> for ignoring methods
17you don't want or need (see L<Moose::Cookbook::Roles::Recipe2> for more
18about C<alias> and C<excludes>).
93c2fd30 19
a4ac31fa 20Because roles serve many different masters, they usually provide only the least
21common denominator of functionality. Not all consumers of a role have a C<>.
22Thus, more configurability than C<alias> and C<excludes> is required. Perhaps
23your role needs to know which method to call when it is done. Or what default
24value to use for its url attribute.
d2abd756 25
a4ac31fa 26Parameterized roles offer exactly this solution.
d2abd756 27
93c2fd30 28=head1 USAGE
29
30=head3 C<with>
31
a4ac31fa 32The syntax of a class consuming a parameterized role has not changed from the
33standard C<with>. You pass in parameters just like you pass in C<alias> and
34C<excludes> to ordinary roles:
35
36 with 'MyRole::InstrumentMethod' => {
37 method_name => 'dbh_do',
38 log_to => 'query.log',
39 };
40
93c2fd30 41=head3 C<parameter>
42
a4ac31fa 43Inside your parameterized role, you specify a set of parameters. This is
44exactly like specifying the attributes of a class. Instead of C<has> you use
45the keyword C<parameter>, but your parameters can use any options to C<has>.
46
47 parameter 'delegation' => (
48 is => 'ro',
49 isa => 'HashRef|ArrayRef|RegexpRef',
50 predicate => 'has_delegation',
51 );
52
53Behind the scenes, C<parameter> uses C<has> to add attributes to a parameter
54class. The arguments to C<with> are used to construct a parameter object, which
55has the attributes specified by calls to C<parameter>. The parameter object is
56then passed to...
57
93c2fd30 58=head3 C<role>
59
a4ac31fa 60C<role> takes a block of code that will be used to generate your role with its
61parameters bound. Here is where you put your regular role code: use C<has>,
62method modifiers, and so on. You receive as an argument the parameter object
63constructed by C<with>. You can access the parameters just like regular
64attributes on that object (assuming you declared them readable).
65
66Each time you compose this parameterized role, the role {} block will be
67executed. It will receive a new parameter object and produce an entirely new
68role.
69
70Due to limitations inherent in Perl, you must declare methods with
71C<method name => sub { ... }> instead of the usual C<sub name { ... }>. Your
72methods may, of course, close over the parameter object. This means that your
73methods may use parameters however they wish!
74
93c2fd30 75=head1 IMPLEMENTATION NOTES
76
d2abd756 77=head1 USES
78
79Ideally these will become fully-explained examples in something resembling
80L<Moose::Cookbook>. But for now, only a braindump.
81
82=over 4
83
84=item Configure a role's attributes
85
86You can rename methods with core Moose, but now you can rename attributes. You
87can now also choose type, default value, whether it's required, B<traits>, etc.
88
89 parameter traits => (
90 is => 'ro',
91 isa => 'ArrayRef[Str]',
92 default => sub { [] },
93 );
94
95 has action => (
96 traits => $p->traits,
97 ...
98 );
99
100=item Inform a role of your class' attributes and methods
101
102Core roles can require only methods with specific names. Now your roles can
103require that you specify a method name you wish the role to instrument, or
104which attributes to dump to a file.
105
106 parameter instrument_method => (
107 is => 'ro',
108 isa => 'Str',
109 required => 1,
110 );
111
112 around $p->instrument_method => sub { ... };
113
114=item Arbitrary execution choices
115
116Your role may be able to provide configuration in how the role's methods
117operate. For example, you can tell the role whether to save intermediate
118states.
119
120 parameter save_intermediate => (
121 is => 'ro',
122 isa => 'Bool',
123 default => 0,
124 );
125
126 method process => sub {
127 ...
128 if ($p->save_intermediate) { ... }
129 ...
130 };
131
132=item Deciding a backend
133
134Your role may be able to freeze and thaw your instances using L<YAML>, L<JSON>,
135L<Storable>. Which backend to use can be a parameter.
136
137 parameter format => (
138 is => 'ro',
139 isa => (enum ['Storable', 'YAML', 'JSON']),
140 default => 'Storable',
141 );
142
143 if ($p->format eq 'Storable') {
144 method freeze => sub { ... };
145 method thaw => sub { ... };
146 }
147 elsif ($p->format eq 'YAML') ...
148 ...
149
150=back
151
93c2fd30 152=cut
153