Commit | Line | Data |
b1eebd55 |
1 | package Moo; |
6c74d087 |
2 | |
3 | use strictures 1; |
b1eebd55 |
4 | use Moo::_Utils; |
6c74d087 |
5 | |
6d71fae7 |
6 | our $VERSION = '0.009001'; # 0.9.1 |
7 | $VERSION = eval $VERSION; |
8 | |
14f32032 |
9 | our %MAKERS; |
10 | |
6c74d087 |
11 | sub import { |
12 | my $target = caller; |
a16d301e |
13 | my $class = shift; |
de3d4906 |
14 | strictures->import; |
1ba11455 |
15 | return if $MAKERS{$target}; # already exported into this package |
6c74d087 |
16 | *{_getglob("${target}::extends")} = sub { |
fb5074f6 |
17 | _load_module($_) for @_; |
6c74d087 |
18 | *{_getglob("${target}::ISA")} = \@_; |
19 | }; |
20 | *{_getglob("${target}::with")} = sub { |
b1eebd55 |
21 | require Moo::Role; |
6c74d087 |
22 | die "Only one role supported at a time by with" if @_ > 1; |
b1eebd55 |
23 | Moo::Role->apply_role_to_package($_[0], $target); |
6c74d087 |
24 | }; |
a16d301e |
25 | $MAKERS{$target} = {}; |
14f32032 |
26 | *{_getglob("${target}::has")} = sub { |
27 | my ($name, %spec) = @_; |
28 | ($MAKERS{$target}{accessor} ||= do { |
29 | require Method::Generate::Accessor; |
30 | Method::Generate::Accessor->new |
31 | })->generate_method($target, $name, \%spec); |
a16d301e |
32 | $class->_constructor_maker_for($target) |
33 | ->register_attribute_specs($name, \%spec); |
14f32032 |
34 | }; |
6c74d087 |
35 | foreach my $type (qw(before after around)) { |
36 | *{_getglob "${target}::${type}"} = sub { |
dccea57d |
37 | require Class::Method::Modifiers; |
6c74d087 |
38 | _install_modifier($target, $type, @_); |
39 | }; |
40 | } |
41 | { |
42 | no strict 'refs'; |
43 | @{"${target}::ISA"} = do { |
b1eebd55 |
44 | require Moo::Object; ('Moo::Object'); |
6c74d087 |
45 | } unless @{"${target}::ISA"}; |
46 | } |
47 | } |
48 | |
a16d301e |
49 | sub _constructor_maker_for { |
50 | my ($class, $target) = @_; |
51 | return unless $MAKERS{$target}; |
52 | $MAKERS{$target}{constructor} ||= do { |
53 | require Method::Generate::Constructor; |
54 | Method::Generate::Constructor |
55 | ->new( |
56 | package => $target, |
57 | accessor_generator => do { |
58 | require Method::Generate::Accessor; |
59 | Method::Generate::Accessor->new; |
60 | } |
61 | ) |
62 | ->install_delayed |
63 | ->register_attribute_specs(do { |
64 | my @spec; |
5d349892 |
65 | # using the -last- entry in @ISA means that classes created by |
66 | # Role::Tiny as N roles + superclass will still get the attributes |
67 | # from the superclass |
098a367b |
68 | if (my $super = do { no strict 'refs'; ${"${target}::ISA"}[-1] }) { |
a16d301e |
69 | if (my $con = $MAKERS{$super}{constructor}) { |
70 | @spec = %{$con->all_attribute_specs}; |
71 | } |
72 | } |
73 | @spec; |
74 | }); |
75 | } |
76 | } |
77 | |
6c74d087 |
78 | 1; |