inline BUILDARGS
[gitmo/Role-Tiny.git] / lib / Moo / Object.pm
CommitLineData
b1eebd55 1package Moo::Object;
6c74d087 2
3use strictures 1;
4
098a367b 5our %NO_BUILD;
6our $BUILD_MAKER;
7
6c74d087 8sub new {
9 my $class = shift;
098a367b 10 $NO_BUILD{$class} and
11 return bless({ ref($_[0]) eq 'HASH' ? %{$_[0]} : @_ }, $class);
12 $NO_BUILD{$class} = !$class->can('BUILD') unless exists $NO_BUILD{$class};
13 $NO_BUILD{$class}
14 ? bless({ ref($_[0]) eq 'HASH' ? %{$_[0]} : @_ }, $class)
077bd026 15 : do {
5d349892 16 my $proto = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };
17 bless({ %$proto }, $class)->BUILDALL($proto);
077bd026 18 };
098a367b 19}
20
0123201b 21# Inlined into Method::Generate::Constructor::_generate_args() - keep in sync
a17be455 22sub BUILDARGS {
23 my $class = shift;
24 if ( scalar @_ == 1 ) {
25 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
26 die "Single parameters to new() must be a HASH ref"
b512e801 27 ." data => ". $_[0] ."\n";
a17be455 28 }
29 return { %{ $_[0] } };
30 }
31 elsif ( @_ % 2 ) {
bbed1e70 32 die "The new() method for $class expects a hash reference or a key/value list."
b512e801 33 . " You passed an odd number of arguments\n";
a17be455 34 }
35 else {
36 return {@_};
37 }
38}
39
098a367b 40sub BUILDALL {
41 my $self = shift;
42 $self->${\(($BUILD_MAKER ||= do {
43 require Method::Generate::BuildAll;
44 Method::Generate::BuildAll->new
45 })->generate_method(ref($self)))}(@_);
6c74d087 46}
47
48sub does {
49 require Role::Tiny;
50 { no warnings 'redefine'; *does = \&Role::Tiny::does_role }
51 goto &Role::Tiny::does_role;
52}
53
541;