| 1 | package MooseX::Attribute::Prototype::Meta; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use MooseX::Attribute::Prototype::Object; | ||
| 5 | use MooseX::Attribute::Prototype::Collection; | ||
| 6 | use MooseX::AttributeHelpers; | ||
| 7 | |||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | use MooseX::Attribute::Prototype::Meta::Attribute::Trait::Prototype; | ||
| 11 | |||
| 12 | our $VERSION = '0.10'; | ||
| 13 | our $AUTHORITY = 'cpan:CTBROWN'; | ||
| 14 | |||
| 15 | # This keeps the queue of the roles to keep track which roles the prototype | ||
| 16 | # attribtutes come from. | ||
| 17 | |||
| 18 | has 'prototype_queue' => ( | ||
| 19 | metaclass => 'Collection::Array' , | ||
| 20 | is => 'rw' , | ||
| 21 | isa => 'ArrayRef[Str]' , | ||
| 22 | default => sub { [] } , | ||
| 23 | required => 1 , | ||
| 24 | provides => { | ||
| 25 | 'unshift' => 'queue_role' , | ||
| 26 | 'shift' => 'dequeue_role' , | ||
| 27 | 'get' => 'get_role' , | ||
| 28 | 'count' => 'queue_size' , | ||
| 29 | } , | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | has 'prototypes' => ( | ||
| 34 | is => 'rw' , | ||
| 35 | isa => 'MooseX::Attribute::Prototype::Collection' , | ||
| 36 | default => sub { MooseX::Attribute::Prototype::Collection->new() } , | ||
| 37 | required => 1 , | ||
| 38 | documentation => 'Slot for holding the prototypes definitions' , | ||
| 39 | handles => [ | ||
| 40 | 'add_prototype' , | ||
| 41 | 'get_prototype' , | ||
| 42 | 'count_prototypes' , | ||
| 43 | 'get_prototype_keys' , | ||
| 44 | ] , | ||
| 45 | ); | ||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | around 'add_attribute' => sub { | ||
| 50 | |||
| 51 | my ( $add_attribute, $self, $name, @options ) = @_; | ||
| 52 | |||
| 53 | my %opts; | ||
| 54 | if ( scalar @options % 2 == 0 ) { | ||
| 55 | %opts = @options; | ||
| 56 | } else { #if ( scalar @options == 1 ) { | ||
| 57 | %opts = %{ $options[0] }; | ||
| 58 | } | ||
| 59 | |||
| 60 | # CASE: prototype used in attribute specification. | ||
| 61 | # If specified with 'prototype' we need to first load that roll | ||
| 62 | # into the prototypes slot. Borrow those attributes and | ||
| 63 | # | ||
| 64 | # As of Moose ~0.85, an an option as part of an attribute will | ||
| 65 | # cause a warning to be issued. This is the case for | ||
| 66 | # Here we dynamically add a meta-attribute trait. | ||
| 67 | # | ||
| 68 | if ( $opts{ prototype } ) { | ||
| 69 | |||
| 70 | # Install the metaclass | ||
| 71 | # Check to see if this is the abbreviated prototype specification | ||
| 72 | if ( $opts{ prototype } !~ m/\// ) { | ||
| 73 | |||
| 74 | # CTB: 2009-07-18 | ||
| 75 | |||
| 76 | # if ( $opts{ 'traits' } ) { | ||
| 77 | # push @{ $opts{'traits'} }, 'Prototype'; | ||
| 78 | # } else { | ||
| 79 | # $opts{ 'traits' } = 'Prototype'; | ||
| 80 | # } | ||
| 81 | # print Dumper \%opts; | ||
| 82 | # print "\n"; | ||
| 83 | $opts{ prototype } = $opts{ prototype } . '/' . lc( $opts{ prototype } ); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | $opts{ 'traits' } = $opts{'traits'} ? [ 'Prototype', @{ $opts{'traits'} } ] : ['Prototype'] ; | ||
| 88 | |||
| 89 | |||
| 90 | # $self->flag( $self->flag + 1 ); # Indicates that all attributes until | ||
| 91 | # # the flag are unset should be diverted into | ||
| 92 | # # the prototype slot | ||
| 93 | |||
| 94 | my $role_name = _parse_prototype_name( $opts{ prototype } )->{role}; | ||
| 95 | |||
| 96 | $self->queue_role( $role_name ); # Keeps track of the rolls | ||
| 97 | |||
| 98 | |||
| 99 | # Dynamic loading of classes. | ||
| 100 | Class::MOP::load_class( $role_name ); | ||
| 101 | my $role = Moose::Meta::Role->initialize( $role_name ); | ||
| 102 | $role->apply( $self ); | ||
| 103 | |||
| 104 | # Now, let's construct the new opt string from the prototype. | ||
| 105 | |||
| 106 | my $proto = $self->prototypes->get( $opts{ prototype } ) | ||
| 107 | || confess( $opts{ prototype } . " does not exist" ); | ||
| 108 | |||
| 109 | # Clobber the prototype options with those specified in the | ||
| 110 | # class | ||
| 111 | my %new_opts = ( %{ $proto->options }, %opts ); | ||
| 112 | |||
| 113 | # Now. let's install the attribute, finally! | ||
| 114 | # Would it be possible to create this r | ||
| 115 | $self->$add_attribute( $name, %new_opts ); | ||
| 116 | |||
| 117 | # Mark the prototype as referenced | ||
| 118 | $self->prototypes->set_referenced( $opts{ prototype } ); | ||
| 119 | |||
| 120 | |||
| 121 | # We are done borrowing | ||
| 122 | $self->dequeue_role; # We are done with the prototype roll now. | ||
| 123 | # $self->flag( $self->flag - 1 ); # Set the flag. | ||
| 124 | |||
| 125 | |||
| 126 | } elsif ( $self->queue_size > 0 ) { | ||
| 127 | |||
| 128 | # We were not using a prototype, but the flag is set | ||
| 129 | # - divert the install to the prototypes. | ||
| 130 | |||
| 131 | $self->get_role(0); # role_name. We do not remove this from the queue since | ||
| 132 | # each role can provide multiple attributes. | ||
| 133 | |||
| 134 | $self->prototypes->add_prototype( | ||
| 135 | MooseX::Attribute::Prototype::Object->new( | ||
| 136 | name => $self->get_role(0) . "/" . $name , | ||
| 137 | options => \%opts | ||
| 138 | ) | ||
| 139 | ); | ||
| 140 | |||
| 141 | } else { | ||
| 142 | |||
| 143 | # PLAIN OLD ATTRIBUTE | ||
| 144 | $self->$add_attribute( $name, @options ); | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | }; | ||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | # These should be exported from MooseX::Attribute::Prototype::Object | ||
| 153 | sub _parse_prototype_name { | ||
| 154 | |||
| 155 | my $name = shift || confess( "Must pass a prototype name to _parse_prototype_name" ); | ||
| 156 | my ( $role, $attribute ); | ||
| 157 | |||
| 158 | if ( $name =~ m/\// ) { | ||
| 159 | $name =~ m/^(.*)\/(.*)$/; | ||
| 160 | $role = $1; | ||
| 161 | $attribute = $2; | ||
| 162 | } elsif ( $name =~ /::/ ) { | ||
| 163 | $name =~ m/^(.*)::(.*)$/; | ||
| 164 | $role = $name; | ||
| 165 | $attribute = lc $2; | ||
| 166 | } else { | ||
| 167 | $role = $name; | ||
| 168 | $attribute = lc $name; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | return { role => $role , attribute => $attribute } ; | ||
| 173 | |||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | no Moose::Role; | ||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | =pod | ||
| 182 | |||
| 183 | =head1 NAME | ||
| 184 | |||
| 185 | MooseX::Attribute::Prototype::Meta - Metaclass Role for Attribute Prototypes | ||
| 186 | |||
| 187 | =head1 VERSION | ||
| 188 | |||
| 189 | 0.10 - Released 2009-07-18 | ||
| 190 | |||
| 191 | =head1 SYNOPSIS | ||
| 192 | |||
| 193 | Please see L<MooseX::Attribute::Prototype>. | ||
| 194 | |||
| 195 | =head1 DESCRIPTION | ||
| 196 | |||
| 197 | This metaclass role, when injected into an objects metaclass provides | ||
| 198 | the ability to borrow and extend Moose attributes. | ||
| 199 | |||
| 200 | =head1 INTERNAL METHODS | ||
| 201 | |||
| 202 | =head2 _parse_prototype_name | ||
| 203 | |||
| 204 | Given the name of the prototype in either standard or abbreviated form, | ||
| 205 | returns a hashref with C<role> and C<attribute> key-value pairs. | ||
| 206 | |||
| 207 | # { role => 'M::X::Foo', attribute => 'bar' } | ||
| 208 | _parse_prototype_name( 'M::X::Foo/bar' ); | ||
| 209 | |||
| 210 | # { role => 'M::X::Foo', attribute => 'foo' } | ||
| 211 | _parse_prototype_name( 'M::X::Foo' ); | ||
| 212 | |||
| 213 | |||
| 214 | =head1 SEE ALSO | ||
| 215 | |||
| 216 | L<MooseX::Attribute::Prototype>, | ||
| 217 | |||
| 218 | L<MooseX::Attribute::Prototype::Object>, | ||
| 219 | |||
| 220 | L<MooseX::Attribute::Prototype::Collection>, | ||
| 221 | |||
| 222 | L<Moose> | ||
| 223 | |||
| 224 | |||
| 225 | =head1 AUTHOR | ||
| 226 | |||
| 227 | Christopher Brown, C<< <ctbrown at cpan.org> >> | ||
| 228 | |||
| 229 | =head1 BUGS | ||
| 230 | |||
| 231 | Please report any bugs or feature requests to C<bug-moosex-attribute at rt.cpan.org>, or through | ||
| 232 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Attribute-Prototpye>. I will be notified, and then you'll | ||
| 233 | automatically be notified of progress on your bug as I make changes. | ||
| 234 | |||
| 235 | |||
| 236 | =head1 SUPPORT | ||
| 237 | |||
| 238 | You can find documentation for this module with the perldoc command. | ||
| 239 | |||
| 240 | perldoc MooseX::Attribute::Prototype | ||
| 241 | |||
| 242 | You can also look for information at: | ||
| 243 | |||
| 244 | =over 4 | ||
| 245 | |||
| 246 | =item * RT: CPAN's request tracker | ||
| 247 | |||
| 248 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attribute-Prototype> | ||
| 249 | |||
| 250 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 251 | |||
| 252 | L<http://annocpan.org/dist/MooseX-Attribute-Prototype> | ||
| 253 | |||
| 254 | =item * CPAN Ratings | ||
| 255 | |||
| 256 | L<http://cpanratings.perl.org/d/MooseX-Attribute-Prototype> | ||
| 257 | |||
| 258 | =item * Search CPAN | ||
| 259 | |||
| 260 | L<http://search.cpan.org/dist/MooseX-Attribute-Prototype> | ||
| 261 | |||
| 262 | =back | ||
| 263 | |||
| 264 | |||
| 265 | =head1 ACKNOWLEDGEMENTS | ||
| 266 | |||
| 267 | Though they would probably cringe to hear it, this effort would not have | ||
| 268 | been possible without: | ||
| 269 | |||
| 270 | Shawn Moore | ||
| 271 | |||
| 272 | David Rolsky | ||
| 273 | |||
| 274 | Thomas Doran | ||
| 275 | |||
| 276 | Stevan Little | ||
| 277 | |||
| 278 | |||
| 279 | =head1 COPYRIGHT & LICENSE | ||
| 280 | |||
| 281 | Copyright 2009 Christopher Brown and Open Data Group L<http://opendatagroup.com>, | ||
| 282 | all rights reserved. | ||
| 283 | |||
| 284 | This program is free software; you can redistribute it and/or modify it | ||
| 285 | under the same terms as Perl itself. | ||
| 286 | |||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | |||
| 291 | 1; # End of module MooseX::Attribute::Prototype::Meta |