Added version and authority info to a couple of files.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Composite / Trait.pm
CommitLineData
1b45ecc4 1package MooseX::AttributeHelpers::Composite::Trait;
2use Moose::Role;
3use MooseX::AttributeHelpers::MethodProvider;
4use MooseX::AttributeHelpers::Meta::Method::Provided;
5
17dcc9c9 6our $VERSION = '0.01';
7our $AUTHORITY = 'cpan:STEVAN';
8
1b45ecc4 9has provides => (
10 is => 'ro',
11 isa => 'HashRef',
12 default => sub { {} },
13);
14
15after install_accessors => sub {
16 my $attr = shift;
17 my $class = $attr->associated_class;
18
19 my $provides = $attr->provides;
20
21 foreach my $method_provider (keys %$provides) {
22 my $typename = get_provider_type($method_provider);
23 confess "Attribute must be of type $typename to use $method_provider"
24 unless ($attr->has_type_constraint
25 && $attr->type_constraint->is_a_type_of($typename));
26
27 my $spec = $provides->{$method_provider};
28 my $factories = get_provider_methods($method_provider, $spec);
29
30 foreach my $method_name (keys %$factories) {
31 confess "$method_name already exists in class " . $class->name
32 if $class->has_method($method_name);
33
34 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
35 $factories->{$method_name}->(
36 $attr,
37 $attr->get_read_method_ref,
38 $attr->get_write_method_ref
39 ),
40 );
41 $attr->associate_method($method);
42 $class->add_method($method_name => $method)
43 }
44 }
45};
46
471;
f2b3b8f9 48
49__END__
50
51=pod
52
53=head1 NAME
54
55MooseX::AttributeHelpers::Composite:Trait
56
57=head1 SYNOPSIS
58
59 package Foo;
60 use Moose;
61 use MooseX::AttributeHelpers;
62
63 has foo => (
64 #metaclass => 'Composite',
65 traits => ['MooseX::AttributeHelpers::Composite::Trait'],
66 is => 'ro',
67 isa => 'Int',
68 default => 0,
69 provides => {
70 Counter => {
71 inc => 'inc_foo',
72 reset => 'reset_foo',
73 },
74 Number => {
75 mul => 'mul_foo',
76 },
77 # My::Method::Provider => {
78 # some_method => 'foo_method',
79 #},
80 },
81 );
82
83 package main;
84
85 my $foo = Foo->new;
86 $foo->inc_foo;
87 $foo->mul_foo(3);
88 print $foo->foo . "\n";
89 # 3
90
91=head1 DESCRIPTION
92
93This is an expansion on the basic L<MooseX::AttributeHelpers> idea, allowing you
94to compose the methods provided by the various
95L<MethodProviders|MooseX::AttributeHelpers::MethodProvider> without naming
96conflicts. In addition, this module provides a way to get these helper
97methods without being locked into a particular
98L<metaclass|MooseX::AttributeHelpers::Composite>, as it is simply a role with
99a provides attribute and a method modifier. It cannot, however, be used with
100old-style AttributeHelpers - the 'provides' name will conflict.
101
102=head1 ATTRIBUTES
103
104=over 4
105
106=item provides
107
108The map that tells Composite which methods from which providers you would like
109installed under which names. It follows the format
110
111 ProviderName => {
112 provided_method => desired_method_name,
113 ...
114 },
115 AnotherProvider => {
116 ...
117 },
118 ...
119
120=back
121
122=head1 BUGS
123
124All complex software has bugs lurking in it, and this module is no
125exception. If you find a bug please either email me, or add the bug
126to cpan-RT.
127
128=head1 AUTHOR
129
130Paul Driver E<lt>frodwith@cpan.orgE<gt>
131
132=cut