Added version and authority info to a couple of files.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Composite / Trait.pm
1 package MooseX::AttributeHelpers::Composite::Trait;
2 use Moose::Role;
3 use MooseX::AttributeHelpers::MethodProvider;
4 use MooseX::AttributeHelpers::Meta::Method::Provided;
5
6 our $VERSION   = '0.01';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 has provides => (
10     is      => 'ro',
11     isa     => 'HashRef',
12     default => sub { {} },
13 );
14
15 after 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
47 1;
48
49 __END__
50
51 =pod
52
53 =head1 NAME
54
55 MooseX::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
93 This is an expansion on the basic L<MooseX::AttributeHelpers> idea, allowing you
94 to compose the methods provided by the various
95 L<MethodProviders|MooseX::AttributeHelpers::MethodProvider> without naming
96 conflicts.  In addition, this module provides a way to get these helper
97 methods without being locked into a particular
98 L<metaclass|MooseX::AttributeHelpers::Composite>, as it is simply a role with
99 a provides attribute and a method modifier.  It cannot, however, be used with
100 old-style AttributeHelpers - the 'provides' name will conflict.
101
102 =head1 ATTRIBUTES
103
104 =over 4
105
106 =item provides
107
108 The map that tells Composite which methods from which providers you would like
109 installed 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
124 All complex software has bugs lurking in it, and this module is no
125 exception. If you find a bug please either email me, or add the bug
126 to cpan-RT.
127
128 =head1 AUTHOR
129
130 Paul Driver E<lt>frodwith@cpan.orgE<gt>
131
132 =cut