blib/lib/Marlin/X/Clone.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Marlin::X::Clone;
6  
7 our $AUTHORITY = 'cpan:TOBYINK';
8 our $VERSION = '0.020000';
9  
10 0 ▶use Marlin::Util qw( true false );
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Types::Common qw( -types );
12  
13 use Marlin (
14 -with => 'Marlin::X',
15 method_name => { isa => Str, default => 'clone' },
16 call_build => { isa => Bool, default => true },
17 strict_args => { isa => Bool, default => true },
18 );
19  
20 use B ();
21 use Clone ();
22 use Eval::TypeTiny::CodeAccumulator;
23 use Scalar::Util ();
24  
25 # Is possible to do some sanity checking here.
26 sub BUILD {
27 my $plugin = shift;
28 if ( $plugin->marlin->isa('Marlin::Role') ) {
29 Marlin::Util::_croak "Marlin::X::Clone cannot be applied to roles";
30 }
31 }
32  
33 # This is the main hook Marlin uses to give plugins a chance to do something.
34 # $steps is a list of subs which will be called by Marlin when setting up the
35 # class or role. Plugins can add or remove steps.
36 sub adjust_setup_steps {
37 my $plugin = shift;
38 my $steps = shift;
39
40 # Add an extra step called "Marlin::X::Clone::setup_clone_method".
41 # The callback needs to be fully qualified and start with this
42 # package's class name so that Marlin knows which plugin added it.
43 #
44 # In our case, it doesn't really matter where we add the step, so
45 # we'll just add it at the end.
46 my $callback = sprintf '%s::%s', __PACKAGE__, 'setup_clone_method';
47 push @$steps, $callback;
48 }
49  
50 # When our step is called, it is passed the plugin object and the Marlin
51 # object.
52 sub setup_clone_method {
53 my $plugin = shift;
54 my $marlin = shift;
55
56 # All we're doing here is creating a method called "clone" and
57 # then telling Marlin to export it.
58 my $code = $plugin->_make_clone_method( $marlin );
59 $marlin->export( $plugin->method_name, $code->compile );
60
61 # Marlin offers "export" and "lexport". The former will export methods
62 # into the class/role currently being built. The latter will export a
63 # function lexically into the compiling package (or the caller if lexical
64 # exports are unavailable).
65 #
66 # Use "export" to install methods into the class being built. Use
67 # "lexport" for utility functions, keywords, etc.
68
69 return $marlin;
70 }
71  
72 # This is the guts to the Marlin::X::Clone plugin. It is building up
73 # a coderef as a string using Eval::TypeTiny::CodeAccumulator. Using
74 # CodeAccumulator is just a little prettier than concatenating strings
75 # and doing an `eval` at the end.
76 sub _make_clone_method {
77 my $plugin = shift;
78 my $marlin = shift;
79
80 # Eval::TypeTiny::CodeAccumulator allows you to build up some
81 # Perl source code line-by-line and compile it at the end.
82 my $code = Eval::TypeTiny::CodeAccumulator->new( description => 'clone' );
83 $code->addf( 'sub {' );
84 $code->increase_indent;
85 $code->addf( 'my $self = shift;' );
86 $code->addf( InstanceOf->of( $marlin->this )->inline_assert('$self') );
87 $code->addf( 'my $class = ref $self;' );
88  
89 # ->clone() has been called on a child class which doesn't override clone.
90 # This means it may need to handle additional attributes. So attempt to
91 # create a clone method for the child class!
92 {
93 my $var = $code->add_variable( '$clone_plugin', \$plugin );
94 $code->addf( 'if ( $class ne %s ) {', B::perlstring($marlin->this) );
95 $code->increase_indent;
96 $code->addf( 'my $child_marlin = %s->find_meta( $class ) or %s("$class is not a Marlin class");', ref($marlin), $marlin->_croaker );
97 $code->addf( 'my $child_cloner = %s->_make_clone_method_for_child( $child_marlin );', $var );
98 $code->addf( 'return $self->$child_cloner( @_ );' );
99 $code->decrease_indent;
100 $code->addf( '}' );
101 }
102  
103 $code->addf( 'my %%args = ( @_ == 1 and %s ) ? %%{+shift} : @_;', HashRef->inline_check('$_[0]') );
104 $code->addf( 'my $clone = bless( {}, $class );' );
105 $code->addf( 'my $used = 0;' ) if $plugin->strict_args;
106 $code->add_gap;
107
108 $marlin->canonicalize_attributes;
109 my @allowed;
110
111 for my $attr ( @{ $marlin->attributes_with_inheritance } ) {
112
113 if ( not exists $attr->{on_clone} ) {
114 $attr->{on_clone} = ( $attr->{storage} ne 'PRIVATE' );
115 }
116
117 $code->addf( '{' );
118 $code->increase_indent;
119 $code->addf( 'my ( $value, $has_value );' );
120
121 my @aliases;
122 @aliases = @{ $attr->{alias} or [] } if $attr->{alias};
123
124 my $if = 'if';
125 my $init_arg = exists( $attr->{init_arg} ) ? $attr->{init_arg} : $attr->{slot};
126 push @allowed, $init_arg if defined $init_arg;
127
128 if ( @aliases ) {
129 $code->addf( 'if ( my @found = grep { exists $args{$_} } %s ) {',
130 join( q[, ] => map { B::perlstring($_) } $init_arg, @aliases ) );
131 $code->increase_indent;
132 $code->addf( 'if ( @found > 1 ) {' );
133 $code->increase_indent;
134 $code->addf( 'shift @found;' );
135 $code->addf( '%s("Superfluous %%s used for attribute \'%%s\': %%s" , @found==1 ? "alias" : "aliases", %s, join( q[, ], sort @found ) );', $attr->_croaker, B::perlstring($attr->{slot}) );
136 $code->decrease_indent;
137 $code->addf( '}' );
138 $code->addf( '( $value, $has_value ) = ( $args{$found[0]}, !!1 );' );
139 $code->addf( '$used++;' ) if $plugin->strict_args;
140 $code->addf( 'undef $has_value unless defined $value;' ) if $attr->{undef_tolerant};
141 $code->decrease_indent;
142 $code->addf( '}' );
143 $if = 'elsif';
144 push @allowed, @aliases;
145 }
146 elsif ( defined $init_arg ) {
147 $code->addf( 'if ( exists $args{%s} ) {', B::perlstring($init_arg) );
148 $code->increase_indent;
149 $code->addf( '( $value, $has_value ) = ( $args{%s}, !!1 );', B::perlstring($init_arg) );
150 $code->addf( '$used++;' ) if $plugin->strict_args;
151 $code->addf( 'undef $has_value unless defined $value;' ) if $attr->{undef_tolerant};
152 $code->decrease_indent;
153 $code->addf( '}' );
154 $if = 'elsif';
155 }
156
157 if ( $attr->{on_clone} eq 1 or not exists $attr->{on_clone} ) {
158 $code->addf( '%s ( %s ) {', $if, $attr->inline_predicate('$self') );
159 $code->increase_indent;
160 $code->addf( '( $value, $has_value ) = ( %s, !!1 );', $attr->inline_access('$self') );
161 $code->decrease_indent;
162 $code->addf( '}' );
163 }
164 elsif ( 'CODE' eq ref $attr->{on_clone} ) {
165 my $var = $code->add_variable( $attr->make_var_name('cloner'), \$attr->{on_clone} );
166 $code->addf( '%s ( %s ) {', $if, $attr->inline_predicate('$self') );
167 $code->increase_indent;
168 $code->addf( '( $value, $has_value ) = ( scalar $self->%s( %s, %s ), !!1 );', $var, B::perlstring($attr->{slot}), $attr->inline_access('$self') );
169 $code->decrease_indent;
170 $code->addf( '}' );
171 }
172 elsif ( $attr->{on_clone} eq 0 or $attr->{on_clone} eq ':none' ) {
173 # no clone
174 }
175 elsif ( !ref $attr->{on_clone} and $attr->{on_clone} =~ /:method\((.+?)\)/ ) {
176 my $clone_method = $1;
177 $code->addf( '%s ( %s and Scalar::Util::blessed( %s ) ) {', $if, $attr->inline_predicate('$self'), $attr->inline_access('$self') );
178 $code->increase_indent;
179 $code->addf( '( $value, $has_value ) = ( ( %s )->%s, !!1 );', $attr->inline_access('$self'), $clone_method );
180 $code->decrease_indent;
181 $code->addf( '}' );
182 }
183 elsif ( !ref $attr->{on_clone} and $attr->{on_clone} =~ /:method/ ) {
184 $code->addf( '%s ( %s and Scalar::Util::blessed( %s ) ) {', $if, $attr->inline_predicate('$self'), $attr->inline_access('$self') );
185 $code->increase_indent;
186 $code->addf( '( $value, $has_value ) = ( ( %s )->clone, !!1 );', $attr->inline_access('$self') );
187 $code->decrease_indent;
188 $code->addf( '}' );
189 }
190 elsif ( !ref $attr->{on_clone} and $attr->{on_clone} =~ /:selfmethod\((.+?)\)/ ) {
191 my $clone_method = $1;
192 $code->addf( '%s ( %s ) {', $if, $attr->inline_predicate('$self') );
193 $code->increase_indent;
194 $code->addf( '( $value, $has_value ) = ( scalar $self->%s( %s, %s ), !!1 );', $clone_method, B::perlstring($attr->{slot}), $attr->inline_access('$self') );
195 $code->decrease_indent;
196 $code->addf( '}' );
197 }
198 elsif ( !ref $attr->{on_clone} and $attr->{on_clone} =~ /^[^\W0-9]\w+$/ ) {
199 $code->addf( '%s ( %s ) {', $if, $attr->inline_predicate('$self') );
200 $code->increase_indent;
201 $code->addf( '( $value, $has_value ) = ( scalar $self->%s( %s, %s ), !!1 );', $attr->{on_clone}, B::perlstring($attr->{slot}), $attr->inline_access('$self') );
202 $code->decrease_indent;
203 $code->addf( '}' );
204 }
205 elsif ( !ref $attr->{on_clone} and $attr->{on_clone} =~ /:deep/ ) {
206 $code->addf( '%s ( %s ) {', $if, $attr->inline_predicate('$self') );
207 $code->increase_indent;
208 $code->addf( '( $value, $has_value ) = ( scalar Clone::clone( %s ), !!1 );', $attr->inline_access('$self') );
209 $code->decrease_indent;
210 $code->addf( '}' );
211 }
212 elsif ( !ref $attr->{on_clone} and $attr->{on_clone} =~ /:simple/ ) {
213 $code->addf( '%s ( %s ) {', $if, $attr->inline_predicate('$self') );
214 $code->increase_indent;
215 $code->addf( '( $value, $has_value ) = ( %s, !!1 );', $attr->inline_access('$self') );
216 $code->decrease_indent;
217 $code->addf( '}' );
218 }
219
220 $code->addf( 'if ( $has_value ) {' );
221 $code->increase_indent;
222 $code->add_line( $attr->inline_writer( '$clone', '$value' ) );
223 $code->decrease_indent;
224 $code->addf( '}' );
225
226 if ( ( exists $attr->{default} or defined $attr->{builder} ) and not $attr->{lazy} ) {
227 my $var = $code->add_variable( $attr->make_var_name('default'), \$attr->{default} );
228 $code->addf( 'else {' );
229 $code->increase_indent;
230 $code->addf( '$value = %s;', $attr->inline_default('$clone', $var) );
231 delete local $attr->{trigger};
232 $code->add_line( $attr->inline_writer( '$clone', '$value' ) );
233 $code->decrease_indent;
234 $code->addf( '}' );
235 }
236 elsif ( $attr->{required} ) {
237 $code->addf( 'else {' );
238 $code->increase_indent;
239 $code->addf( '%s("Missing required attribute \'%%s\'" , %s );', $attr->_croaker, B::perlstring($attr->{slot}) );
240 $code->decrease_indent;
241 $code->addf( '}' );
242 }
243  
244 $code->decrease_indent;
245 $code->addf( '}' );
246 $code->add_gap;
247 }
248
249 if ( $plugin->call_build ) {
250 $code->addf( 'if ( not delete $args{__no_BUILD__} ) {' );
251 $code->increase_indent;
252 $code->addf( '$%s::BUILD_CACHE{$class} ||= do {', ref($marlin) );
253 $code->increase_indent;
254 $code->add_line( 'no strict "refs";' );
255 $code->add_line( 'my $linear_isa = mro::get_linear_isa($class);' );
256 $code->add_line( '[ map { ( *{$_}{CODE} ) ? ( *{$_}{CODE} ) : () } map { "$_\::BUILD" } reverse @$linear_isa ];' );
257 $code->decrease_indent;
258 $code->add_line( '};' );
259 $code->addf( '$_->( $clone, \%%args ) for @{ $%s::BUILD_CACHE{$class} };', ref($marlin) );
260 $code->add_gap;
261 $code->decrease_indent;
262 $code->add_line( '}' );
263 }
264
265 if ( $plugin->strict_args ) {
266 my $check = do {
267 my $enum = Enum->of( @allowed );
268 $enum->can( '_regexp' )
269 ? sprintf( '/\\A%s\\z/', $enum->_regexp )
270 : $enum->inline_check( '$_' );
271 };
272 $code->addf( 'if ( keys( %%args ) > $used ) {' );
273 $code->increase_indent;
274 $code->addf( 'my @unknown = grep not( %s ), keys %%args;', $check );
275 $code->addf( '%s("Unexpected keys in clone arguments: " . join( q[, ], sort @unknown ) ) if @unknown;', $marlin->_croaker );
276 $code->decrease_indent;
277 $code->add_line( '}' );
278 $code->add_gap;
279 }
280
281 $code->addf('return $clone;');
282 $code->decrease_indent;
283 $code->addf( '}' );
284
285 # warn $code->code;
286
287 return $code;
288 }
289  
290 # Utility method used by ->clone() to prepare a separate ->clone() method
291 # for any child classes.
292 sub _make_clone_method_for_child {
293 my $plugin = shift;
294 my $marlin = shift;
295 my $code = $plugin->_make_clone_method( $marlin );
296 my $coderef = $code->compile;
297
298 $marlin->export( $plugin->method_name, $coderef );
299 return $coderef;
300 }
301  
302 __PACKAGE__
303 __END__
↑ Summary