File Coverage

blib/lib/Persistence/Fetchable.pm
Criterion Covered Total %
statement 27 56 48.2
branch 0 8 0.0
condition 0 3 0.0
subroutine 9 14 64.2
pod 4 4 100.0
total 40 85 47.0


line stmt bran cond sub pod time code
1             package Persistence::Fetchable;
2              
3 17     17   91 use strict;
  17         30  
  17         621  
4 17     17   84 use warnings;
  17         35  
  17         676  
5              
6 17     17   83 use vars qw($VERSION);
  17         34  
  17         692  
7 17     17   108 use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION);
  17         32  
  17         924  
8              
9 17     17   86 use Abstract::Meta::Class ':all';
  17         29  
  17         2653  
10 17     17   90 use base 'Exporter';
  17         33  
  17         5442  
11 17     17   110 use Carp 'confess';
  17         33  
  17         2386  
12              
13 17     17   108 use constant LAZY => 0;
  17         53  
  17         1261  
14 17     17   92 use constant EAGER => 1;
  17         30  
  17         9718  
15              
16             $VERSION = 0.02;
17              
18             @EXPORT_OK = qw(LAZY EAGER);
19             %EXPORT_TAGS = (all => \@EXPORT_OK);
20              
21             abstract_class;
22              
23             =head1 NAME
24              
25             Persistence::Fetchable - Fetching method base class.
26              
27             =cut
28              
29             =head1 SYNOPSIS
30              
31             Abstract class.
32              
33             =head1 DESCRIPTION
34              
35             Represents a base class for attributes that use eager or lazy fetch methods.
36              
37             =head1 EXPORT
38              
39             LAZY EAGER by ':all' tag.
40              
41             =head2 ATTRIBUTES
42              
43             =over
44              
45             =item fetch_method
46              
47             LAZY, EAGER
48              
49             =cut
50              
51             has '$.fetch_method' => (default => LAZY);
52              
53             =back
54              
55             =head2 METHODS
56              
57             =over
58              
59             =item eager_fetch_filter
60              
61             Returns list of objects that have EAGER fetch method.
62             Takes hash ref of objects.
63              
64             =cut
65              
66             sub eager_fetch_filter {
67 0     0 1   my ($class, $hash_of_objects) = @_;
68 0           $class->fetch_objects_filter($hash_of_objects, EAGER);
69             }
70              
71              
72             =item lazy_fetch_filter
73              
74             Returns list of objects that have LAZY fetch method.
75             Takes hash ref of objects.
76              
77             =cut
78              
79             sub lazy_fetch_filter {
80 0     0 1   my ($class, $hash_of_objects) = @_;
81 0           $class->fetch_objects_filter($hash_of_objects, LAZY);
82             }
83              
84              
85             =item fetch_objects_filter
86              
87             Returns list of objects that have specyfied fetch method.
88             Takes hash ref of objects, fetch method.
89              
90             =cut
91              
92             sub fetch_objects_filter {
93 0     0 1   my ($class, $hash_of_objects, $fetch_method) = @_;
94 0           my @result;
95 0           foreach my $k (keys %$hash_of_objects) {
96 0           my $object = $hash_of_objects->{$k};
97 0 0         next if $object->fetch_method ne $fetch_method;
98 0           push @result, $object;
99             }
100 0           @result;
101             }
102              
103              
104             =item lazy_fetch_handler
105              
106             =cut
107              
108             sub lazy_fetch_handler {
109 0     0 1   my ($self, $attribute) = @_;
110 0           my %pending_fetch;
111 0           my $class_name = $attribute->class_name;
112 0           my $attr_name = $attribute->name;
113             sub {
114 0     0     my ($this, $values) = @_;
115 0           my $entity_manager = $self->orm->entity_manager;
116 0 0 0       if ($entity_manager && ! $attribute->has_value($this)) {
117 0 0         unless ($entity_manager->has_lazy_fetch_flag($this, $attr_name)) {
118 0 0         unless ($pending_fetch{$this}) {
119 0           $pending_fetch{$self} = 1;
120 0           my $orm = $entity_manager->find_entity_mappings($class_name);
121 0           $self->deserialise_attribute($this, $entity_manager, $orm);
122 0           delete $pending_fetch{$self};
123 0           $values = $attribute->get_value($this);
124             }
125 0           $entity_manager->add_lazy_fetch_flag($this, $attr_name);
126             }
127             }
128 0           $entity_manager->add_lazy_fetch_flag($this, $attr_name);
129 0           $values;
130 0           };
131             }
132              
133             1;
134              
135              
136              
137             __END__