1 package MooseX::AlwaysCoerce;
2 # ABSTRACT: Automatically enable coercions for Moose attributes
7 use namespace::autoclean 0.12;
9 use MooseX::ClassAttribute 0.24 ();
11 use Moose::Util::MetaRole;
14 Moose::Exporter->setup_import_methods;
18 =for stopwords coercions
25 use MooseX::AlwaysCoerce;
26 use MyTypeLib 'SomeType';
28 has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added
30 # same, MooseX::ClassAttribute is automatically applied
31 class_has bar => (is => 'rw', isa => SomeType);
35 Have you ever spent an hour or more trying to figure out "Hey, why did my
36 coercion not run?" only to find out that you forgot C<< coerce => 1 >> ?
38 Just load this module in your L<Moose> class and C<< coerce => 1 >> will be
39 enabled for every attribute and class attribute automatically.
41 Use C<< coerce => 0 >> to disable a coercion explicitly.
46 package MooseX::AlwaysCoerce::Role::Meta::Attribute;
47 use namespace::autoclean;
50 around should_coerce => sub {
54 my $current_val = $self->$orig(@_);
56 return $current_val if defined $current_val;
58 return 1 if $self->type_constraint && $self->type_constraint->has_coercion;
62 package MooseX::AlwaysCoerce::Role::Meta::Class;
63 use namespace::autoclean;
65 use Moose::Util::TypeConstraints;
67 around add_class_attribute => sub {
70 my ($what, %opts) = @_;
72 if (exists $opts{isa}) {
73 my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint($opts{isa});
74 $opts{coerce} = 1 if not exists $opts{coerce} and $type->has_coercion;
77 $self->$next($what, %opts);
81 my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
83 install => [ qw(import unimport) ],
86 attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
87 class => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
91 (Moose->VERSION >= 1.9900
92 ? (applied_attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'])
94 role => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
99 my ($class, %options) = @_;
100 my $for_class = $options{for_class};
102 MooseX::ClassAttribute->import({ into => $for_class });
104 # call generated method to do the rest of the work.
109 # vim:et sts=4 sw=4 tw=0:
117 Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
118 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
119 automatically be notified of progress on your bug as I make changes.
123 You can find more information at:
127 =item * RT: CPAN's request tracker
129 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
131 =for stopwords AnnoCPAN
133 =item * AnnoCPAN: Annotated CPAN documentation
135 L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
139 L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
143 L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
147 =head1 ACKNOWLEDGEMENTS
149 My own stupidity, for inspiring me to write this module.
151 =for stopwords Rolsky
153 Dave Rolsky, for telling me how to do it the L<Moose> way.