File Coverage

blib/lib/Rose/DB/Object/MakeMethods/Std.pm
Criterion Covered Total %
statement 6 9 66.6
branch n/a
condition 0 3 0.0
subroutine 2 3 66.6
pod 1 1 100.0
total 9 16 56.2


line stmt bran cond sub pod time code
1             package Rose::DB::Object::MakeMethods::Std;
2              
3 1     1   1282 use strict;
  1         2  
  1         33  
4              
5 1     1   5 use Rose::DB::Object::MakeMethods::Generic();
  1         2  
  1         130  
6             our @ISA = qw(Rose::DB::Object::MakeMethods::Generic);
7              
8             our $VERSION = '0.011';
9              
10             sub object_by_id
11             {
12 0     0 1   my($class, $name, $args, $options) = @_;
13              
14             $args->{'key_columns'} =
15             {
16 0   0       ($args->{'id_method'} || $name . '_id') => 'id',
17             };
18              
19 0           $class->object_by_key($name, $args, $options);
20             }
21              
22             1;
23              
24             __END__
25              
26             =head1 NAME
27              
28             Rose::DB::Object::MakeMethods::Std - Create object methods related to Rose::DB::Object::Std-derived objects.
29              
30             =head1 SYNOPSIS
31              
32             package Category;
33             our @ISA = qw(Rose::DB::Object::Std);
34             ...
35              
36             package Color;
37             our @ISA = qw(Rose::DB::Object::Std);
38             ...
39              
40             package Product;
41             our @ISA = qw(Rose::DB::Object);
42             ...
43              
44             use Rose::DB::Object::MakeMethods::Std
45             (
46             object_by_id =>
47             [
48             color => { class => 'Color' },
49              
50             category =>
51             {
52             class => 'Category',
53             id_method => 'cat_id',
54             share_db => 0,
55             },
56             ],
57             );
58              
59             ...
60              
61             $prod = Product->new(...);
62              
63             $color = $prod->color;
64              
65             # $prod->color call is roughly equivalent to:
66             #
67             # $color = Color->new(id => $prod->color_id,
68             # db => $prod->db);
69             # $ret = $color->load;
70             # return $ret unless($ret);
71             # return $color;
72              
73             $cat = $prod->category;
74              
75             # $prod->category call is roughly equivalent to:
76             #
77             # $cat = Category->new(id => $prod->cat_id);
78             # $ret = $cat->load;
79             # return $ret unless($ret);
80             # return $cat;
81              
82             =head1 DESCRIPTION
83              
84             C<Rose::DB::Object::MakeMethods::Std> creates methods related to Rose::DB::Object::Std-derived objects. It inherits from L<Rose::Object::MakeMethods>. See the L<Rose::Object::MakeMethods> documentation to learn about the interface. The method types provided by this module are described below.
85              
86             All method types defined by this module are designed to work with objects that are subclasses of (or otherwise conform to the interface of) L<Rose::DB::Object>. In particular, the object is expected to have a C<db> method that returns a L<Rose::DB>-derived object. See the L<Rose::DB::Object::Std> documentation for more details.
87              
88             =head1 METHODS TYPES
89              
90             =over 4
91              
92             =item B<object_by_id>
93              
94             Create a get/set methods for a single L<Rose::DB::Object::Std>-derived object loaded based on a primary key stored in an attribute of the current object.
95              
96             =over 4
97              
98             =item Options
99              
100             =over 4
101              
102             =item C<class>
103              
104             The name of the L<Rose::DB::Object::Std>-derived class of the object to be loaded. This option is required.
105              
106             =item C<hash_key>
107              
108             The key inside the hash-based object to use for the storage of the object. Defaults to the name of the method.
109              
110             =item C<id_method>
111              
112             The name of the method that contains the primary key of the object to be loaded. Defaults to the method name concatenated with "_id".
113              
114             =item C<interface>
115              
116             Choose the interface. The only current interface is C<get_set>, which is the default.
117              
118             =item C<share_db>
119              
120             If true, the C<db> attribute of the current object is shared with the object loaded. Defaults to true.
121              
122             =back
123              
124             =item Interfaces
125              
126             =over 4
127              
128             =item C<get_set>
129              
130             Creates a method that will attempt to create and load a L<Rose::DB::Object::Std>-derived object based on a primary key stored in an attribute of the current object.
131              
132             If passed a single argument of undef, the C<hash_key> used to store the object is set to undef. Otherwise, the argument is assumed to be an object of type C<class> and is assigned to C<hash_key> after having its primary key set to the corresponding value in the current object.
133              
134             If called with no arguments and the C<hash_key> used to store the object is defined, the object is returned. Otherwise, the object is created and loaded.
135              
136             The load may fail for several reasons. The load will not even be attempted if the primary key attribute in the current object is undefined. Instead, undef will be returned. If the call to the newly created object's C<load> method returns false, that false value is returned.
137              
138             If the load succeeds, the object is returned.
139              
140             =back
141              
142             =back
143              
144             Example:
145              
146             package Category;
147             our @ISA = qw(Rose::DB::Object::Std);
148             ...
149              
150             package Color;
151             our @ISA = qw(Rose::DB::Object::Std);
152             ...
153              
154             package Product;
155             our @ISA = qw(Rose::DB::Object);
156             ...
157              
158             use Rose::DB::Object::MakeMethods::Std
159             (
160             object_by_id =>
161             [
162             color => { class => 'Color' },
163              
164             category =>
165             {
166             class => 'Category',
167             id_method => 'cat_id',
168             share_db => 0,
169             },
170             ],
171             );
172              
173             ...
174              
175             $prod = Product->new(...);
176              
177             $color = $prod->color;
178              
179             # $prod->color call is roughly equivalent to:
180             #
181             # $color = Color->new(id => $prod->color_id,
182             # db => $prod->db);
183             # $ret = $color->load;
184             # return $ret unless($ret);
185             # return $color;
186              
187             $cat = $prod->category;
188              
189             # $prod->category call is roughly equivalent to:
190             #
191             # $cat = Category->new(id => $prod->cat_id);
192             # $ret = $cat->load;
193             # return $ret unless($ret);
194             # return $cat;
195              
196             =back
197              
198             =head1 AUTHOR
199              
200             John C. Siracusa (siracusa@gmail.com)
201              
202             =head1 LICENSE
203              
204             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is
205             free software; you can redistribute it and/or modify it under the same terms
206             as Perl itself.