2 package MooseX::AttributeHelpers::Trait::Base;
4 use Moose::Util::TypeConstraints;
7 our $AUTHORITY = 'cpan:STEVAN';
9 requires 'helper_type';
11 # this is the method map you define ...
19 # these next two are the possible methods
20 # you can use in the 'provides' map.
22 # provide a Class or Role which we can
23 # collect the method providers from
25 # requires_attr 'method_provider'
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' => (
36 return +{} unless $self->has_method_provider;
37 # or grab them from the role/class
38 my $method_provider = $self->method_provider->meta;
41 $_ => $method_provider->get_method($_)
42 } $method_provider->get_method_list
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);
52 ## Methods called prior to instantiation
54 sub process_options_for_provides {
55 my ($self, $options) = @_;
57 if (my $type = $self->helper_type) {
58 (exists $options->{isa})
59 || confess "You must define a type with the $type metaclass";
61 my $isa = $options->{isa};
63 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
64 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
67 ($isa->is_a_type_of($type))
68 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
72 before '_process_options' => sub {
73 my ($self, $name, $options) = @_;
74 $self->process_options_for_provides($options, $name);
77 ## methods called after instantiation
79 # this confirms that provides has
80 # all valid possibilities in it
81 sub check_provides_values {
84 my $method_constructors = $self->method_constructors;
86 foreach my $key (keys %{$self->provides}) {
87 (exists $method_constructors->{$key})
88 || confess "$key is an unsupported method type";
92 after 'install_accessors' => sub {
94 my $class = $attr->associated_class;
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;
103 # before we install them, lets
104 # make sure they are valid
105 $attr->check_provides_values;
107 my $method_constructors = $attr->method_constructors;
109 my $class_name = $class->name;
111 foreach my $key (keys %{$attr->provides}) {
113 my $method_name = $attr->provides->{$key};
115 if ($class->has_method($method_name)) {
116 confess "The method ($method_name) already exists in class (" . $class->name . ")";
119 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
120 $method_constructors->{$key}->(
125 package_name => $class_name,
126 name => $method_name,
129 $attr->associate_method($method);
130 $class->add_method($method_name => $method);
134 after 'remove_accessors' => sub {
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');
147 no Moose::Util::TypeConstraints;