potential fixes for role problems.. doesnt solve everything though; will have to...
[gitmo/MooseX-AlwaysCoerce.git] / lib / MooseX / AlwaysCoerce.pm
CommitLineData
7a603ffa 1package MooseX::AlwaysCoerce;
2
3use strict;
4use warnings;
5
ad1917d7 6use namespace::autoclean;
7use Moose ();
2429fb7e 8use MooseX::ClassAttribute ();
ad1917d7 9use Moose::Exporter;
2429fb7e 10use Moose::Util::MetaRole;
ad1917d7 11use Carp;
12
2429fb7e 13Moose::Exporter->setup_import_methods;
ad1917d7 14
7a603ffa 15=head1 NAME
16
17MooseX::AlwaysCoerce - Automatically enable coercions for Moose attributes
18
19=head1 VERSION
20
26d62781 21Version 0.06
7a603ffa 22
23=cut
24
26d62781 25our $VERSION = '0.06';
7a603ffa 26
27=head1 SYNOPSIS
28
29 package MyClass;
30
31 use Moose;
32 use MooseX::AlwaysCoerce;
33 use MyTypeLib 'SomeType';
34
ad1917d7 35 has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added
36
2429fb7e 37 # same, MooseX::ClassAttribute is automatically applied
ad1917d7 38 class_has bar => (is => 'rw', isa => SomeType);
39
40=head1 DESCRIPTION
41
42Have you ever spent an hour or more trying to figure out "WTF, why did my
43coercion not run?" only to find out that you forgot C<< coerce => 1 >> ?
44
45Just load this module in your L<Moose> class and C<< coerce => 1 >> will be
2429fb7e 46enabled for every attribute and class attribute automatically.
ad1917d7 47
44b44091 48Use C<< coerce => 0 >> to disable a coercion explicitly.
49
ad1917d7 50=cut
51
2429fb7e 52{
53 package MooseX::AlwaysCoerce::Role::Meta::Attribute;
54 use namespace::autoclean;
55 use Moose::Role;
56
b058bf61 57 has coerce => (
58 lazy => 1,
59 reader => "should_coerce",
60 default => sub {
61 return 1 if shift->type_constraint->has_coercion;
62 return 0;
63 }
64 );
65
2429fb7e 66
67 package MooseX::AlwaysCoerce::Role::Meta::Class;
68 use namespace::autoclean;
69 use Moose::Role;
b058bf61 70 use Moose::Util::TypeConstraints;
a193e05d 71 use MooseX::ClassAttribute;
2429fb7e 72
73 around add_class_attribute => sub {
74 my $next = shift;
75 my $self = shift;
44b44091 76 my ($what, %opts) = @_;
77
b058bf61 78 my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint($opts{isa});
79 $opts{coerce} = 1 if !exists $opts{coerce} and $type->has_coercion;
44b44091 80
81 $self->$next($what, %opts);
2429fb7e 82 };
ad1917d7 83}
84
e307e391 85my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
a193e05d 86
e307e391 87 install => [ qw(import unimport) ],
a193e05d 88
e307e391 89 class_metaroles => {
90 attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
91 class => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
92 },
a193e05d 93
94 also => ['MooseX::ClassAttribute'],
e307e391 95);
96
2429fb7e 97sub init_meta {
e307e391 98 my ($class, %options) = @_;
2429fb7e 99 my $for_class = $options{for_class};
100
a193e05d 101 # Bring this in only if we are being applied to a
102 # metaclass, but not a metarole.
103 if (Class::MOP::class_of($for_class)->isa('Class::MOP::Class'))
104 {
105 MooseX::ClassAttribute->import({ into => $for_class });
106 }
2429fb7e 107
e307e391 108 # call generated method to do the rest of the work.
109 goto $init_meta;
ad1917d7 110}
7a603ffa 111
112=head1 AUTHOR
113
114Rafael Kitover, C<< <rkitover at cpan.org> >>
115
6b46d35c 116=head1 CONTRIBUTORS
117
118Schwern: Michael G. Schwern <mschwern@cpan.org>
e307e391 119Ether: Karen Etheridge <ether@cpan.org>
6b46d35c 120
7a603ffa 121=head1 BUGS
122
123Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
124the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
125automatically be notified of progress on your bug as I make changes.
126
127=head1 SUPPORT
128
129You can find more information at:
130
131=over 4
132
133=item * RT: CPAN's request tracker
134
135L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
136
137=item * AnnoCPAN: Annotated CPAN documentation
138
139L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
140
141=item * CPAN Ratings
142
143L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
144
145=item * Search CPAN
146
147L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
148
149=back
150
151=head1 ACKNOWLEDGEMENTS
152
153My own stupidity, for inspiring me to write this module.
154
2429fb7e 155Dave Rolsky, for telling me how to do it the L<Moose> way.
156
7a603ffa 157=head1 COPYRIGHT & LICENSE
158
dd11ea45 159Copyright (c) 2009-2010 Rafael Kitover
7a603ffa 160
161This program is free software; you can redistribute it and/or modify it
162under the same terms as Perl itself.
163
164=cut
165
1661; # End of MooseX::AlwaysCoerce