File Coverage

lib/DBIx/Simple/OO.pm
Criterion Covered Total %
statement 21 21 100.0
branch 4 8 50.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 31 35 88.5


line stmt bran cond sub pod time code
1             package DBIx::Simple::OO;
2              
3 3     3   2188 use strict;
  3         5  
  3         136  
4 3     3   17 use vars qw[$VERSION];
  3         6  
  3         1094  
5              
6             $VERSION = 0.03;
7              
8             =head1 NAME
9              
10             DBIx::Simple::OO
11              
12             =head1 SYNOPSIS
13              
14             use DBIx::Simple;
15             use DBIx::Simple::OO; # adds OO methods
16              
17             $db = DBIx::Simple->connect( ... );
18             $query = 'select id,name,age from people';
19              
20             $res = $db->query( $query );
21              
22             $obj = $res->object;
23             @obj = $res->objects;
24              
25             $id = $obj->id; # get the value for field 'id'
26             $name = $obj->name; # get the value for field 'name'
27             $age = $obj->age; # get the value for field 'age'
28              
29             @acc = $obj->ls_accessors; # get a list of all fields
30             $sub = $obj->can('name'); # check if this object has a
31             # 'name' method
32              
33             ### add a method to every object that will be returned
34             ### by DBIx::Simple::OO
35             { package DBIx::Simple::OO::Item;
36             sub has_valid_id { return shift->id !~ /\D/ ? 1 : 0 }
37             }
38             $bool = $obj->has_valid_id;
39              
40             =head1 DESCRIPTION
41              
42             This module provides a possibility to retrieve rows from a
43             database as objects, rather than the traditional C
44             or C. This provides all the usual benefits of using
45             objects over plain references for accessing data, as well as
46             allowing you to add methods of your own choosing for data
47             retrieval.
48              
49             =head1 HOW IT WORKS
50              
51             C declares it's 2 methods in the C
52             namespace, transforming the rows retrieved from the database to
53             full fledged objects.
54              
55             =cut
56              
57             ### the retrieval methods are in the DBIx::Simple::Result package
58              
59             package # hide from PAUSE
60             DBIx::Simple::Result;
61              
62             =head1 METHODS
63              
64             This module subclasses C and only adds the following
65             methods. Any other method, like the C call should be looked up
66             in the C manpage instead.
67              
68             =head2 $obj = = $db->query(....)->object( );
69              
70             Returns the first result from your query as an object.
71              
72             =cut
73              
74             sub object {
75 1 50   1   4134 my $self = shift or return;
76              
77 1         12 return $self->_href_to_obj( $self->hash );
78             }
79              
80             =head2 @objs = $db->query(....)->objects( );
81              
82             Returns the results from your query as a list of objects.
83              
84             =cut
85              
86             sub objects {
87 1 50   1   1550 my $self = shift or return;
88              
89 1         13 return map { $self->_href_to_obj( $_ ) } $self->hashes;
  2         16  
90             }
91              
92             ### convert the hashref to a nice O::A object
93             sub _href_to_obj {
94 4 50   4   786 my $self = shift or return;
95 4 50       18 my $href = shift or return;
96              
97 4         33 my $obj = DBIx::Simple::OO::Item->new;
98              
99             ### create accessors for every hash key
100 4         58 $obj->mk_accessors( keys %$href );
101              
102             ### and set the value
103 4         144 for my $acc ( $obj->ls_accessors ) {
104 11         1997 $obj->$acc( $href->{$acc} );
105             }
106              
107 4         2308 return $obj;
108             }
109              
110             =head1 ACCESSORS
111              
112             All objects returned by the above methods are from the
113             C class, which subclasses
114             C.
115              
116             The most important methods are described in the synopsis, but
117             you should refer to the C manpage for more
118             extensive documentation.
119              
120             Note that it is possible to declare methods into the
121             C class to extend the functionality
122             of the objects returned by C, as also
123             described in the C
124              
125             =cut
126              
127             ### a full on inherited O::A class
128             package DBIx::Simple::OO::Item;
129              
130 3     3   47 use base 'Object::Accessor';
  3         7  
  3         3760  
131              
132             1;
133              
134             =head1 BUG REPORTS
135              
136             Please report bugs or other issues to Ebug-dbix-simple-oo@rt.cpan.org.
137              
138             =head1 AUTHOR
139              
140             This module by Jos Boumans Ekane@cpan.orgE.
141              
142             =head1 COPYRIGHT
143              
144             This library is free software; you may redistribute and/or modify it
145             under the same terms as Perl itself.
146             =cut