summaryrefslogtreecommitdiffstats
path: root/lib/MooseX/NonMoose.pm
blob: db2e385d0c97d154b0d53c80934d462b59a0da24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package MooseX::NonMoose;
use Moose ();
use Moose::Exporter;

Moose::Exporter->setup_import_methods(
    with_caller => ['extends_nonmoose'],
);

sub extends_nonmoose {
    my $caller = shift;
    my @superclasses = @_;

    push @superclasses, 'Moose::Object'
        unless grep { $_->isa('Moose::Object') } @superclasses;

    Moose::extends($caller, @superclasses);

    my $meta = Class::MOP::Class->initialize($caller);
    if ($meta->find_next_method_by_name('new')->body ne \&constructor) {
        $meta->add_method(new => sub {
            my $class = shift;
            my $meta = Class::MOP::Class->initialize($class);

            # we need to get the non-moose constructor from the superclass
            # of the class where this method actually exists
            my $caller_meta = Class::MOP::Class->initialize($caller);
            my $super_new = $caller_meta->find_next_method_by_name('new');
            # but we need to call it as a method on the class we're actually
            # trying to instantiate
            my $self = $super_new->execute($class, @_);

            my $params = $class->BUILDARGS(@_);
            my $moose_self = $meta->new_object(
                __INSTANCE__ => $self,
                %$params,
            );
            $moose_self->BUILDALL($params);
            return $moose_self;
        });
    }
}

sub init_meta {
    shift;
    my %options = @_;
    Moose->init_meta(%options);
    Moose::Util::MetaRole::apply_metaclass_roles(
        for_class                 => $options{for_class},
        metaclass_roles => ['MooseX::NonMoose::Meta::Role::Class'],
    );
    return $options{for_class}->meta;
}

1;