add failing tests for types without coercion, add Schwern to CONTRIBUTORS
[gitmo/MooseX-AlwaysCoerce.git] / lib / MooseX / AlwaysCoerce.pm
1 package MooseX::AlwaysCoerce;
2
3 use strict;
4 use warnings;
5
6 use namespace::autoclean;
7 use Moose ();
8 use MooseX::ClassAttribute ();
9 use Moose::Exporter;
10 use Moose::Util::MetaRole;
11 use Carp;
12
13 Moose::Exporter->setup_import_methods;
14
15 =head1 NAME
16
17 MooseX::AlwaysCoerce - Automatically enable coercions for Moose attributes
18
19 =head1 VERSION
20
21 Version 0.04
22
23 =cut
24
25 our $VERSION = '0.04';
26
27 =head1 SYNOPSIS
28
29     package MyClass;
30
31     use Moose;
32     use MooseX::AlwaysCoerce;
33     use MyTypeLib 'SomeType';
34
35     has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added
36
37     # same, MooseX::ClassAttribute is automatically applied
38     class_has bar => (is => 'rw', isa => SomeType);
39
40 =head1 DESCRIPTION
41
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 >> ?
44
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.
47
48 Use C<< coerce => 0 >> to disable a coercion explicitly.
49
50 =cut
51
52 {
53     package MooseX::AlwaysCoerce::Role::Meta::Attribute;
54     use namespace::autoclean;
55     use Moose::Role;
56
57     has coerce => (is => 'rw', default => 1);
58
59     package MooseX::AlwaysCoerce::Role::Meta::Class;
60     use namespace::autoclean;
61     use Moose::Role;
62
63     around add_class_attribute => sub {
64         my $next = shift;
65         my $self = shift;
66         my ($what, %opts) = @_;
67
68         $opts{coerce} = 1 unless exists $opts{coerce};
69
70         $self->$next($what, %opts);
71     };
72 }
73
74 sub init_meta {
75     shift;
76     my %options = @_;
77     my $for_class = $options{for_class};
78
79     MooseX::ClassAttribute->import({ into => $for_class });
80
81     Moose::Util::MetaRole::apply_metaclass_roles(
82         for_class => $for_class,
83         attribute_metaclass_roles =>
84             ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
85         metaclass_roles =>
86             ['MooseX::AlwaysCoerce::Role::Meta::Class'],
87     );
88
89     return $for_class->meta;
90 }
91
92 =head1 AUTHOR
93
94 Rafael Kitover, C<< <rkitover at cpan.org> >>
95
96 =head1 CONTRIBUTORS
97
98 Schwern: Michael G. Schwern <mschwern@cpan.org>
99
100 =head1 BUGS
101
102 Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
103 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>.  I will be notified, and then you'll
104 automatically be notified of progress on your bug as I make changes.
105
106 =head1 SUPPORT
107
108 You can find more information at:
109
110 =over 4
111
112 =item * RT: CPAN's request tracker
113
114 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
115
116 =item * AnnoCPAN: Annotated CPAN documentation
117
118 L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
119
120 =item * CPAN Ratings
121
122 L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
123
124 =item * Search CPAN
125
126 L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
127
128 =back
129
130 =head1 ACKNOWLEDGEMENTS
131
132 My own stupidity, for inspiring me to write this module.
133
134 Dave Rolsky, for telling me how to do it the L<Moose> way.
135
136 =head1 COPYRIGHT & LICENSE
137
138 Copyright (c) 2009 Rafael Kitover
139
140 This program is free software; you can redistribute it and/or modify it
141 under the same terms as Perl itself.
142
143 =cut
144
145 1; # End of MooseX::AlwaysCoerce