| MooseX-Attributes-Curried documentation | view source | Contained in the MooseX-Attributes-Curried distribution. |
MooseX::Attributes::Curried - curry your "has"es
package MyAttrs;
use MooseX::Attributes::Curried (
has_datetime => {
isa => 'DateTime',
default => sub { DateTime->now },
},
has_rw => {
is => 'rw',
},
);
package My::Class;
use Moose;
use MyAttrs;
has_datetime 'birthday' => (
is => 'ro',
);
has_rw 'age' => (
isa => 'Int',
);
This module lets you define curried versions of has in Moose. If many of your
attributes share the same options, especially across multiple classes, then you
can refactor those options away into a curried has.
Typical usage of this extension is to create a standalone "has library"
module. If you only need a curried has for one class, then you might as
well just define a sub has_datetime { has(...) } in that class.
When you use your "has library", you can customize each curried has
further by specifying additional options on your import line, like so:
use MyAttrs (
has_datetime => {
is => 'ro',
},
has_datetime => {
-as => 'needs_datetime',
required => 1,
},
);
Your "defaults" for the attribute can also be a code reference. This code reference will receive both the additional specializations performed when the curried attribute was imported, as well as any additional specializations used in the individual attribute. This is immensely powerful, see t/007-smart-has.t for a taste.
MooseX::Attribute::Prototype, which has very similar goals; this extension was originally proposed as an implementation of prototype attributes.
Shawn M Moore, sartak@gmail.com
Copyright 2009 Shawn M Moore.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| MooseX-Attributes-Curried documentation | view source | Contained in the MooseX-Attributes-Curried distribution. |