1 package MooseX::AlwaysCoerce;
6 use namespace::autoclean 0.12;
8 use MooseX::ClassAttribute 0.24 ();
10 use Moose::Util::MetaRole;
13 Moose::Exporter->setup_import_methods;
19 MooseX::AlwaysCoerce - Automatically enable coercions for Moose attributes
26 use MooseX::AlwaysCoerce;
27 use MyTypeLib 'SomeType';
29 has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added
31 # same, MooseX::ClassAttribute is automatically applied
32 class_has bar => (is => 'rw', isa => SomeType);
36 Have you ever spent an hour or more trying to figure out "WTF, why did my
37 coercion not run?" only to find out that you forgot C<< coerce => 1 >> ?
39 Just load this module in your L<Moose> class and C<< coerce => 1 >> will be
40 enabled for every attribute and class attribute automatically.
42 Use C<< coerce => 0 >> to disable a coercion explicitly.
47 package MooseX::AlwaysCoerce::Role::Meta::Attribute;
48 use namespace::autoclean;
51 around should_coerce => sub {
55 my $current_val = $self->$orig(@_);
57 return $current_val if defined $current_val;
59 return 1 if $self->type_constraint && $self->type_constraint->has_coercion;
63 package MooseX::AlwaysCoerce::Role::Meta::Class;
64 use namespace::autoclean;
66 use Moose::Util::TypeConstraints;
68 around add_class_attribute => sub {
71 my ($what, %opts) = @_;
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;
78 $self->$next($what, %opts);
82 my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
84 install => [ qw(import unimport) ],
87 attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
88 class => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
92 (Moose->VERSION >= 1.9900
93 ? (applied_attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'])
95 role => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
100 my ($class, %options) = @_;
101 my $for_class = $options{for_class};
103 MooseX::ClassAttribute->import({ into => $for_class });
105 # call generated method to do the rest of the work.
110 # vim:et sts=4 sw=4 tw=0:
118 Rafael Kitover, C<< <rkitover at cpan.org> >>
122 Schwern: Michael G. Schwern <mschwern@cpan.org>
123 Ether: Karen Etheridge <ether@cpan.org>
127 Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
128 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
129 automatically be notified of progress on your bug as I make changes.
133 You can find more information at:
137 =item * RT: CPAN's request tracker
139 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
141 =item * AnnoCPAN: Annotated CPAN documentation
143 L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
147 L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
151 L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
155 =head1 ACKNOWLEDGEMENTS
157 My own stupidity, for inspiring me to write this module.
159 Dave Rolsky, for telling me how to do it the L<Moose> way.
161 =head1 COPYRIGHT & LICENSE
163 Copyright (c) 2009-2010 Rafael Kitover
165 This program is free software; you can redistribute it and/or modify it
166 under the same terms as Perl itself.