| 1 | package MooseX::NestedAttributesConstructor; | ||
| 2 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | our $VERSION = '0.01'; | |
| 4 | |||
| 5 | 1 | Moose::Exporter->setup_import_methods( | |
| 6 | class_metaroles => { | ||
| 7 | class => ['MooseX::NestedAttributesConstructor::Trait::Class'] | ||
| 8 | } | ||
| 9 | ); | ||
| 10 | |||
| 11 | package MooseX::NestedAttributesConstructor::Meta::Trait::NestedAttribute; | ||
| 12 | 1 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | Moose::Util::meta_attribute_alias('NestedAttribute'); | |
| 14 | |||
| 15 | 1 | 1; | |
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =encoding utf8 | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | MooseX::NestedAttributesConstructor - Create attributes from a nested data structure | ||
| 24 | |||
| 25 | =head1 OVERVIEW | ||
| 26 | |||
| 27 | package Address | ||
| 28 | use Moose; | ||
| 29 | |||
| 30 | has street => ( is => 'rw' ); | ||
| 31 | has city => ( is => 'rw' ); | ||
| 32 | # ... | ||
| 33 | |||
| 34 | package Person; | ||
| 35 | use Moose; | ||
| 36 | use MooseX::NestedAttributesConstructor | ||
| 37 | |||
| 38 | has name => ( is => 'rw' ); | ||
| 39 | has addresses => ( is => 'rw', | ||
| 40 | isa => 'ArrayRef[Address]', | ||
| 41 | traits => ['NestedAttribute'] ); | ||
| 42 | # ... | ||
| 43 | |||
| 44 | package main; | ||
| 45 | use Person; | ||
| 46 | |||
| 47 | my $p = Person->new(name => 'sshaw', | ||
| 48 | addresses => [ | ||
| 49 | { city => 'LA' }, | ||
| 50 | { city => 'Da Bay' }, | ||
| 51 | { city => 'Even São José' } | ||
| 52 | ]); | ||
| 53 | |||
| 54 | say $_->city for @{$p->addresses}; | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | This module sets attributes from a nested data structure passed your object's constructor. | ||
| 59 | The appropriate types will be created, just add the C<NestedAttrubute> trait to attributes with | ||
| 60 | a custom or parameterized type. | ||
| 61 | |||
| 62 | Nested attributes are turned into objects after C<BUILDARGS> is called. | ||
| 63 | |||
| 64 | =head1 AUTHOR | ||
| 65 | |||
| 66 | Skye Shaw (sshaw AT lucas.cis.temple.edu) | ||
| 67 | |||
| 68 | =head1 SEE ALSO | ||
| 69 | |||
| 70 | L<MooseX::StrictConstructor> | ||
| 71 | |||
| 72 | =head1 COPYRIGHT | ||
| 73 | |||
| 74 | Copyright (c) 2012 Skye Shaw. | ||
| 75 | |||
| 76 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||