deferred constructor construction
[gitmo/Moo.git] / lib / Class / Tiny.pm
1 package Class::Tiny;
2
3 use strictures 1;
4 use Class::Tiny::_Utils;
5
6 our %MAKERS;
7
8 sub import {
9   my $target = caller;
10   strictures->import;
11   *{_getglob("${target}::extends")} = sub {
12     *{_getglob("${target}::ISA")} = \@_;
13   };
14   *{_getglob("${target}::with")} = sub {
15     require Role::Tiny;
16     die "Only one role supported at a time by with" if @_ > 1;
17     Role::Tiny->apply_role_to_package($_[0], $target);
18   };
19   *{_getglob("${target}::has")} = sub {
20     my ($name, %spec) = @_;
21     ($MAKERS{$target}{accessor} ||= do {
22       require Method::Generate::Accessor;
23       Method::Generate::Accessor->new
24     })->generate_method($target, $name, \%spec);
25     ($MAKERS{$target}{constructor} ||= do {
26       require Method::Generate::Constructor;
27       Method::Generate::Constructor->new(package => $target)->install_delayed
28     })->register_attribute_spec($name, \%spec);
29   };
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 {
38       require Class::Tiny::Object; ('Class::Tiny::Object');
39     } unless @{"${target}::ISA"};
40   }
41 }
42
43 1;