1 package MooseX::AlwaysCoerce;
6 use namespace::autoclean;
8 use MooseX::ClassAttribute ();
10 use Moose::Util::MetaRole;
13 Moose::Exporter->setup_import_methods;
17 MooseX::AlwaysCoerce - Automatically enable coercions for Moose attributes
25 our $VERSION = '0.06';
32 use MooseX::AlwaysCoerce;
33 use MyTypeLib 'SomeType';
35 has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added
37 # same, MooseX::ClassAttribute is automatically applied
38 class_has bar => (is => 'rw', isa => SomeType);
42 Have you ever spent an hour or more trying to figure out "WTF, why did my
43 coercion not run?" only to find out that you forgot C<< coerce => 1 >> ?
45 Just load this module in your L<Moose> class and C<< coerce => 1 >> will be
46 enabled for every attribute and class attribute automatically.
48 Use C<< coerce => 0 >> to disable a coercion explicitly.
53 package MooseX::AlwaysCoerce::Role::Meta::Attribute;
54 use namespace::autoclean;
59 reader => "should_coerce",
61 return 1 if shift->type_constraint->has_coercion;
67 package MooseX::AlwaysCoerce::Role::Meta::Class;
68 use namespace::autoclean;
70 use Moose::Util::TypeConstraints;
71 use MooseX::ClassAttribute;
73 around add_class_attribute => sub {
76 my ($what, %opts) = @_;
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;
81 $self->$next($what, %opts);
85 my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
87 install => [ qw(import unimport) ],
90 attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
91 class => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
94 also => ['MooseX::ClassAttribute'],
98 my ($class, %options) = @_;
99 my $for_class = $options{for_class};
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'))
105 MooseX::ClassAttribute->import({ into => $for_class });
108 # call generated method to do the rest of the work.
114 Rafael Kitover, C<< <rkitover at cpan.org> >>
118 Schwern: Michael G. Schwern <mschwern@cpan.org>
119 Ether: Karen Etheridge <ether@cpan.org>
123 Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
124 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
125 automatically be notified of progress on your bug as I make changes.
129 You can find more information at:
133 =item * RT: CPAN's request tracker
135 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
137 =item * AnnoCPAN: Annotated CPAN documentation
139 L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
143 L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
147 L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
151 =head1 ACKNOWLEDGEMENTS
153 My own stupidity, for inspiring me to write this module.
155 Dave Rolsky, for telling me how to do it the L<Moose> way.
157 =head1 COPYRIGHT & LICENSE
159 Copyright (c) 2009-2010 Rafael Kitover
161 This program is free software; you can redistribute it and/or modify it
162 under the same terms as Perl itself.
166 1; # End of MooseX::AlwaysCoerce