| 1 | package MooseX::Attribute::Prototype; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use Moose; | ||
| 5 | use Moose::Exporter; | ||
| 6 | use MooseX::Attribute::Prototype::Meta; | ||
| 7 | use Moose::Util::MetaRole; | ||
| 8 | use MooseX::Attribute::Prototype::Meta::Attribute::Trait::Prototype; | ||
| 9 | |||
| 10 | our $VERSION = '0.10'; | ||
| 11 | our $AUTHORITY = 'cpan:CTBROWN'; | ||
| 12 | |||
| 13 | Moose::Exporter->setup_import_methods(); | ||
| 14 | |||
| 15 | sub init_meta { | ||
| 16 | |||
| 17 | my ( $caller, %options ) = @_; | ||
| 18 | |||
| 19 | Moose::Util::MetaRole::apply_metaclass_roles( | ||
| 20 | for_class => $options{for_class} , | ||
| 21 | metaclass_roles => [ 'MooseX::Attribute::Prototype::Meta' ] , | ||
| 22 | ); | ||
| 23 | |||
| 24 | } | ||
| 25 | |||
| 26 | no Moose; | ||
| 27 | |||
| 28 | |||
| 29 | =pod | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | MooseX::Attribute::Prototype - Borrow and Extend Moose Attrtibutes | ||
| 34 | |||
| 35 | =head1 VERSION | ||
| 36 | |||
| 37 | 0.10 - Released 2009-07-18 | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | package MyClass; | ||
| 42 | use Moose; | ||
| 43 | use MooseX::Attribute::Prototype; | ||
| 44 | |||
| 45 | has 'my_attr' => ( | ||
| 46 | is => 'rw' , | ||
| 47 | isa => 'Str' , | ||
| 48 | prototype => 'MyRole/my_attr' , | ||
| 49 | ); | ||
| 50 | |||
| 51 | |||
| 52 | has 'my_attr_2' => prototype => 'MyRole2/my_attr_2'; | ||
| 53 | |||
| 54 | has 'my_attr_3' => prototype => 'MyRole3'; # Same as 'MyRole3/myrole3' | ||
| 55 | |||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | This module implements attribute prototyping -- the practice of borrowing | ||
| 60 | an attribute from a role and optionally overriding/extending the attribute | ||
| 61 | definition. This is This works very similar to Moose's native attribute | ||
| 62 | cloning, but allows for additional benefits such as changing the name of | ||
| 63 | the attribute and the abstracting of attributes into roles. | ||
| 64 | |||
| 65 | Attributes are very often designed as objects that have their own types and | ||
| 66 | methods associated with them. MooseX::Attribute::Prototype takes a very | ||
| 67 | pragmatic view of attributes. They are the fundamental building | ||
| 68 | blocks of a class. This module promotes a more natural reuse of attributes. | ||
| 69 | |||
| 70 | When your attribute includes a C<prototype> specification, the | ||
| 71 | attribute is copied from the role and attribute. In many situations, | ||
| 72 | all you will want is declare a C<prototype>. | ||
| 73 | All current specifications override those provided by prototype. | ||
| 74 | |||
| 75 | |||
| 76 | =head1 How to use Attribute Prototypes | ||
| 77 | |||
| 78 | All variants of usage are in the SYNOPSIS above. This is a more | ||
| 79 | thorough explanation. | ||
| 80 | |||
| 81 | Prototypes are just any good ole Moose attributes in good ole | ||
| 82 | L<Moose::Role>. To use them simply declare a C<prototype> in your | ||
| 83 | attribute definition: | ||
| 84 | |||
| 85 | prototype => 'MyRole/attribute' | ||
| 86 | |||
| 87 | where C<MyRole> is the name of the role and C<attribute> is the name of | ||
| 88 | the attribute. As of version 0.05, you may use the abbreviated | ||
| 89 | specification and omit the name of the C<attribute>. The attribute | ||
| 90 | used as the prototype has the the same name as the role, except it | ||
| 91 | has all lower-case letters. | ||
| 92 | |||
| 93 | prototype => 'MyRole' | ||
| 94 | |||
| 95 | In this example, the prototype is C<MyRole/myrole> serves as the | ||
| 96 | prototype. This is just a shortcut to cover the very common occurrence where | ||
| 97 | the attribute shares the name of the role. | ||
| 98 | |||
| 99 | |||
| 100 | =head1 WHY? | ||
| 101 | |||
| 102 | L<MooseX::Role::Parameterized> and L<MooseX::Types> abstract | ||
| 103 | the roles and types, respectively. But surprisinly, there is no similar | ||
| 104 | functionality for attributes. Moose leans towards viewing attributes | ||
| 105 | as containers for data. However, attributes can store full-fledged | ||
| 106 | objects. And these objects often have specialized types and subtypes, | ||
| 107 | methods, and behaviors (such as getting their values using | ||
| 108 | L<MooseX::Getopt>). In fact, attribute specifications, can often become | ||
| 109 | the majority of code for a given application. Why not seperate these | ||
| 110 | chunks into horizontally-reusable roles? | ||
| 111 | |||
| 112 | L<MooseX::Attribute::Prototype> takes a functional view of attributes -- | ||
| 113 | slots that can contain anything -- and provides an easy interface for | ||
| 114 | making these slots reusable. | ||
| 115 | |||
| 116 | =head2 Why Not Moose's Attribute Clone Mechanism? | ||
| 117 | |||
| 118 | Moose's attribute cloning does not allow you to change the name | ||
| 119 | of the derived attribute. You can take the defaults of an attribute from | ||
| 120 | a role and change its default, but good luck in changing the name of the | ||
| 121 | attribute. | ||
| 122 | |||
| 123 | =head2 Subclassing Benefit | ||
| 124 | |||
| 125 | L<Moose> makes subclassing easy through the c<extends> sugar. More | ||
| 126 | often than not, however, Moose applications are an amalgam of | ||
| 127 | objects including other Moose classes and other CPAN modules. In these | ||
| 128 | cases, one often places the objects in the the attributes. | ||
| 129 | L<MooseX::Attributes::Prototypes> allows for the Moosifying of these CPAN | ||
| 130 | classes in a reusable way. | ||
| 131 | |||
| 132 | |||
| 133 | =head1 SEE ALSO | ||
| 134 | |||
| 135 | L<MooseX::Attribute::Prototype::Meta>, | ||
| 136 | |||
| 137 | L<MooseX::Attribute::Prototype::Object>, | ||
| 138 | |||
| 139 | L<MooseX::Attribute::Prototype::Collection>, | ||
| 140 | |||
| 141 | L<Moose> | ||
| 142 | |||
| 143 | L<MooseX::Role::Parameterized> | ||
| 144 | |||
| 145 | L<MooseX::Types> | ||
| 146 | |||
| 147 | |||
| 148 | =head1 AUTHOR | ||
| 149 | |||
| 150 | Christopher Brown, C<< <ctbrown at cpan.org> >> | ||
| 151 | |||
| 152 | =head1 BUGS | ||
| 153 | |||
| 154 | Please report any bugs or feature requests to C<bug-moosex-attribute-prototype at rt.cpan.org>, or through | ||
| 155 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Attribute-Prototype>. I will be notified, and then you'll | ||
| 156 | automatically be notified of progress on your bug as I make changes. | ||
| 157 | |||
| 158 | |||
| 159 | =head1 SUPPORT | ||
| 160 | |||
| 161 | You can find documentation for this module with the perldoc command. | ||
| 162 | |||
| 163 | perldoc MooseX::Attribute::Prototype | ||
| 164 | |||
| 165 | You can also look for information at: | ||
| 166 | |||
| 167 | =over 4 | ||
| 168 | |||
| 169 | =item * RT: CPAN's request tracker | ||
| 170 | |||
| 171 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attribute-Prototype> | ||
| 172 | |||
| 173 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 174 | |||
| 175 | L<http://annocpan.org/dist/MooseX-Attribute-Prototype> | ||
| 176 | |||
| 177 | =item * CPAN Ratings | ||
| 178 | |||
| 179 | L<http://cpanratings.perl.org/d/MooseX-Attribute-Prototype> | ||
| 180 | |||
| 181 | =item * Search CPAN | ||
| 182 | |||
| 183 | L<http://search.cpan.org/dist/MooseX-Attribute-Prototype> | ||
| 184 | |||
| 185 | =back | ||
| 186 | |||
| 187 | |||
| 188 | =head1 ACKNOWLEDGEMENTS | ||
| 189 | |||
| 190 | Though they would probably cringe to hear it, this effort would not have | ||
| 191 | been possible without: | ||
| 192 | |||
| 193 | Shawn Moore | ||
| 194 | |||
| 195 | David Rolsky | ||
| 196 | |||
| 197 | Thomas Doran | ||
| 198 | |||
| 199 | Stevan Little | ||
| 200 | |||
| 201 | |||
| 202 | =head1 COPYRIGHT & LICENSE | ||
| 203 | |||
| 204 | Copyright 2009 Christopher Brown and Open Data Group L<http://opendatagroup.com>. | ||
| 205 | |||
| 206 | This program is free software; you can redistribute it and/or modify it | ||
| 207 | under the same terms as Perl itself. | ||
| 208 | |||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | 1; # End of MooseX::Attribute::Prototype |