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