File: NoClone.pm

package info (click to toggle)
libmoosex-clone-perl 0.05-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 112 kB
  • ctags: 24
  • sloc: perl: 347; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 1,418 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl

package MooseX::Clone::Meta::Attribute::Trait::NoClone;
use Moose::Role;

use namespace::clean -except => [qw(meta)];

with qw(MooseX::Clone::Meta::Attribute::Trait::Clone::Base);

sub Moose::Meta::Attribute::Custom::Trait::NoClone::register_implementation { __PACKAGE__ }

sub clone_value {
    my ( $self, $target, $proto, %args ) = @_;

    # FIXME default cloning behavior works like this
    #if ( exists $args{init_arg} ) {
    #   $self->set_value($args{init_arg});
    #} else {
    # but i think this is more correct

    $self->clear_value($target);
    $self->initialize_instance_slot(
        $self->meta->get_meta_instance,
        $target,
        { exists $args{init_arg} ? ( $self->init_arg => $args{init_arg} ) : () },
    );
}

__PACKAGE__

__END__

=pod

=head1 NAME

MooseX::Clone::Meta::Attribute::Trait::NoClone - A trait for attrs that should
not be copied while cloning.

=head1 SYNOPSIS

    with qw(MooseX::Clone);

    has _some_special_thingy => (
        traits => [qw(NoClone)],
    );

=head1 DESCRIPTION

Sometimes certain values should not be carried over when cloning an object.

This attribute trait implements just that.

=head1 METHODS

=over 4

=item clone_value

If the C<init_arg> param is set (that means an explicit value was given to
C<clone>) sets the attribute to that value.

Otherwise calls C<clear_value> and C<initialize_instance_slot>.

=back

=cut