isa is not a required option -- dont do anything extra if class attr was not defined...
[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
62e7cdef 21Version 0.07
7a603ffa 22
23=cut
24
62e7cdef 25our $VERSION = '0.07';
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
62e7cdef 57 around should_coerce => sub {
58 my $orig = shift;
59 my $self = shift;
60
61 my $current_val = $self->$orig(@_);
b058bf61 62
90d2721a 63 return $current_val if defined $current_val;
64
62e7cdef 65 return 1 if $self->type_constraint->has_coercion;
66 return 0;
67 };
2429fb7e 68
69 package MooseX::AlwaysCoerce::Role::Meta::Class;
70 use namespace::autoclean;
71 use Moose::Role;
b058bf61 72 use Moose::Util::TypeConstraints;
a193e05d 73 use MooseX::ClassAttribute;
2429fb7e 74
75 around add_class_attribute => sub {
76 my $next = shift;
77 my $self = shift;
44b44091 78 my ($what, %opts) = @_;
79
b058bf61 80 my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint($opts{isa});
8210a2f9 81 $opts{coerce} = 1 if $type and not exists $opts{coerce} and $type->has_coercion;
44b44091 82
83 $self->$next($what, %opts);
2429fb7e 84 };
ad1917d7 85}
86
e307e391 87my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
a193e05d 88
e307e391 89 install => [ qw(import unimport) ],
a193e05d 90
e307e391 91 class_metaroles => {
92 attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
93 class => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
94 },
a193e05d 95
62e7cdef 96 role_metaroles => {
97 # applied_attribute should be available soon, for now roles are borked
98 # applied_attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
99 role => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
100 }
e307e391 101);
102
2429fb7e 103sub init_meta {
e307e391 104 my ($class, %options) = @_;
2429fb7e 105 my $for_class = $options{for_class};
106
62e7cdef 107 MooseX::ClassAttribute->import({ into => $for_class });
2429fb7e 108
e307e391 109 # call generated method to do the rest of the work.
110 goto $init_meta;
ad1917d7 111}
7a603ffa 112
113=head1 AUTHOR
114
115Rafael Kitover, C<< <rkitover at cpan.org> >>
116
6b46d35c 117=head1 CONTRIBUTORS
118
119Schwern: Michael G. Schwern <mschwern@cpan.org>
e307e391 120Ether: Karen Etheridge <ether@cpan.org>
6b46d35c 121
7a603ffa 122=head1 BUGS
123
124Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
125the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
126automatically be notified of progress on your bug as I make changes.
127
128=head1 SUPPORT
129
130You can find more information at:
131
132=over 4
133
134=item * RT: CPAN's request tracker
135
136L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
137
138=item * AnnoCPAN: Annotated CPAN documentation
139
140L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
141
142=item * CPAN Ratings
143
144L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
145
146=item * Search CPAN
147
148L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
149
150=back
151
152=head1 ACKNOWLEDGEMENTS
153
154My own stupidity, for inspiring me to write this module.
155
2429fb7e 156Dave Rolsky, for telling me how to do it the L<Moose> way.
157
7a603ffa 158=head1 COPYRIGHT & LICENSE
159
dd11ea45 160Copyright (c) 2009-2010 Rafael Kitover
7a603ffa 161
162This program is free software; you can redistribute it and/or modify it
163under the same terms as Perl itself.
164
165=cut
166
1671; # End of MooseX::AlwaysCoerce