line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::HandlesConstructor; |
2
|
|
|
|
|
|
|
# ABSTRACT: Moo[se] extension that allows for setting handle accessors with the constructor |
3
|
|
|
|
|
|
|
$MooseX::HandlesConstructor::VERSION = '0.001'; |
4
|
1
|
|
|
1
|
|
652091
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
77
|
|
6
|
1
|
|
|
1
|
|
974
|
use MooseX::MungeHas (); |
|
1
|
|
|
|
|
5361
|
|
|
1
|
|
|
|
|
28
|
|
7
|
1
|
|
|
1
|
|
13
|
use Import::Into; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
8
|
1
|
|
|
1
|
|
1244
|
use Class::Method::Modifiers qw(install_modifier); |
|
1
|
|
|
|
|
1909
|
|
|
1
|
|
|
|
|
351
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub import { |
11
|
1
|
|
|
1
|
|
12
|
my ($class) = @_; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
|
|
10
|
my $target = caller; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
22
|
my %_handles_via_accessors; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
MooseX::MungeHas->import::into( $target, sub { |
18
|
1
|
|
|
1
|
|
1065
|
my $name = shift; |
19
|
1
|
|
|
|
|
5
|
my %has_args = @_; |
20
|
1
|
50
|
|
|
|
6
|
if( exists $has_args{handles} ) { |
21
|
1
|
|
|
|
|
2
|
my %handles = %{ $has_args{handles} }; |
|
1
|
|
|
|
|
5
|
|
22
|
2
|
|
|
|
|
5
|
my @accessors = grep { |
23
|
1
|
|
|
|
|
5
|
my $handle_val = $handles{$_}; |
24
|
2
|
50
|
33
|
|
|
23
|
ref $handle_val eq 'ARRAY' |
25
|
|
|
|
|
|
|
and @$handle_val == 2 |
26
|
|
|
|
|
|
|
and $handle_val->[0] eq 'accessor' |
27
|
|
|
|
|
|
|
} keys %handles; |
28
|
1
|
|
|
|
|
34
|
@_handles_via_accessors{ @accessors } = (1) x @accessors; |
29
|
|
|
|
|
|
|
} |
30
|
1
|
|
|
|
|
18
|
} ); |
31
|
|
|
|
|
|
|
|
32
|
1
|
50
|
|
2
|
|
1339
|
install_modifier( $target, 'fresh', BUILD => sub {} ) unless $target->can('BUILD'); |
|
2
|
|
|
|
|
324370
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
install_modifier($target, 'after', BUILD => sub { |
35
|
2
|
|
|
2
|
|
14
|
my ($self, $args) = @_; |
36
|
2
|
|
|
|
|
38
|
while( my ($attr, $attr_value) = each %$args ) { |
37
|
|
|
|
|
|
|
# NOTE This may be better handled as a set intersection |
38
|
|
|
|
|
|
|
# between (keys %_handles_via_accessors) and |
39
|
|
|
|
|
|
|
# (keys %$args) but for small hashes, it's probably not |
40
|
|
|
|
|
|
|
# efficient. |
41
|
2
|
100
|
|
|
|
11
|
if( exists $_handles_via_accessors{$attr} ) { |
42
|
1
|
|
|
|
|
44
|
$self->$attr( $attr_value ); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
1
|
|
|
|
|
181
|
}); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
1; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
__END__ |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=pod |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=encoding UTF-8 |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 NAME |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
MooseX::HandlesConstructor - Moo[se] extension that allows for setting handle accessors with the constructor |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 VERSION |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
version 0.001 |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 SYNOPSIS |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
package Message; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
use Moo; # or Moose traits |
69
|
|
|
|
|
|
|
use MooX::HandlesVia; |
70
|
|
|
|
|
|
|
use MooseX::HandlesConstructor; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
has header => ( is => 'rw', |
73
|
|
|
|
|
|
|
default => sub { {} }, |
74
|
|
|
|
|
|
|
handles_via => 'Hash', |
75
|
|
|
|
|
|
|
handles => { |
76
|
|
|
|
|
|
|
session => [ accessor => 'session' ], |
77
|
|
|
|
|
|
|
msg_type => [ accessor => 'msg_type' ] |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# elsewhere... |
82
|
|
|
|
|
|
|
my $msg = Message->new( msg_type => 'reply', header => { answer => 42 } ); |
83
|
|
|
|
|
|
|
use Data::Dumper; print Dumper $msg->header; |
84
|
|
|
|
|
|
|
# $VAR1 = { |
85
|
|
|
|
|
|
|
# 'answer' => 42, |
86
|
|
|
|
|
|
|
# 'msg_type' => 'reply' |
87
|
|
|
|
|
|
|
# }; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 DESCRIPTION |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
When using Moo or Moose handles that provide an accessor handle on an |
92
|
|
|
|
|
|
|
attribute, it may make sense to pass the name of handles to the constructor to |
93
|
|
|
|
|
|
|
simplify the API. Using this module will get all curried accessor handles |
94
|
|
|
|
|
|
|
defined in a class and allow setting them when calling C<->new()> |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 SEE ALSO |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
L<MooX::HandlesVia>, L<Moose native delegation|Moose::Manual::Delegation/"NATIVE DELEGATION">. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 AUTHOR |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Zakariyya Mughal <zmughal@cpan.org> |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Zakariyya Mughal. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
109
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |