-
Notifications
You must be signed in to change notification settings - Fork 1
/
Class.pm
57 lines (47 loc) · 1.27 KB
/
Class.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
package GID::Class;
# ABSTRACT: Making your classes in GID
use Package::Stash;
use Import::Into;
use Scalar::Util qw( blessed );
use GID ();
use Moo ();
use MooX ();
sub import {
shift;
my $target = scalar caller;
GID->import::into($target,@_);
my $stash = Package::Stash->new($target);
my @gid_methods = $stash->list_all_symbols('CODE');
MooX->import::into($target,qw(
ClassStash
HasEnv
Options
));
$target->can('extends')->('GID::Object');
$target->class_stash->around_method('has',sub {
my $has = shift;
my $attribute_arg = shift;
my @attribute_args = @_;
my @attributes = ref $attribute_arg eq 'ARRAY' ? @{$attribute_arg} : ($attribute_arg);
for (@attributes) {
my $attribute = $_;
if (grep { $attribute eq $_ } @gid_methods) {
my $gid_method = $target->class_stash->get_method($attribute);
$target->class_stash->remove_method($attribute);
$has->($attribute,@attribute_args);
$target->class_stash->around_method($attribute,sub {
my $attribute_method = shift;
my @args = @_;
if (blessed $args[0]) {
return $attribute_method->(@args);
} else {
return $gid_method->(@args);
}
});
} else {
$has->($attribute,@attribute_args);
}
}
});
}
1;