File Coverage

blib/lib/MooX/BuildArgs.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 19 20 95.0


line stmt bran cond sub pod time code
1             package MooX::BuildArgs;
2 2     2   8532 use 5.008001;
  2         8  
3 2     2   11 use strictures 2;
  2         13  
  2         69  
4             our $VERSION = '0.07';
5              
6             =head1 NAME
7              
8             MooX::BuildArgs - Save instantiation arguments for later use.
9              
10             =head1 SYNOPSIS
11              
12             package Foo;
13             use Moo;
14             with 'MooX::BuildArgs';
15             has bar => (is => 'ro');
16            
17             my $foo = Foo->new( bar => 32 );
18             print $foo->build_args->{bar}; # 32
19              
20             =head1 DESCRIPTION
21              
22             It is often useful to be able to access the arguments that were
23             used to create an object in their unadulterated form, before any
24             coercions or init_args have changed them. This L role
25             provides the arguments via the L attribute.
26              
27             Note that no attempt is made to weaken the args. So, if you use
28             this module and you have attributes with C set the
29             references will not be weakened within L.
30              
31             =cut
32              
33 2     2   419 use Moo::Role;
  2         3  
  2         12  
34 2     2   1478 use namespace::clean;
  2         9978  
  2         10  
35              
36             with 'MooX::BuildArgsHooks';
37              
38             around FINALIZE_BUILDARGS => sub{
39             my ($orig, $class, $args) = @_;
40              
41             $args = $class->$orig( $args );
42              
43             return $class->FINALIZE_BUILD_ARGS_BUILDARGS( $args );
44             };
45              
46             sub FINALIZE_BUILD_ARGS_BUILDARGS {
47 6     6 0 13 my ($class, $args) = @_;
48              
49 6         20 $args->{_build_args} = { %$args };
50              
51 6         18 return $args;
52             }
53              
54             =head1 ATTRIBUTES
55              
56             =head2 build_args
57              
58             my $args_hashref = $object->build_args();
59              
60             Returns a hashref containing the captured arguments.
61              
62             =cut
63              
64             has build_args => (
65             is => 'ro',
66             init_arg => '_build_args',
67             );
68              
69             1;
70             __END__