File Coverage

blib/lib/MooseX/AlwaysCoerce.pm
Criterion Covered Total %
statement 37 37 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod 0 1 0.0
total 49 50 98.0


line stmt bran cond sub pod time code
1             package MooseX::AlwaysCoerce; # git description: v0.22-7-gf255031
2             # vim: set ts=8 sts=4 sw=4 tw=115 et :
3             # ABSTRACT: Automatically enable coercions for Moose attributes
4             # KEYWORDS: Moose extension type constraint coerce coercion
5              
6             our $VERSION = '0.23';
7              
8 4     4   1638912 use strict;
  4         11  
  4         122  
9 4     4   21 use warnings;
  4         8  
  4         108  
10              
11 4     4   762 use namespace::autoclean 0.12;
  4         12366  
  4         24  
12 4     4   1296 use MooseX::ClassAttribute 0.24 ();
  4         205329  
  4         105  
13 4     4   28 use Moose::Exporter;
  4         9  
  4         20  
14 4     4   129 use Moose::Util::MetaRole;
  4         8  
  4         173  
15              
16             Moose::Exporter->setup_import_methods;
17              
18             #pod =pod
19             #pod
20             #pod =for stopwords coercions
21             #pod
22             #pod =head1 SYNOPSIS
23             #pod
24             #pod package MyClass;
25             #pod
26             #pod use Moose;
27             #pod use MooseX::AlwaysCoerce;
28             #pod use MyTypeLib 'SomeType';
29             #pod
30             #pod has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added
31             #pod
32             #pod # same, MooseX::ClassAttribute is automatically applied
33             #pod class_has bar => (is => 'rw', isa => SomeType);
34             #pod
35             #pod =head1 DESCRIPTION
36             #pod
37             #pod Have you ever spent an hour or more trying to figure out "Hey, why did my
38             #pod coercion not run?" only to find out that you forgot C<< coerce => 1 >> ?
39             #pod
40             #pod Just load this module in your L<Moose> class and C<< coerce => 1 >> will be
41             #pod enabled for every attribute and class attribute automatically.
42             #pod
43             #pod Use C<< coerce => 0 >> to disable a coercion explicitly.
44             #pod
45             #pod =cut
46              
47             {
48             package # hide from PAUSE
49             MooseX::AlwaysCoerce::Role::Meta::Attribute;
50 4     4   18 use namespace::autoclean;
  4         10  
  4         17  
51 4     4   229 use Moose::Role;
  4         21  
  4         16  
52              
53             around should_coerce => sub {
54             my $orig = shift;
55             my $self = shift;
56              
57             my $current_val = $self->$orig(@_);
58              
59             return $current_val if defined $current_val;
60              
61             return 1 if $self->type_constraint && $self->type_constraint->has_coercion;
62             return 0;
63             };
64              
65             package # hide from PAUSE
66             MooseX::AlwaysCoerce::Role::Meta::Class;
67 4     4   18014 use namespace::autoclean;
  4         10  
  4         17  
68 4     4   208 use Moose::Role;
  4         8  
  4         15  
69 4     4   18132 use Moose::Util::TypeConstraints ();
  4         9  
  4         400  
70              
71             around add_class_attribute => sub {
72             my $next = shift;
73             my $self = shift;
74             my ($what, %opts) = @_;
75              
76             if (exists $opts{isa}) {
77             my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint($opts{isa});
78             $opts{coerce} = 1 if not exists $opts{coerce} and $type->has_coercion;
79             }
80              
81             $self->$next($what, %opts);
82             };
83             }
84              
85             my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
86              
87             install => [ qw(import unimport) ],
88              
89             class_metaroles => {
90             attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
91             class => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
92             },
93              
94             role_metaroles => {
95             (Moose->VERSION >= 1.9900
96             ? (applied_attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'])
97             : ()),
98             role => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
99             }
100             );
101              
102             sub init_meta {
103 3     3 0 257 my ($class, %options) = @_;
104 3         7 my $for_class = $options{for_class};
105              
106 3         14 MooseX::ClassAttribute->import({ into => $for_class });
107              
108             # call generated method to do the rest of the work.
109 3         521379 goto $init_meta;
110             }
111              
112             1;
113              
114             __END__
115              
116             =pod
117              
118             =encoding UTF-8
119              
120             =head1 NAME
121              
122             MooseX::AlwaysCoerce - Automatically enable coercions for Moose attributes
123              
124             =head1 VERSION
125              
126             version 0.23
127              
128             =head1 SYNOPSIS
129              
130             package MyClass;
131              
132             use Moose;
133             use MooseX::AlwaysCoerce;
134             use MyTypeLib 'SomeType';
135              
136             has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added
137              
138             # same, MooseX::ClassAttribute is automatically applied
139             class_has bar => (is => 'rw', isa => SomeType);
140              
141             =head1 DESCRIPTION
142              
143             Have you ever spent an hour or more trying to figure out "Hey, why did my
144             coercion not run?" only to find out that you forgot C<< coerce => 1 >> ?
145              
146             Just load this module in your L<Moose> class and C<< coerce => 1 >> will be
147             enabled for every attribute and class attribute automatically.
148              
149             Use C<< coerce => 0 >> to disable a coercion explicitly.
150              
151             =for stopwords coercions
152              
153             =for Pod::Coverage init_meta
154              
155             =head1 ACKNOWLEDGEMENTS
156              
157             My own stupidity, for inspiring me to write this module.
158              
159             =for stopwords Rolsky
160              
161             Dave Rolsky, for telling me how to do it the L<Moose> way.
162              
163             =head1 SUPPORT
164              
165             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-AlwaysCoerce>
166             (or L<bug-MooseX-AlwaysCoerce@rt.cpan.org|mailto:bug-MooseX-AlwaysCoerce@rt.cpan.org>).
167              
168             There is also a mailing list available for users of this distribution, at
169             L<http://lists.perl.org/list/moose.html>.
170              
171             There is also an irc channel available for users of this distribution, at
172             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
173              
174             =head1 AUTHOR
175              
176             Rafael Kitover <rkitover@cpan.org>
177              
178             =head1 CONTRIBUTORS
179              
180             =for stopwords Karen Etheridge Jesse Luehrs Michael G. Schwern
181              
182             =over 4
183              
184             =item *
185              
186             Karen Etheridge <ether@cpan.org>
187              
188             =item *
189              
190             Jesse Luehrs <doy@tozt.net>
191              
192             =item *
193              
194             Michael G. Schwern <schwern@pobox.com>
195              
196             =back
197              
198             =head1 COPYRIGHT AND LICENSE
199              
200             This software is copyright (c) 2009 by Rafael Kitover <rkitover@cpan.org>.
201              
202             This is free software; you can redistribute it and/or modify it under
203             the same terms as the Perl 5 programming language system itself.
204              
205             =cut