blob: f66e71fad8b4120d4637867d8a72684db2c5865e (
plain) (
blame)
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
|
package MooseX::Attribute::Shorthand;
use strict;
use warnings;
# ABSTRACT: write custom attribute option bundles
use Moose ();
use Scalar::Util qw(reftype);
=head1 SYNOPSIS
=head1 DESCRIPTION
=cut
sub import {
my $package = shift;
my %custom_options = @_;
my $for_class = delete($custom_options{'-for_class'}) || caller;
my $role = Moose::Meta::Role->create_anon_role(cache => 1);
for my $option (keys %custom_options) {
my $meta_options = delete $custom_options{$option}{'-meta_attr_options'};
$role->add_attribute($option => (
is => 'ro',
isa => 'Bool',
%{ $meta_options || {} },
));
}
$role->add_around_method_modifier(_process_options => sub {
my $orig = shift;
my $class = shift;
my ($name, $options) = @_;
my %new_options;
for my $option (keys %$options) {
if (exists($custom_options{$option})) {
for my $expanded_option (keys %{ $custom_options{$option} }) {
my $expanded_val = $custom_options{$option}->{$expanded_option};
if (reftype($expanded_val)
&& reftype($expanded_val) eq 'CODE') {
$new_options{$expanded_option} = $expanded_val->(
$name, $options->{$option},
);
}
else {
$new_options{$expanded_option} = $expanded_val;
}
}
}
else {
$new_options{$option} = $options->{$option};
}
}
# relies on being modified in-place
%$options = (%$options, %new_options);
$class->$orig($name, $options);
});
Moose::Util::MetaRole::apply_metaroles(
for => $for_class,
class_metaroles => {
attribute => [$role->name],
},
role_metaroles => {
class_attribute => [$role->name],
}
);
}
=head1 SEE ALSO
L<MooseX::Attributes::Curried>
=cut
1;
|