50bc1f475e6afb297d07edc8d51521a51c5e41fa
[gitmo/MooseX-MetaDescription.git] / lib / MooseX / MetaDescription / Meta / Trait.pm
1 package MooseX::MetaDescription::Meta::Trait;
2 use Moose::Role;
3
4 our $VERSION   = '0.02';
5 our $AUTHORITY = 'cpan:STEVAN';
6
7 has 'description' => (
8     is      => 'ro',
9     isa     => 'HashRef',
10     lazy    => 1,   
11     default => sub { +{} },
12 );
13
14 has 'metadescription_classname' => (
15     is      => 'rw',
16     isa     => 'Str', 
17     lazy    => 1,  
18     default => sub {
19         'MooseX::MetaDescription::Description'
20     }
21 );
22
23 has 'metadescription' => (
24     is      => 'ro',
25     isa     => 'MooseX::MetaDescription::Description',
26     lazy    => 1,   
27     default => sub {
28         my $self = shift;
29         
30         my $metadesc_class = $self->metadescription_classname;
31         my $desc           = $self->description;
32         
33         Class::MOP::load_class($metadesc_class);
34         
35         if (my $traits = delete $desc->{traits}) {
36             my $meta = Moose::Meta::Class->create_anon_class(
37                 superclasses => [ $metadesc_class ],
38                 roles        => $traits,
39             );
40             $meta->add_method('meta' => sub { $meta });
41             $metadesc_class = $meta->name;
42         }
43         
44         return $metadesc_class->new(%$desc, descriptor => $self);
45     },
46 );
47
48 no Moose::Role; 1;
49
50 __END__
51
52 =pod
53
54 =head1 NAME
55
56 MooseX::MetaDescription::Meta::Trait - Custom class meta-trait for meta-descriptions
57
58 =head1 SYNOPSIS
59
60   package Foo;
61   use Moose;
62   
63   has 'baz' => (
64       # apply this as a trait to your attribute
65       traits      => [ 'MooseX::MetaDescription::Meta::Trait' ],
66       is          => 'ro',
67       isa         => 'Str',   
68       default     => sub { 'Foo::baz' },
69       description => {
70           bar   => 'Foo::baz::bar',
71           gorch => 'Foo::baz::gorch',
72       }
73   );
74
75 =head1 DESCRIPTION
76
77 This is the core of the Meta Description functionality, it is a role that is done
78 by both L<MooseX::MetaDescription::Meta::Attribute> and L<MooseX::MetaDescription::Meta::Class>
79 and can be used on it's own as a meta-attribute trait.
80
81 =head1 METHODS 
82
83 =over 4
84
85 =item B<description>
86
87 The description C<HASH> ref is stored here.
88
89 =item B<metadescription_classname>
90
91 This provides the name of the metadescription class, currently this 
92 defaults to L<MooseX::MetaDescription::Description>. It is read only 
93 and so can only be specified at instance construction time.
94
95 =item B<metadescription>
96
97 This is the instance of the class specified in C<metadescription_classname>
98 it is generated lazily and is also read-only. In general you will never 
99 need to set this yourself, but simply set C<metadescription_classname>
100 and it will all just work.
101
102
103 =item B<meta>
104
105 The L<Moose::Role> metaclass.
106
107 =back
108
109 =head1 BUGS
110
111 All complex software has bugs lurking in it, and this module is no 
112 exception. If you find a bug please either email me, or add the bug
113 to cpan-RT.
114
115 =head1 AUTHOR
116
117 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
118
119 =head1 COPYRIGHT AND LICENSE
120
121 Copyright 2008 Infinity Interactive, Inc.
122
123 L<http://www.iinteractive.com>
124
125 This library is free software; you can redistribute it and/or modify
126 it under the same terms as Perl itself.
127
128 =cut