Replace method_provider with a ( future :( ) requires_attr
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Trait / Base.pm
1
2 package MooseX::AttributeHelpers::Trait::Base;
3 use Moose::Role;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.04';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 requires 'helper_type';
10
11 # this is the method map you define ...
12 has 'provides' => (
13     is      => 'ro',
14     isa     => 'HashRef',
15     default => sub {{}}
16 );
17
18
19 # these next two are the possible methods
20 # you can use in the 'provides' map.
21
22 # provide a Class or Role which we can
23 # collect the method providers from
24
25 # requires_attr 'method_provider'
26
27 # or you can provide a HASH ref of anon subs
28 # yourself. This will also collect and store
29 # the methods from a method_provider as well
30 has 'method_constructors' => (
31     is      => 'ro',
32     isa     => 'HashRef',
33     lazy    => 1,
34     default => sub {
35         my $self = shift;
36         return +{} unless $self->has_method_provider;
37         # or grab them from the role/class
38         my $method_provider = $self->method_provider->meta;
39         return +{
40             map {
41                 $_ => $method_provider->get_method($_)
42             } $method_provider->get_method_list
43         };
44     },
45 );
46
47 # extend the parents stuff to make sure
48 # certain bits are now required ...
49 has '+$!default'       => (required => 1);
50 has '+type_constraint' => (required => 1);
51
52 ## Methods called prior to instantiation
53
54 sub process_options_for_provides {
55     my ($self, $options) = @_;
56
57     if (my $type = $self->helper_type) {
58         (exists $options->{isa})
59             || confess "You must define a type with the $type metaclass";
60
61         my $isa = $options->{isa};
62
63         unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
64             $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
65         }
66
67         ($isa->is_a_type_of($type))
68             || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
69     }
70 }
71
72 before '_process_options' => sub {
73     my ($self, $name, $options) = @_;
74     $self->process_options_for_provides($options, $name);
75 };
76
77 ## methods called after instantiation
78
79 # this confirms that provides has
80 # all valid possibilities in it
81 sub check_provides_values {
82     my $self = shift;
83
84     my $method_constructors = $self->method_constructors;
85
86     foreach my $key (keys %{$self->provides}) {
87         (exists $method_constructors->{$key})
88             || confess "$key is an unsupported method type";
89     }
90 }
91
92 after 'install_accessors' => sub {
93     my $attr  = shift;
94     my $class = $attr->associated_class;
95
96     # grab the reader and writer methods
97     # as well, this will be useful for
98     # our method provider constructors
99     my $attr_reader = $attr->get_read_method_ref;
100     my $attr_writer = $attr->get_write_method_ref;
101
102
103     # before we install them, lets
104     # make sure they are valid
105     $attr->check_provides_values;
106
107     my $method_constructors = $attr->method_constructors;
108
109     my $class_name = $class->name;
110
111     foreach my $key (keys %{$attr->provides}) {
112
113         my $method_name = $attr->provides->{$key};
114
115         if ($class->has_method($method_name)) {
116             confess "The method ($method_name) already exists in class (" . $class->name . ")";
117         }
118
119         my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
120             $method_constructors->{$key}->(
121                 $attr,
122                 $attr_reader,
123                 $attr_writer,
124             ),
125             package_name => $class_name,
126             name => $method_name,
127         );
128         
129         $attr->associate_method($method);
130         $class->add_method($method_name => $method);
131     }
132 };
133
134 after 'remove_accessors' => sub {
135     my $attr  = shift;
136     my $class = $attr->associated_class;
137     foreach my $key (keys %{$attr->provides}) {
138         my $method_name = $attr->provides->{$key};
139         my $method = $class->get_method($method_name);
140         $class->remove_method($method_name)
141             if blessed($method) &&
142                $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
143     }
144 };
145
146 no Moose::Role;
147 no Moose::Util::TypeConstraints;
148
149 1;
150