/
PartialObject.pm
62 lines (48 loc) · 1.47 KB
/
PartialObject.pm
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
package MooseX::PartialObject;
use Moose;
has 'class' => (
is => 'ro',
isa => 'Moose::Meta::Class',
required => 1,
handles => {
_get_attribute => 'get_attribute',
},
);
has 'partial_instance' => (
is => 'ro',
lazy => 1,
default => sub { shift->class->get_meta_instance->create_instance },
);
# add get/set
for my $dir (qw/get set/){
__PACKAGE__->meta->add_method( $dir => sub {
my ($self, $slot_name, @args) = @_;
my $method = "${dir}_value";
confess "'$slot_name' is not a valid attribute in '@{[$self->class->name]}'"
unless $self->class->get_meta_instance->is_valid_slot($slot_name);
$self->_get_attribute($slot_name)->$method($self->partial_instance, @args)
});
}
sub expand {
my $self = shift;
my %init_args =
map { @$_ }
grep { defined $_->[1] }
map { [ $_->init_arg, $self->get($_->name) ] }
$self->class->compute_all_applicable_attributes;
return $self->class->name->new( \%init_args );
}
1;
__END__
=head1 NAME
MooseX::PartialObject - build an object incrementally
=head1 SYNOPSIS
{ package Class; use Moose; has [qw/foo bar/] => ( is => 'ro', required => 1 ) }
my $partial = MooseX::PartialObject->new(
class => Class->meta,
);
$partial->set('foo', 42);
$partial->set('bar', 13);
$partial->set('made_up_name', 1); # throws an exception
my $real = $self->expand;
say $real->foo + $real->bar