File Coverage

blib/lib/Object/InsideOut/Dynamic.pm
Criterion Covered Total %
statement 70 96 72.9
branch 22 44 50.0
condition 1 6 16.6
subroutine 6 7 85.7
pod n/a
total 99 153 64.7


line stmt bran cond sub pod time code
1             package Object::InsideOut; {
2              
3 4     4   23 use strict;
  4         3  
  4         116  
4 4     4   13 use warnings;
  4         4  
  4         106  
5 4     4   11 no warnings 'redefine';
  4         5  
  4         2109  
6              
7             sub create_field
8             {
9             my ($GBL, $call, @args) = @_;
10             push(@{$$GBL{'export'}}, 'create_field');
11             if ($call eq 'create_field') {
12             $$GBL{'init'} = 1;
13             }
14              
15             # Dynamically create a new object field
16             *Object::InsideOut::create_field = sub
17             {
18             # Handle being called as a method or subroutine
19 4 100   4   11 if ($_[0] eq 'Object::InsideOut') {
20 1         1 shift;
21             }
22              
23 4         63 my ($class, $field, @attrs) = @_;
24              
25             # Verify valid class
26 4 50       27 if (! $class->isa('Object::InsideOut')) {
27 0         0 OIO::Args->die(
28             'message' => 'Not an Object::InsideOut class',
29             'Arg' => $class);
30             }
31              
32             # Check for valid field
33 4 50       25 if ($field !~ /^\s*[@%]\s*[a-zA-Z_]\w*\s*$/) {
34 0         0 OIO::Args->die(
35             'message' => 'Not an array or hash declaration',
36             'Arg' => $field);
37             }
38              
39             # Convert attributes to single string
40 4         4 my $attr;
41 4 50       11 if (@attrs) {
42 4         40 s/^\s*(.*?)\s*$/$1/ foreach @attrs;
43 4         9 $attr = join(',', @attrs);
44 4         7 $attr =~ s/[\r\n]/ /sg;
45 4         9 $attr =~ s/,\s*,/,/g;
46 4         14 $attr =~ s/\s*,\s*:/ :/g;
47 4 100       12 if ($attr !~ /^\s*:/) {
48 1         2 $attr = ":Field($attr)";
49             }
50             } else {
51 0         0 $attr = ':Field';
52             }
53              
54             # Create the declaration
55 4         5 my @errs;
56 4     0   22 local $SIG{'__WARN__'} = sub { push(@errs, @_); };
  0         0  
57              
58 4         10 my $code = "package $class; my $field $attr;";
59 4         222 eval $code;
60 4 50       388 if (my $e = Exception::Class::Base->caught()) {
61 0         0 die($e);
62             }
63 4 50 33     50 if ($@ || @errs) {
64 0   0     0 my ($err) = split(/ at /, $@ || join(" | ", @errs));
65 0         0 OIO::Code->die(
66             'message' => 'Failure creating field',
67             'Error' => $err,
68             'Code' => $code);
69             }
70              
71             # Invalidate object initialization activity cache
72 4         5 delete($$GBL{'cache'});
73              
74             # Process the declaration
75 4         11 process_fields();
76             };
77              
78              
79             # Runtime hierarchy building
80             *Object::InsideOut::add_class = sub
81             {
82 1     1   1 my $class = shift;
83 1 50       2 if (ref($class)) {
84 0         0 OIO::Method->die('message' => q/'add_class' called as an object method/);
85             }
86 1 50       2 if ($class eq 'Object::InsideOut') {
87 0         0 OIO::Method->die('message' => q/'add_class' called on non-class 'Object::InsideOut'/);
88             }
89 1 50       5 if (! $class->isa('Object::InsideOut')) {
90 0         0 OIO::Method->die('message' => "'add_class' called on non-Object::InsideOut class '$class'");
91             }
92              
93 1         1 my $pkg = shift;
94 1 50       2 if (! $pkg) {
95 0         0 OIO::Args->die(
96             'message' => 'Missing argument',
97             'Usage' => "$class\->add_class(\$class)");
98             }
99              
100             # Already in the hierarchy - ignore
101 1 50       3 return if ($class->isa($pkg));
102              
103 4     4   17 no strict 'refs';
  4         104  
  4         1668  
104              
105             # If no package symbols, then load it
106 1 50       2 if (! grep { $_ !~ /::$/ } keys(%{$pkg.'::'})) {
  11         12  
  1         4  
107 0         0 eval "require $pkg";
108 0 0       0 if ($@) {
109 0         0 OIO::Code->die(
110             'message' => "Failure loading package '$pkg'",
111             'Error' => $@);
112             }
113             # Empty packages make no sense
114 0 0       0 if (! grep { $_ !~ /::$/ } keys(%{$pkg.'::'})) {
  0         0  
  0         0  
115 0         0 OIO::Code->die('message' => "Package '$pkg' is empty");
116             }
117             }
118              
119             # Import the package, if needed
120 1 50       2 if (@_) {
121 0         0 eval { $pkg->import(@_); };
  0         0  
122 0 0       0 if ($@) {
123 0         0 OIO::Code->die(
124             'message' => "Failure running 'import' on package '$pkg'",
125             'Error' => $@);
126             }
127             }
128              
129 1         2 my $tree_bu = $$GBL{'tree'}{'bu'};
130 1         1 my $tree_td = $$GBL{'tree'}{'td'};
131              
132             # Foreign class added
133 1 50       4 if (! exists($$tree_bu{$pkg})) {
134             # Get inheritance 'classes' hash
135 0 0       0 if (! exists($$GBL{'heritage'}{$class})) {
136 0         0 create_heritage($class);
137             }
138             # Add package to inherited classes
139 0         0 $$GBL{'heritage'}{$class}{'cl'}{$pkg} = undef;
140 0         0 return;
141             }
142              
143             # Add to class trees
144 1         1 foreach my $cl (keys(%{$tree_bu})) {
  1         3  
145 3 100       3 next if (! grep { $_ eq $class } @{$$tree_bu{$cl}});
  4         9  
  3         4  
146              
147             # Splice in the added class's tree
148 2         2 my @tree;
149 2         1 foreach (@{$$tree_bu{$cl}}) {
  2         3  
150 3         3 push(@tree, $_);
151 3 100       4 if ($_ eq $class) {
152 2         2 my %seen;
153 2         3 @seen{@{$$tree_bu{$cl}}} = undef;
  2         3  
154 2         1 foreach (@{$$tree_bu{$pkg}}) {
  2         5  
155 2 50       5 push(@tree, $_) if (! exists($seen{$_}));
156             }
157             }
158             }
159              
160             # Add to @ISA array
161 2         1 push(@{$cl.'::ISA'}, $pkg);
  2         23  
162              
163             # Save revised trees
164 2         4 $$tree_bu{$cl} = \@tree;
165 2         3 @{$$tree_td{$cl}} = reverse(@tree);
  2         5  
166             }
167 1         3 $$GBL{'asi'}{$pkg}{$class} = undef;
168             };
169              
170             # Invalidate object initialization activity cache
171             delete($$GBL{'cache'});
172              
173             # Do the original call
174             @_ = @args;
175             goto &$call;
176             }
177              
178             } # End of package's lexical scope
179              
180              
181             # Ensure correct versioning
182             ($Object::InsideOut::VERSION eq '4.03')
183             or die("Version mismatch\n");
184              
185             # EOF