File: Rebuild.pm

package info (click to toggle)
libmoox-buildargs-perl 0.08-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144 kB
  • sloc: perl: 300; makefile: 2
file content (104 lines) | stat: -rw-r--r-- 1,511 bytes parent folder | download | duplicates (2)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package MooX::Rebuild;
our $VERSION = '0.08';

=encoding utf8

=head1 NAME

MooX::Rebuild - Rebuild your Moo objects.

=head1 SYNOPSIS

    package Foo;
    use Moo;
    with 'MooX::Rebuild';
    has get_bar => (
        is       => 'ro',
        init_arg => 'bar',
    );
    
    my $foo1 = Foo->new( bar => 'lala' );
    my $foo2 = $foo1->rebuild();
    print $foo2->get_bar(); # lala

=head1 DESCRIPTION

Make copies of Moo objects using the same arguments used to create
the original objects.

This Moo role depends on, and uses, the L<MooX::BuildArgs> role in
order to capture the original arguments used to create an object.

=cut

use Moo::Role;
use strictures 2;
use namespace::clean;

with 'MooX::BuildArgs';

=head1 METHODS

=head2 rebuild

    my $clone   = $object->rebuild();
    my $similar = $object->rebuild( %extra_args );

Creates a new instance in the same class as the source object and
using the same arguments used to make the source object.

=cut

sub rebuild {
    my $self = shift;
    my $class = ref( $self );

    my $args = $class->BUILDARGS( @_ );

    $args = {
        %{ $self->build_args() },
        %$args,
    };

    return $class->new( $args );
}

1;
__END__

=head1 SEE ALSO

=over

=item *

L<MooX::BuildArgs>

=item *

L<MooX::BuildArgsHooks>

=item *

L<MooX::MethodProxyArgs>

=item *

L<MooX::SingleArg>

=back

=head1 SUPPORT

See L<MooX::BuildArgs/SUPPORT>.

=head1 AUTHORS

See L<MooX::BuildArgs/AUTHORS>.

=head1 LICENSE

See L<MooX::BuildArgs/LICENSE>.

=cut