| 1 | package Populous; | ||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | $Populous::AUTHORITY = 'cpan:GETTY'; | |||||||||
| 4 | } | ||||||||||
| 5 | { | ||||||||||
| 6 | 1 | $Populous::VERSION = '0.001'; | |||||||||
| 7 | } | ||||||||||
| 8 | # ABSTRACT: Populate anything like a god | ||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Package::Stash; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | has classes => ( | |||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | required => 1, | ||||||||||
| 16 | ); | ||||||||||
| 17 | |||||||||||
| 18 | has cache => ( | ||||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | lazy => 1, | ||||||||||
| 21 | 1 | ▶ | builder => sub {{}}, | ||||||||
|
Subroutine
_build_cache: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ); | |||||||||
| 23 | |||||||||||
| 24 | has package_namespace => ( | ||||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | lazy => 1, | ||||||||||
| 27 | 1 | ▶ | builder => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | my ( $self ) = @_; | |||||||||
| 29 | 1 | my $class_id = "$self"; | |||||||||
| 30 | 1 | $class_id =~ s/[^\w\d]//g; | |||||||||
| 31 | 1 | return "__POPULOUS_GENERATED_$class_id"; | |||||||||
| 32 | }, | ||||||||||
| 33 | 1 | ); | |||||||||
| 34 | |||||||||||
| 35 | 1 | ▶ | sub class_to_package { | ||||||||
|
Subroutine
class_to_package: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 36 | 1 | my ( $self, $class ) = @_; | |||||||||
| 37 | 1 | return join("::",$self->package_namespace,$class); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 41 | 1 | my ( $self ) = @_; | |||||||||
| 42 | 1 | my @classes = @{$self->classes}; | |||||||||
| 43 | 1 | my $stash = Package::Stash->new(ref $self); | |||||||||
| 44 | 1 | ▶ | while (@classes) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 1 | my $class = shift @classes; | |||||||||
| 46 | 1 | my $constructor = shift @classes; | |||||||||
| 47 | 1 | my %functions; | |||||||||
| 48 | 1 | ▶ | if (ref $classes[0] eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 1 | my $functions_ref = shift @classes; | |||||||||
| 50 | 1 | %functions = %{$functions_ref}; | |||||||||
| 51 | } | ||||||||||
| 52 | 1 | my $package = $self->class_to_package($class); | |||||||||
| 53 | 1 | ▶ | $stash->add_symbol('&new_'.$class,sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 54 | 1 | shift; | |||||||||
| 55 | 1 | my $i = 0; | |||||||||
| 56 | 1 | ▶ | while (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 3 | my $id = shift; | |||||||||
| 58 | 3 | my $arg = shift; | |||||||||
| 59 | 3 | my $object = $constructor->($id,$arg); | |||||||||
| 60 | 3 | $self->cache->{$class}->{$id} = bless [ $object, $id ], $package; | |||||||||
| 61 | 3 | $i++; | |||||||||
| 62 | } | ||||||||||
| 63 | 1 | return $i; | |||||||||
| 64 | 1 | }); | |||||||||
| 65 | 1 | ▶ | $stash->add_symbol('&delete_'.$class,sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 66 | 1 | shift; | |||||||||
| 67 | 1 | my @killed; | |||||||||
| 68 | 1 | for (@_) { | |||||||||
| 69 | 1 | push @killed, (delete $self->cache->{$class}->{$_}); | |||||||||
| 70 | } | ||||||||||
| 71 | 1 | return @killed; | |||||||||
| 72 | 1 | }); | |||||||||
| 73 | 4 | ▶ | $stash->add_symbol('&get_'.$class,sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=15.1
| |||||||||||
| 74 | 4 | shift; | |||||||||
| 75 | 4 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | my @return; | |||||||||
| 77 | 0 | for (@_) { | |||||||||
| 78 | 0 | push @return, $self->cache->{$class}->{$_}->[0]; | |||||||||
| 79 | } | ||||||||||
| 80 | 0 | return @return; | |||||||||
| 81 | } else { | ||||||||||
| 82 | 4 | return $self->cache->{$class}->{$_[0]}->[0]; | |||||||||
| 83 | } | ||||||||||
| 84 | 1 | }); | |||||||||
| 85 | 7 | ▶ | $stash->add_symbol('&'.$class,sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 86 | 7 | shift; | |||||||||
| 87 | 7 | my ( $id, $func, @args ) = @_; | |||||||||
| 88 | 7 | ▶ | return unless defined $self->cache->{$class}->{$id}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 6 | my $object = $self->cache->{$class}->{$id}; | |||||||||
| 90 | 6 | ▶ | return $object unless defined $func; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 2 | return $object->$func(@args); | |||||||||
| 92 | 1 | }); | |||||||||
| 93 | 1 | ▶ | $functions{delete} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 94 | 1 | delete $self->cache->{$class}->{$_->[1]}; | |||||||||
| 95 | 1 | }; | |||||||||
| 96 | 1 | $self->create_package($package, %functions); | |||||||||
| 97 | } | ||||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 1 | ▶ | sub create_package { | ||||||||
|
Subroutine
create_package: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 101 | 1 | my ( $self, $package, %functions ) = @_; | |||||||||
| 102 | 1 | my $stash = Package::Stash->new($package); | |||||||||
| 103 | 1 | for my $function_name (keys %functions) { | |||||||||
| 104 | 7 | ▶ | $stash->add_symbol('&'.$function_name,sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 105 | 7 | my $self_ref = shift; | |||||||||
| 106 | 7 | my $self = $self_ref->[0]; | |||||||||
| 107 | 7 | for ($self_ref) { | |||||||||
| 108 | 7 | return $functions{$function_name}->($self,@_); | |||||||||
| 109 | } | ||||||||||
| 110 | 3 | }); | |||||||||
| 111 | } | ||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 1 | 1; | |||||||||
| 115 | |||||||||||
| 116 | __END__ | ||||||||||