File Coverage

blib/lib/Pipeline/Store/ISA.pm
Criterion Covered Total %
statement 51 54 94.4
branch 11 14 78.5
condition n/a
subroutine 10 10 100.0
pod 3 5 60.0
total 75 83 90.3


line stmt bran cond sub pod time code
1             package Pipeline::Store::ISA;
2              
3 2     2   45109 use strict;
  2         4  
  2         221  
4 2     2   16 use warnings::register;
  2         5  
  2         434  
5              
6 2     2   1018 use Pipeline::Store;
  2         5  
  2         53  
7 2     2   25 use base qw ( Pipeline::Store );
  2         4  
  2         162  
8              
9 2     2   2524 use Class::ISA;
  2         9089  
  2         1403  
10              
11             our $VERSION = "3.12";
12              
13             sub init {
14 1     1 1 2 my $self = shift;
15 1 50       9 if ( $self->SUPER::init( @_ )) {
16 1         3 $self->obj_store( {} );
17 1         4 $self->isa_store( {} );
18             }
19             }
20              
21             sub obj_store {
22 18     18 0 24 my $self = shift;
23 18 100       37 if (@_) {
24 1         7 $self->{ store } = shift;
25 1         2 return $self;
26             }
27 17         50 return $self->{ store };
28             }
29              
30             sub isa_store {
31 14     14 0 14 my $self = shift;
32 14 100       24 if (@_) {
33 1         2 $self->{ isa_store } = shift;
34 1         5 return $self;
35             }
36 13         33 return $self->{ isa_store };
37             }
38              
39             sub set {
40 3     3 1 9 my $self = shift;
41 3         3 my $obj = shift;
42 3         11 my @isa = Class::ISA::super_path( ref($obj) );
43 3         120 my $store = $self->isa_store;
44 3         4 foreach my $isa (@isa) {
45 6 100       9 if (!exists $self->isa_store->{ $isa }) {
46 2         5 $store->{ $isa } = {};
47             }
48 6         12 $store->{ $isa }->{ ref($obj) } = 1;
49             #push @{$store->{ $isa }}, ref($obj);
50             }
51 3         7 $self->obj_store->{ref($obj)} = $obj;
52 3         15 $self->emit("setting object " . ref($obj));
53 3         9 return $self;
54             }
55              
56             sub get {
57 8     8 1 12 my $self = shift;
58 8         9 my $key = shift;
59              
60 8         20 $self->emit("$key requested");
61              
62 8 100       14 if (exists( $self->obj_store->{ $key })) {
    50          
63 6         17 $self->emit("returning object $key");
64 6         13 return $self->obj_store->{ $key };
65             } elsif (exists( $self->isa_store->{$key})) {
66 2         3 my @objs;
67 2         3 foreach my $thing ( keys %{$self->isa_store->{ $key }} ) {
  2         3  
68 4         11 push @objs, $self->get( $thing );
69             }
70 2 50       23 return [ @objs ] if (@objs > 1);
71 0           return $objs[0];
72             } else {
73 0           $self->emit("no object $key");
74 0           return undef;
75             }
76             }
77              
78             1;
79              
80              
81             =head1 NAME
82              
83             Pipeline::Store::ISA - inheritance-based store for pipelines
84              
85             =head1 SYNOPSIS
86              
87             use Pipeline::Store::ISA;
88              
89             my $store = Pipeline::Store::ISA->new();
90             $store->set( $object );
91             my $object = $store->get( $class );
92              
93             =head1 DESCRIPTION
94              
95             C is a slightly more complex implementation of a
96             Pipeline store than C. It stores things as in a
97             hashref indexed by classname, and also their inheritance tree. You can add
98             an object to a store by calling the set method with an object, and you can
99             get an object by calling the get method with the classname or parent classname
100             of the object you wish to retrieve.
101              
102             C inherits from the C class and
103             includes its methods also.
104              
105             =head1 METHODS
106              
107             =over 4
108              
109             =item set( OBJECT )
110              
111             The C method stores an object specified by OBJECT in itself. Replaces
112             existing objects of the same type.
113              
114             =item get( SCALAR )
115              
116             The C method attempts to return an object of the class specified
117             by SCALAR. If an object of that class does not exist in the store it
118             returns undef instead. In the case that you request a super class of
119             multiple objects an array reference will be returned containing all
120             the objects that are blessed into child classes of SCALAR.
121              
122             =back
123              
124             =head1 SEE ALSO
125              
126             C, C, C
127              
128             =head1 AUTHOR
129              
130             James A. Duncan
131              
132             =head1 COPYRIGHT
133              
134             Copyright 2002 Fotango Ltd. All Rights Reserved.
135              
136             This software is distributed under the same terms as Perl itself.
137              
138             =cut
139              
140