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