File Coverage

blib/lib/MooseX/UndefTolerant/Attribute.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package MooseX::UndefTolerant::Attribute;
2              
3             our $VERSION = '0.21';
4              
5 7     7   140485 use Moose::Role;
  7         227483  
  7         26  
6 7     7   29002 use namespace::autoclean;
  7         39543  
  7         28  
7              
8             around('initialize_instance_slot', sub {
9             my $orig = shift;
10             my $self = shift;
11             my ($meta_instance, $instance, $params) = @_;
12              
13             my $key_name = $self->init_arg;
14              
15             # If our parameter passed in was undef, remove it from the parameter list...
16             # but leave the value unscathed if the attribute's type constraint can
17             # handle undef (or doesn't have one, which implicitly means it can)
18             if (defined $key_name and not defined($params->{$key_name}))
19             {
20             my $type_constraint = $self->type_constraint;
21             if ($type_constraint and not $type_constraint->check(undef))
22             {
23             delete $params->{$key_name};
24             }
25             }
26              
27             # Invoke the real init, as the above line cleared the undef param value
28             $self->$orig(@_)
29             });
30              
31             1;
32              
33             # ABSTRACT: Make your attribute(s) tolerant to undef initialization
34              
35             __END__
36              
37             =pod
38              
39             =encoding UTF-8
40              
41             =head1 NAME
42              
43             MooseX::UndefTolerant::Attribute - Make your attribute(s) tolerant to undef initialization
44              
45             =head1 VERSION
46              
47             version 0.21
48              
49             =head1 SYNOPSIS
50              
51             package My:Class;
52             use Moose;
53              
54             use MooseX::UndefTolerant::Attribute;
55              
56             has 'bar' => (
57             traits => [ qw(MooseX::UndefTolerant::Attribute)],
58             is => 'ro',
59             isa => 'Num',
60             predicate => 'has_bar'
61             );
62              
63             # Meanwhile, under the city...
64              
65             # Doesn't explode
66             my $class = My::Class->new(bar => undef);
67             $class->has_bar # False!
68              
69             =head1 DESCRIPTION
70              
71             Applying this trait to your attribute makes its initialization tolerant of
72             of undef. If you specify the value of undef to any of the attributes they
73             will not be initialized (or will be set to the default, if applicable).
74             Effectively behaving as if you had not provided a value at all.
75              
76             =head1 SUPPORT
77              
78             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-UndefTolerant>
79             (or L<bug-MooseX-UndefTolerant@rt.cpan.org|mailto:bug-MooseX-UndefTolerant@rt.cpan.org>).
80              
81             There is also a mailing list available for users of this distribution, at
82             L<http://lists.perl.org/list/moose.html>.
83              
84             There is also an irc channel available for users of this distribution, at
85             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
86              
87             =head1 AUTHOR
88              
89             Cory G Watson <gphat at cpan.org>
90              
91             =head1 COPYRIGHT AND LICENSE
92              
93             This software is copyright (c) 2011 by Cory G Watson.
94              
95             This is free software; you can redistribute it and/or modify it under
96             the same terms as the Perl 5 programming language system itself.
97              
98             =cut