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