File Coverage

blib/lib/Rope/Factory.pm
Criterion Covered Total %
statement 49 49 100.0
branch 14 16 87.5
condition n/a
subroutine 11 11 100.0
pod n/a
total 74 76 97.3


line stmt bran cond sub pod time code
1             package Rope::Factory;
2              
3 4     4   1553 use strict; use warnings;
  4     4   6  
  4         106  
  4         26  
  4         5  
  4         151  
4 4     4   1466 use Factory::Sub;
  4         536998  
  4         15  
5 4     4   171 use Coerce::Types::Standard;
  4         8  
  4         39  
6 4     4   4742 use Rope::Pro;
  4         8  
  4         163  
7              
8             my (%PRO);
9              
10             BEGIN {
11 4     4   23 %PRO = Rope::Pro->new;
12             }
13              
14             sub import {
15 5     5   80 my ($caller, $pkg, @types) = (scalar caller, @_);
16 5 50       24 if (@types) {
17 5         33 Coerce::Types::Standard->import::into($caller, @types)
18             }
19             $PRO{keyword}($caller, 'factory', sub {
20 10     10   135328 my ($name, @factory) = @_;
21 10 100       36 my ($meta, $ind, $fact, $exists, $extends) = (
22             Rope->get_meta($caller),
23             (! ref $factory[0] ? shift @factory : 0)
24             );
25 10 100       23 if ($meta->{properties}->{$name}) {
26 8 100       31 if ($meta->{properties}->{$name}->{fact}->[$ind]) {
27 3         5 $fact = $meta->{properties}->{$name}->{fact}->[$ind];
28 3         5 $exists = 1;
29             } else {
30 5         23 $fact = Factory::Sub->new();
31 5         135 $extends = 1;
32             }
33             } else {
34 2         14 $fact = Factory::Sub->new();
35             }
36 10         44 while (@factory) {
37 17         168 my ($check, $cb) = (shift(@factory), shift(@factory));
38 17 100       31 if (ref $check eq 'CODE') {
39 1 50       4 unshift @factory, $cb if $cb;
40 1         3 $fact->add($check);
41             } else {
42 16     27   35 $fact->add(sub { $_[0]; }, @{$check}, $cb);
  27         9474  
  16         37  
43             }
44             }
45 10 100       95 if ($extends) {
    100          
46 5         16 Rope->clear_property($caller, $name);
47 5         6 my $prop = $meta->{properties}->{$name};
48 5         6 push @{$prop->{fact}}, $fact;
  5         7  
49 5     11   6 push @{$prop->{after}}, sub { $fact->(@_) };
  5         14  
  11         39  
50 5         5 $caller->property($name, %{$prop});
  5         16  
51             } elsif (!$exists) {
52             $caller->property($name,
53 11     11   37 value => sub { $fact->(@_) },
54 2         17 initable => 0,
55             enumerable => 0,
56             writeable => 0,
57             fact => [$fact]
58             );
59             }
60 5         4966 });
61             }
62              
63             1;
64              
65             __END__