add newlines at die messages
[gitmo/Role-Tiny.git] / lib / Moo / Object.pm
1 package Moo::Object;
2
3 use strictures 1;
4
5 our %NO_BUILD;
6 our $BUILD_MAKER;
7
8 sub new {
9   my $class = shift;
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)
15     : do {
16         my $proto = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };
17         bless({ %$proto }, $class)->BUILDALL($proto);
18       };
19 }
20
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] ."\n";
27         }
28         return { %{ $_[0] } };
29     }
30     elsif ( @_ % 2 ) {
31         die "The new() method for $class expects a hash reference or a key/value list."
32                 . " You passed an odd number of arguments\n";
33     }
34     else {
35         return {@_};
36     }
37 }
38
39 sub BUILDALL {
40   my $self = shift;
41   $self->${\(($BUILD_MAKER ||= do {
42     require Method::Generate::BuildAll;
43     Method::Generate::BuildAll->new
44   })->generate_method(ref($self)))}(@_);
45 }
46
47 sub does {
48   require Role::Tiny;
49   { no warnings 'redefine'; *does = \&Role::Tiny::does_role }
50   goto &Role::Tiny::does_role;
51 }
52
53 1;