line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MouseX::AttributeTraitHelper::Merge; |
2
|
1
|
|
|
1
|
|
27268
|
use Mouse::Role; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
215
|
use Mouse::Util; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
has TRAIT_MAPPING => ( |
6
|
|
|
|
|
|
|
is => 'ro', |
7
|
|
|
|
|
|
|
isa => 'HashRef[ClassName]', |
8
|
|
|
|
|
|
|
default => sub {return {}}, |
9
|
|
|
|
|
|
|
); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
around add_attribute => sub { |
12
|
|
|
|
|
|
|
my ($orig, $self) = (shift, shift); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
return $self->$orig($_[0]) if Scalar::Util::blessed($_[0]); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $name = shift; |
17
|
|
|
|
|
|
|
my %args = (@_ == 1) ? %{$_[0]} : @_; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
defined($name) |
20
|
|
|
|
|
|
|
or $self->throw_error('You must provide a name for the attribute'); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $traits = delete $args{traits}; |
23
|
|
|
|
|
|
|
if ($traits) { |
24
|
|
|
|
|
|
|
my $role_name; |
25
|
|
|
|
|
|
|
if (@$traits == 1) { |
26
|
|
|
|
|
|
|
$role_name = $traits->[0]; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
else { |
29
|
|
|
|
|
|
|
$role_name = join "::" , 'MouseX::AttributeTraitHelper::Merge' , @$traits; |
30
|
|
|
|
|
|
|
if (!Mouse::Util::is_class_loaded($role_name)) { |
31
|
|
|
|
|
|
|
my $meta = Mouse::Role->init_meta(for_class => $role_name); |
32
|
|
|
|
|
|
|
$meta->add_around_method_modifier('does' => sub { |
33
|
|
|
|
|
|
|
my ($orig_meta, $self_meta, $role) = @_; |
34
|
|
|
|
|
|
|
if ($self->TRAIT_MAPPING->{$role}){ |
35
|
|
|
|
|
|
|
return 1; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
|
|
|
|
|
|
return $self->$orig($name) |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
}); |
41
|
|
|
|
|
|
|
for my $trait (@$traits) { |
42
|
|
|
|
|
|
|
$self->TRAIT_MAPPING->{$trait} = $role_name; |
43
|
|
|
|
|
|
|
Mouse::Util::load_class($trait); |
44
|
|
|
|
|
|
|
for my $trait_attr_name ($trait->meta->get_attribute_list()) { |
45
|
|
|
|
|
|
|
my $trait_attr = $trait->meta->get_attribute($trait_attr_name); |
46
|
|
|
|
|
|
|
$trait_attr_name =~ s/^\+//; |
47
|
|
|
|
|
|
|
my $exist_trait_attr = $meta->get_attribute($trait_attr_name); |
48
|
|
|
|
|
|
|
if ($exist_trait_attr) { |
49
|
|
|
|
|
|
|
@$exist_trait_attr{keys %$trait_attr} = values %$trait_attr; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
|
|
|
|
|
|
$meta->add_attribute($trait_attr_name => {is => 'ro', %$trait_attr}); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
$args{traits} = [$role_name]; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
return $self->$orig($name, %args); |
61
|
|
|
|
|
|
|
}; |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
1
|
|
432
|
no Mouse::Role; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
__END__ |