lib/MooseX/Role/Hashable.pm

filter: Statementstmt 100% 73 / 73 Branchbran 66.6% 4 / 6 Conditioncond 66.6% 12 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 18 / 18 Podpod 33.3% 1 / 3 totaltotal 87.5% 112 / 128 CC 13 SCAR 8.3
CC
13
Cov
92%
__ANON__
4.1
__ANON__
3.5
__ANON__
3.5
CRAP 2.3
↑ Summary
1 package MooseX::Role::Hashable;
2  
3 =head1 NAME
4  
5 MooseX::Role::Hashable - Transform the object into a hash
6  
7 =cut
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Set::Functional qw{difference_by setify_by};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 VERSION
17  
18 Version 1.04
19  
20 =cut
21  
22 2 our $VERSION = '1.04';
23  
24 =head1 SYNOPSIS
25  
26 This module adds a single method to an object to convert it into a simple hash.
27 This is meant to act as the inverse function of I<new>, provided nothing too
28 crazy is going on during initialization. If the class is made immutable, the
29 optimizer will precalculate the extracted attributes for a signifcant speed up.
30  
31 Example usage:
32  
33 package Foo;
34 use Moose;
35 use MooseX::Role::Hashable;
36  
37 has field1 => (is => 'rw');
38 has field2 => (is => 'ro');
39 has field3 => (is => 'bare');
40 has _field4 => (is => 'rw', init_arg => 'field4');
41  
42 __PACKAGE__->meta->make_immutable;
43  
44 package main;
45  
46 my $foo = Foo->new(field1 => 'val1', field2 => 'val2', field3 => 'val3', field4 => 'val4');
47 $foo->as_hash;
48 # => {field1 => 'val1', field2 => 'val2', field3 => 'val3', field4 => 'val4'}
49  
50 =cut
51  
52 2 do {
53 2 my $package = __PACKAGE__;
54 package
55 Moose::Meta::Class;
56  
57 2 ▶ use Class::Method::Modifiers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 6 ▶ Class::Method::Modifiers::after(make_immutable => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
60 6 my $meta = shift;
61 6 my $class = $meta->name;
62 6 ▶ $class->optimize_as_hash
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("does") and $class->does($package) 6 0 6
Condition33%1/3
$class->can("does") and $class->does($package)
!ll&&!rl&&r
006
MC/DC0%0/2
$class->can("does") and $class->does($package)
$class->can("does") $class->does($package)
Truth table33%1/3
A and B
A= $class->can("does")B= $class->does($package)
ABresult
0X0
100
111
63 if $class->can('does')
64 && $class->does($package);
65 2 });
66  
67 1 ▶ Class::Method::Modifiers::before(make_mutable => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
68 1 my $meta = shift;
69 1 my $class = $meta->name;
70 1 ▶ $class->deoptimize_as_hash
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("does") and $class->does($package) 1 0 1
Condition33%1/3
$class->can("does") and $class->does($package)
!ll&&!rl&&r
001
MC/DC0%0/2
$class->can("does") and $class->does($package)
$class->can("does") $class->does($package)
Truth table33%1/3
A and B
A= $class->can("does")B= $class->does($package)
ABresult
0X0
100
111
71 if $class->can('does')
72 && $class->does($package);
73 2 });
74 };
75  
76 =head1 METHODS
77  
78 =cut
79  
80 =head2 as_hash
81  
82 Transform the object into a hash of attribute-value pairs. All attributes,
83 including those without a reader, are extracted. Attributes whose initial
84 arguments differ from their name will appear using the initialization argument.
85 Attributes which can' be initialized will be ignored. Reference values will
86 perform a shallow copy.
87  
88 =cut
89  
90 2 my %CLASS_TO_ATTRIBUTES;
91 29 ▶my $extract_attributes_ref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 return
93 #We only want one copy of each attribute
94 207 ▶ setify_by { $_->name }
Subroutine __ANON__: called CC=1 SCAR=0.0
95 #Manually taverse all attributes, get_all_attributes doesn't update
96 #with superclass changes afte subclass immutability
97 29 map { my $meta = $_->meta; map { $meta->get_attribute($_) } $meta->get_attribute_list }
98 #Make sure attribute overrides take precedence
99 reverse $_[0]->meta->linearized_isa;
100 2 };
101 29 ▶my $extract_ignored_ref = sub { grep { ! $_->has_init_arg } @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
102 29 ▶my $extract_translated_ref = sub { map { ($_->name => $_->init_arg) } grep { $_->has_init_arg && $_->init_arg ne $_->name } @_ };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%3/3
$_->has_init_arg && $_->init_arg ne $_->name
!ll&&!rl&&r
1416814
MC/DC100%2/2
$_->has_init_arg && $_->init_arg ne $_->name
$_->has_init_arg $_->init_arg ne $_->name
Truth table100%3/3
A && B
A= $_->has_init_argB= $_->init_arg ne $_->name
ABresult
0X0
100
111
103 29 ▶my $extract_uninitialized_ref = sub { grep { ! ($_->is_required || ! $_->is_lazy && ($_->has_builder || $_->has_default)) } @_ };
Subroutine __ANON__: called CC=4 SCAR=14.0
Condition77%7/9
$_->is_required || !$_->is_lazy && ($_->has_builder || $_->has_default)
l!l&&r!l&&!r
051145
not $_->is_lazy and $_->has_builder || $_->has_default
!ll&&!rl&&r
578851
$_->has_builder or $_->has_default
l!l&&r!l&&!r
05188
MC/DC50%2/4
$_->is_required || !$_->is_lazy && ($_->has_builder || $_->has_default)
$_->is_required not $_->is_lazy $_->has_builder $_->has_default
Truth table60%3/5
A || not(B) and C or D
A= $_->is_requiredB= not $_->is_lazyC= $_->has_builderD= $_->has_default
ABCDresult
1XXX1
011X1
01011
00XX0
01000
104 29 ▶my $prepare_attributes_ref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
105 29 my @ignored = $extract_ignored_ref->(@_);
106 29 my %translated = $extract_translated_ref->(@_);
107 29 my @uninitialized = $extract_uninitialized_ref->(@_);
108 return (
109 14 [map { $_->name } @ignored],
110 \%translated,
111 29 ▶ [difference_by { $_->name } \@uninitialized, \@ignored],
Subroutine __ANON__: called CC=1 SCAR=0.0
112 )
113 2 };
114  
115 40 ▶sub as_hash {
Subroutine as_hash: called CC=3 SCAR=11.0
Pod documented
116 40 my $self = shift;
117  
118 40 my $cached_attributes = $CLASS_TO_ATTRIBUTES{ref $self};
119 my ($ignored_attributes, $translated_attributes, $uninitialized_attributes) = $cached_attributes
120 40 ▶ ? @{$cached_attributes}{qw{ignored translated uninitialized}}
Branch: 1 branch
BranchTrueFalseTotal
$cached_attributes ? : 19 21 40
121 : $prepare_attributes_ref->($extract_attributes_ref->($self))
122 ;
123  
124 40 my %copy = %$self;
125 $copy{$_->name} = $_->get_value($self)
126 40 for grep { ! exists $copy{$_->name} } @$uninitialized_attributes;
127 40 @copy{values %$translated_attributes} = delete @copy{keys %$translated_attributes};
128 40 delete @copy{@$ignored_attributes};
129  
130 40 return \%copy;
131 }
132  
133 8 ▶sub optimize_as_hash {
Subroutine optimize_as_hash: called CC=2 SCAR=6.9
Pod undocumented
134 8 my $class = shift;
135  
136 #Precalculate the attributes
137 8 @{$CLASS_TO_ATTRIBUTES{$class}}{qw{ ignored translated uninitialized }} =
138 $prepare_attributes_ref->($extract_attributes_ref->($class));
139  
140 8 $_->optimize_as_hash for $class->meta->direct_subclasses;
141  
142 8 return;
143 }
144  
145 1 ▶sub deoptimize_as_hash {
Subroutine deoptimize_as_hash: called CC=2 SCAR=6.9
Pod undocumented
146 1 my $class = shift;
147  
148 1 delete $CLASS_TO_ATTRIBUTES{$class};
149  
150 1 $_->deoptimize_as_hash for $class->meta->direct_subclasses;
151  
152 1 return;
153 }
154  
155 =head1 AUTHOR
156  
157 Aaron Cohen, C<< <aarondcohen at gmail.com> >>
158  
159 Special thanks to:
160 L<Dibin Pookombil|https://github.com/dibinp>
161  
162 =head1 ACKNOWLEDGEMENTS
163  
164 This module was made possible by L<Shutterstock|http://www.shutterstock.com/>
165 (L<@ShutterTech|https://twitter.com/ShutterTech>). Additional open source
166 projects from Shutterstock can be found at
167 L<code.shutterstock.com|http://code.shutterstock.com/>.
168  
169 =head1 BUGS
170  
171 Please report any bugs or feature requests to C<bug-MooseX-Role-Hashable at rt.cpan.org>, or through
172 the web interface at L<https://github.com/aarondcohen/perl-moosex-role-hashable/issues>. I will
173 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
174  
175 =head1 SUPPORT
176  
177 You can find documentation for this module with the perldoc command.
178  
179 perldoc MooseX::Role::Hashable
180  
181 You can also look for information at:
182  
183 =over 4
184  
185 =item * Official GitHub Repo
186  
187 L<https://github.com/aarondcohen/perl-moosex-role-hashable>
188  
189 =item * GitHub's Issue Tracker (report bugs here)
190  
191 L<https://github.com/aarondcohen/perl-moosex-role-hashable/issues>
192  
193 =item * CPAN Ratings
194  
195 L<http://cpanratings.perl.org/d/MooseX-Role-Hashable>
196  
197 =item * Official CPAN Page
198  
199 L<http://search.cpan.org/dist/MooseX-Role-Hashable/>
200  
201 =back
202  
203 =head1 LICENSE AND COPYRIGHT
204  
205 Copyright 2013,2014 Aaron Cohen.
206  
207 This program is free software; you can redistribute it and/or modify it
208 under the terms of either: the GNU General Public License as published
209 by the Free Software Foundation; or the Artistic License.
210  
211 See http://dev.perl.org/licenses/ for more information.
212  
213 =cut
214  
215 2 1; # End of MooseX::Role::Hashable
↑ Summary