File Coverage

blib/lib/Persistence/Attribute/AMCAdapter.pm
Criterion Covered Total %
statement 21 60 35.0
branch 0 10 0.0
condition n/a
subroutine 7 20 35.0
pod 12 12 100.0
total 40 102 39.2


line stmt bran cond sub pod time code
1             package Persistence::Attribute::AMCAdapter;
2              
3 17     17   93 use strict;
  17         34  
  17         549  
4 17     17   83 use warnings;
  17         60  
  17         518  
5              
6 17     17   80 use vars qw($VERSION);
  17         30  
  17         717  
7              
8 17     17   82 use Abstract::Meta::Class ':all';
  17         28  
  17         3475  
9 17     17   8894 use Persistence::Attribute':all';
  17         43  
  17         638  
10              
11 17     17   115 use base qw(Persistence::Attribute);
  17         29  
  17         1796  
12 17     17   98 use Carp 'confess';
  17         32  
  17         17401  
13              
14             $VERSION = 0.01;
15              
16             =head1 NAME
17              
18             Persistence::Attribute::AMCAdapter - Adapter to Abstract::Meta::Class meta object protocol.
19              
20             =head1 CLASS HIERARCHY
21              
22             Persistence::Attribute
23             |
24             +----Persistence::Attribute::AMCAdapter
25              
26             =head1 SYNOPSIS
27              
28             package Employee;
29              
30             use Abstract::Meta::Class ':all';
31             use Persistence::ORM ':all';
32              
33             my $orm = entity 'emp';
34             $orm->set_mop_attribute_adapter('Persistence::Attribute::AMCAdapter');
35            
36             column empno => has('$.no') ;
37             column ename => has('$.name');
38              
39              
40             =head1 DESCRIPTION
41              
42             Interface to MOP attribute object adapters.
43              
44             =head1 EXPORT
45              
46             None.
47              
48             =head2 ATTRIBUES
49              
50             =over
51              
52             =item object_creation_method
53              
54             Returns object creation method.
55             Allowed values: bless or new
56              
57             =cut
58              
59             has '$.object_creation_method' => (
60             default => 'bless',
61             on_change => sub {
62             my ($self, $attribute, $scope, $value) = @_;
63             confess "invalid value for " . __PACKAGE__ . "::object_creation_method - allowed values(bless | new)"
64             if $$value ne 'bless' && $$value ne 'new'
65             }
66             );
67              
68              
69             =item attribute
70              
71             Any MOP atrribute.
72              
73             =cut
74              
75             has '$.attribute' => (associated_class => 'Abstract::Meta::Attribute');
76              
77              
78             =back
79              
80             =head2 METHODS
81              
82             =over
83              
84             =item name
85              
86             Attribute name.
87              
88             =cut
89              
90             sub name {
91 0     0 1   my ($self) = @_;
92 0           $self->attribute->name;
93             }
94              
95              
96             =item accessor
97              
98             Accessor name - name of the method that returns value of the attribute.
99              
100             my $accessor = $attribute->accessor;
101             my $value = $obj->$accessor;
102              
103             =cut
104              
105             sub accessor {
106 0     0 1   my ($self) = @_;
107 0           $self->attribute->accessor;
108             }
109              
110              
111             =item mutator
112              
113             Accessor name - name of the method that sets value of the attribute.
114              
115             =cut
116              
117             sub mutator {
118 0     0 1   my ($self) = @_;
119 0           $self->attribute->mutator;
120             }
121              
122              
123             =item storage_key
124              
125             Attribute storage key.
126              
127             If this option is set and object_creation_method is set to 'bless'
128             then a new object creation will use bless method
129              
130             bless { map {($_->storage_key, $args{$_->name})} @attributes}, $class
131              
132             otherwise new method will be used.
133              
134             $class->new(map {($_->name, $args{$_->name})} @attributes);
135              
136             =cut
137              
138             sub storage_key {
139 0     0 1   my ($self) = @_;
140 0           $self->attribute->storage_key;
141             }
142              
143              
144             =item associated_class
145              
146             Name of the associated class.
147              
148             For isntance if you have relationship bettwen My::Employee object and My::Dept
149             then associated_class will be My::Dept
150              
151             =cut
152              
153             sub associated_class {
154 0     0 1   my ($self) = @_;
155 0           $self->attribute->associated_class;
156             }
157              
158              
159             =item class_name
160              
161             Class to whom the attribute belongs.
162              
163             =cut
164              
165             sub class_name {
166 0     0 1   my ($self) = @_;
167 0           $self->attribute->class;
168             }
169              
170              
171              
172             =item get_value
173              
174             Returns value form object without triggering any events.
175             Takes object as parameter.
176              
177             =cut
178              
179             sub get_value {
180 0     0 1   my ($self, $object) = @_;
181 0           $self->attribute->get_value($object);
182             }
183              
184              
185              
186             =item set_value
187              
188             Sets object value without triggering any events.
189             Takes object, value as parameter.
190              
191             =cut
192              
193             sub set_value {
194 0     0 1   my ($self, $object, $value) = @_;
195 0           $self->attribute->set_value($object, $value);
196             }
197              
198              
199             =item has_value
200              
201             Returns true if object has value for the attribute.
202              
203             =cut
204              
205             sub has_value {
206 0     0 1   my ($self, $object) = @_;
207 0           my $attribute = $self->attribute;
208 0           my $method = $object->can("has_" . $attribute->accessor);
209 0 0         $method ? $method->($object) : $self->get_value($object);
210             }
211              
212              
213             =item find_attribute
214              
215             Returns attribute
216             Takes class name attribute name.
217              
218              
219             =cut
220              
221             sub find_attribute {
222 0     0 1   my ($clazz, $class, $attribute_name) = @_;
223 0           my $meta_class = Abstract::Meta::Class::meta_class($class);
224 0           $meta_class->attribute($attribute_name);
225             }
226              
227              
228             =item create_meta_attribute
229              
230             Return a new persisitence attribute object
231              
232             =cut
233              
234             sub create_meta_attribute {
235 0     0 1   my ($clazz, $meta_attribute, $class, $column_name) = @_;
236 0           my $meta_class = Abstract::Meta::Class::meta_class($class);
237 0           my $name = $meta_attribute->{name};
238 0 0         $name = '$.' . $name unless ($name =~ m/[\$\@\%]\./);
239 0           my %args = (storage_key => $meta_attribute->{name}, %$meta_attribute, name => $name, class => $class);
240 0           $clazz->new(attribute => $meta_class->attribute_class->new(%args), column_name => $column_name);
241             }
242              
243              
244             =item install_fetch_interceptor
245              
246             =cut
247              
248              
249             sub install_fetch_interceptor {
250 0     0 1   my ($self, $code_ref) = @_;
251 0           my $attribute = $self->attribute;
252             $attribute->set_on_read(
253             sub {
254 0     0     my ($this, $attribute, $scope, $index) = @_;
255 0           my $values = $attribute->get_value($this);
256 0           $values = $code_ref->($this, $values);
257 0 0         if ($scope eq 'accessor') {
258 0           return $values;
259             } else {
260 0           my $type = ref $values;
261 0 0         return $type eq 'HASH' ? $values->{$index} : ($type eq 'ARRAY' ? $values->[$index] : $values);
    0          
262             }
263             }
264 0           );
265             }
266              
267             1;
268              
269             __END__