lib/MooseX/Role/UnsafeConstructable.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 100% 10 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.3% 12 / 13 Podpod 100% 3 / 3 totaltotal 91.6% 66 / 72 CC 7 SCAR 8.4
CC
7
Cov
94%
declare_unsafe_class
5.0
unsafe_class
2.3
__ANON__
2.0
CRAP 2.3
↑ Summary
1 package MooseX::Role::UnsafeConstructable;
2  
3 =head1 NAME
4  
5 MooseX::Role::UnsafeConstructable - construct an object without type-checks
6  
7 =cut
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Class::MOP::Attribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Class::MOP::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 VERSION
19  
20 Version 0.03
21  
22 =cut
23  
24 4 our $VERSION = '0.03';
25  
26 =head1 SYNOPSIS
27  
28 This module provides the method I<unsafe_new> which allows the caller to
29 construct an instance while bypassing all access controls and type constraints.
30 This is useful when the caller knows and trusts the source of the
31 initialization data and doesn't want to choose between performance in a corner
32 case and data integrity of the overall system.
33  
34 Example usage:
35  
36 package Foo;
37 use Moose;
38 with 'MooseX::Role::UnsafeConstructable';
39  
40 has field => (is => 'ro', isa => 'HashRef[ArrayRef[Str]]');
41  
42 __PACKAGE__->meta->make_immutable;
43  
44 package main;
45  
46 my $foo = Foo->unsafe_new(field => {this => [qw{that and another}]});
47 # => is a Foo, but instantiated faster
48  
49 =cut
50  
51 4 do {
52 4 my $moose_meta = Moose::Meta::Class->meta;
53 4 $moose_meta->make_mutable;
54 7 ▶ $moose_meta->add_after_method_modifier('make_immutable', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
55 7 my $meta = shift;
56 7 ▶ $meta->name->declare_unsafe_class if $meta->name->does(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $meta->name->does(__PACKAGE__) 4 3 7
57 4 });
58 4 $moose_meta->make_immutable;
59 };
60  
61  
62 =head1 METHODS
63  
64 =cut
65  
66 =head2 declare_unsafe_class
67  
68 Declare the shadow class to be used for unsafe instantiation. Any class using
69 MooseX::Role::UnsafeConstructable role will call declare_unsafe_class
70 automatically when made immutable. Otherwise, declare_unsafe_class must be
71 called after the original class is fully declared to ensure all attributes are
72 properly shadowed.
73  
74 For each attribute, the shadow class will preserve:
75 name, default, builder, and init_arg. All other options will be dropped. This
76 should provide behavior that mostly matches normal instantiation, with the one
77 caveat that any field declared uninitializable (with init_arg => undef) can now
78 be set. This is considered a feature, not a bug.
79  
80 =cut
81  
82 6 ▶sub declare_unsafe_class {
Subroutine declare_unsafe_class: called CC=5 SCAR=16.1
Pod documented
83 6 my $class = shift;
84  
85 6 ▶ return if Class::MOP::class_of($class->unsafe_class);
Branch: 1 branch
BranchTrueFalseTotal
if Class::MOP::class_of($class->unsafe_class) 1 5 6
86  
87 Class::MOP::Class->create($class->unsafe_class => (
88 0 ▶ methods => {promote => sub { bless shift, $class }},
Subroutine promote: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
89 5 ▶ attributes => [map { Class::MOP::Attribute->new(
Branch: 3 branches
BranchTrueFalseTotal
$_->has_default ? : 2 9 11
$_->has_builder ? : 1 10 11
$_->has_init_arg ? : 10 1 11
90 $_->name,
91 $_->has_default ? (default => $_->default) : (),
92 $_->has_builder ? (builder => $class . '::' . $_->builder) : (),
93 $_->has_init_arg ? (init_arg => $_->init_arg) : (),
94 ) } $class->meta->get_all_attributes],
95 ));
96 5 $class->unsafe_class->meta->make_immutable;
97  
98 5 return;
99 }
100  
101 =head2 unsafe_class
102  
103 Return the name of the shadow class used for unsafe instantiation.
104  
105 =cut
106  
107 43 ▶sub unsafe_class { my $class = shift; (ref $class || $class) . '::Unsafe' }
Subroutine unsafe_class: called CC=2 SCAR=8.1
Pod documented
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0430
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
108  
109 =head2 unsafe_new
110  
111 Instantiate the class, bypassing access and type constraints.
112  
113 =cut
114  
115 15 ▶sub unsafe_new { shift->unsafe_class->new(@_)->promote }
Subroutine unsafe_new: called CC=1 SCAR=0.0
Pod documented
116  
117 =head1 AUTHOR
118  
119 Aaron Cohen, C<< <aarondcohen at gmail.com> >>
120  
121 =head1 ACKNOWLEDGEMENTS
122  
123 This module was made possible by L<Shutterstock|http://www.shutterstock.com/>
124 (L<@ShutterTech|https://twitter.com/ShutterTech>). Additional open source
125 projects from Shutterstock can be found at
126 L<code.shutterstock.com|http://code.shutterstock.com/>.
127  
128 =head1 BUGS
129  
130 Please report any bugs or feature requests to C<bug-MooseX-Role-UnsafeConstructable at rt.cpan.org>, or through
131 the web interface at L<https://github.com/aarondcohen/perl-moosex-role-unsafeconstructable/issues>. I will
132 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
133  
134 =head1 SUPPORT
135  
136 You can find documentation for this module with the perldoc command.
137  
138 perldoc MooseX::Role::UnsafeConstructable
139  
140 You can also look for information at:
141  
142 =over 4
143  
144 =item * Official GitHub Repo
145  
146 L<https://github.com/aarondcohen/perl-moosex-role-unsafeconstructable>
147  
148 =item * GitHub's Issue Tracker (report bugs here)
149  
150 L<https://github.com/aarondcohen/perl-moosex-role-unsafeconstructable/issues>
151  
152 =item * CPAN Ratings
153  
154 L<http://cpanratings.perl.org/d/MooseX-Role-UnsafeConstructable>
155  
156 =item * Official CPAN Page
157  
158 L<http://search.cpan.org/dist/MooseX-Role-UnsafeConstructable/>
159  
160 =back
161  
162 =head1 LICENSE AND COPYRIGHT
163  
164 Copyright 2014 Aaron Cohen.
165  
166 This program is free software; you can redistribute it and/or modify it
167 under the terms of either: the GNU General Public License as published
168 by the Free Software Foundation; or the Artistic License.
169  
170 See http://dev.perl.org/licenses/ for more information.
171  
172 =cut
173  
174 4 1; # End of MooseX::Role::UnsafeConstructable
↑ Summary