Commit | Line | Data |
b1eebd55 |
1 | package Moo::Object; |
6c74d087 |
2 | |
3 | use strictures 1; |
4 | |
098a367b |
5 | our %NO_BUILD; |
6 | our $BUILD_MAKER; |
7 | |
6c74d087 |
8 | sub 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 | |
a17be455 |
21 | sub BUILDARGS { |
22 | my $class = shift; |
23 | if ( scalar @_ == 1 ) { |
24 | unless ( defined $_[0] && ref $_[0] eq 'HASH' ) { |
25 | die "Single parameters to new() must be a HASH ref" |
26 | ." data => ". $_[0]; |
27 | } |
28 | return { %{ $_[0] } }; |
29 | } |
30 | elsif ( @_ % 2 ) { |
31 | warn "The new() method for $class expects a hash reference or a key/value list." |
32 | . " You passed an odd number of arguments"; |
33 | return { @_, undef }; |
34 | } |
35 | else { |
36 | return {@_}; |
37 | } |
38 | } |
39 | |
098a367b |
40 | sub 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 | |
48 | sub does { |
49 | require Role::Tiny; |
50 | { no warnings 'redefine'; *does = \&Role::Tiny::does_role } |
51 | goto &Role::Tiny::does_role; |
52 | } |
53 | |
54 | 1; |