rewritten using metaroles, the Moose way
[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
2429fb7e 21Version 0.02
7a603ffa 22
23=cut
24
2429fb7e 25our $VERSION = '0.02';
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
48=cut
49
2429fb7e 50{
51 package MooseX::AlwaysCoerce::Role::Meta::Attribute;
52 use namespace::autoclean;
53 use Moose::Role;
54
55 has coerce => (is => 'rw', default => 1);
56
57 package MooseX::AlwaysCoerce::Role::Meta::Class;
58 use namespace::autoclean;
59 use Moose::Role;
60
61 around add_class_attribute => sub {
62 my $next = shift;
63 my $self = shift;
64 $self->$next(@_, coerce => 1);
65 };
ad1917d7 66}
67
2429fb7e 68sub init_meta {
69 shift;
70 my %options = @_;
71 my $for_class = $options{for_class};
72
73 MooseX::ClassAttribute->import({ into => $for_class });
74
75 Moose::Util::MetaRole::apply_metaclass_roles(
76 for_class => $for_class,
77 attribute_metaclass_roles =>
78 ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
79 metaclass_roles =>
80 ['MooseX::AlwaysCoerce::Role::Meta::Class'],
81 );
82
83 return $for_class->meta;
ad1917d7 84}
7a603ffa 85
86=head1 AUTHOR
87
88Rafael Kitover, C<< <rkitover at cpan.org> >>
89
90=head1 BUGS
91
92Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
93the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
94automatically be notified of progress on your bug as I make changes.
95
96=head1 SUPPORT
97
98You can find more information at:
99
100=over 4
101
102=item * RT: CPAN's request tracker
103
104L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
105
106=item * AnnoCPAN: Annotated CPAN documentation
107
108L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
109
110=item * CPAN Ratings
111
112L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
113
114=item * Search CPAN
115
116L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
117
118=back
119
120=head1 ACKNOWLEDGEMENTS
121
122My own stupidity, for inspiring me to write this module.
123
2429fb7e 124Dave Rolsky, for telling me how to do it the L<Moose> way.
125
7a603ffa 126=head1 COPYRIGHT & LICENSE
127
128Copyright (c) 2009 Rafael Kitover
129
130This program is free software; you can redistribute it and/or modify it
131under the same terms as Perl itself.
132
133=cut
134
1351; # End of MooseX::AlwaysCoerce