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