Commit | Line | Data |
b70e06c6 |
1 | package MooseX::MetaDescription::Meta::Trait; |
c13295c8 |
2 | use Moose::Role; |
3 | |
d9f5e5ae |
4 | our $VERSION = '0.02'; |
c13295c8 |
5 | our $AUTHORITY = 'cpan:STEVAN'; |
6 | |
7591e02c |
7 | has 'description' => ( |
8 | is => 'ro', |
9 | isa => 'HashRef', |
10 | lazy => 1, |
11 | default => sub { +{} }, |
12 | ); |
13 | |
14 | has 'metadescription_classname' => ( |
d9f5e5ae |
15 | is => 'rw', |
7591e02c |
16 | isa => 'Str', |
91297cb4 |
17 | lazy => 1, |
7591e02c |
18 | default => sub { |
19 | 'MooseX::MetaDescription::Description' |
d9f5e5ae |
20 | } |
7591e02c |
21 | ); |
c13295c8 |
22 | |
7591e02c |
23 | has 'metadescription' => ( |
24 | is => 'ro', |
25 | isa => 'MooseX::MetaDescription::Description', |
26 | lazy => 1, |
27 | default => sub { |
28 | my $self = shift; |
8a18d249 |
29 | |
30 | my $metadesc_class = $self->metadescription_classname; |
31 | my $desc = $self->description; |
32 | |
d9f5e5ae |
33 | Class::MOP::load_class($metadesc_class); |
34 | |
8a18d249 |
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); |
c13295c8 |
45 | }, |
46 | ); |
47 | |
48 | no Moose::Role; 1; |
49 | |
50 | __END__ |
51 | |
52 | =pod |
53 | |
54 | =head1 NAME |
55 | |
d253607a |
56 | MooseX::MetaDescription::Meta::Trait - Custom class meta-trait for meta-descriptions |
c13295c8 |
57 | |
56e78a9c |
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 | |
c13295c8 |
75 | =head1 DESCRIPTION |
76 | |
56e78a9c |
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. |
d253607a |
80 | |
c13295c8 |
81 | =head1 METHODS |
82 | |
83 | =over 4 |
84 | |
d253607a |
85 | =item B<description> |
86 | |
56e78a9c |
87 | The description C<HASH> ref is stored here. |
88 | |
d253607a |
89 | =item B<metadescription_classname> |
90 | |
56e78a9c |
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 | |
d253607a |
95 | =item B<metadescription> |
c13295c8 |
96 | |
56e78a9c |
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 | |
c13295c8 |
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 |