1 package SL::Clipboard::Base;
5 use parent qw(Rose::Object);
7 use Rose::Object::MakeMethods::Generic (
8 'scalar --get_set_init' => [ qw(content timestamp) ],
11 use Rose::DB::Object::Helpers ();
13 sub init_timestamp { die "'timestamp' property not set"; }
14 sub init_content { die "'content' property not set"; }
17 my ($self_or_class) = @_;
18 return (split m/::/, ref($self_or_class) ? ref($self_or_class) : $self_or_class)[-1];
22 my ($self, $object) = @_;
24 return ref($object)->new(map { $_ => $object->$_ } $object->meta->primary_key)->load;
28 my ($self, $object, %params) = @_;
30 my $tree = Rose::DB::Object::Helpers::as_tree($object, %params);
31 $self->_fix_tree($tree, $object);
37 my $object = Rose::DB::Object::Helpers::new_from_tree("SL::DB::" . $self->type, $self->content);
39 # Reset primary key columns and itime/mtime if the class supports it.
40 foreach ($object->meta->primary_key, 'itime', 'mtime') {
41 $object->$_(undef) if $object->can($_);
44 # Let sub classes fix the objects further.
45 $self->_fix_object($object);
50 my ($self, $object) = @_;
51 return $self->as_tree($self->reload_object($object), max_depth => 1);
55 die "'describe' method not overwritten by derived class";
59 my ($self, $object) = @_;
60 # To be overwritten by child classes.
64 my ($self, $tree, $object) = @_;
66 # Delete primary key columns and itime/mtime if the class supports it.
67 foreach ($object->meta->primary_key, 'itime', 'mtime') {
68 delete $tree->{$_} if $object->can($_);
81 SL::Clipboard::Base - Base class for clipboard specialization classes
85 See the synopsis of L<SL::Clipboard>.
89 This is a base class providing a lot of utility and
90 defaults. Sub-classes must overwrite at least the function
91 L</describe> but can overwrite others as well.
93 Writing a specialized sub-class for a database type involves
94 overwriting one or more functions. These are:
100 Must be overwritten. Returns a human-readable description of the
101 content. Should only be one line.
105 Optional. Overwrite if sub-class needs to dump more/less than the
106 implementation in this class dumps.
108 =item * C<_fix_object>
110 Optional. Overwrite if re-created Rose::DB::Object instances must be
111 cleaned further before they're returned to the caller.
115 Optional. Overwrite if the tree created during a copy operation of a
116 Rose::DB::Object instance must be cleaned further before it's stored.
120 You don't have to or should not overwrite the other functions:
126 =item * C<reload_object>
134 Don't forget to C<use> the specialized module here in Base!
140 =item C<as_tree $object, %params>
142 A convenience function calling L<Rose::DB::Object::Helpers/as_tree>
143 with C<$object> and C<%params> as parameters. Returns a hash/array
144 reference tree of the function.
146 Don't overwrite this function in sub-classes. Overwrite L</dump>
151 Returns a human-readable description of the content. This should only
152 be a single line without any markup.
154 Sub-classes must overwrite this function.
156 =item C<dump $object>
158 Dumps the object as a hash/array tree and returns it by calling
159 L<Rose::DB::Object::Helpers/as_tree>. The default implementation
160 reloads the object first by calling L</reload_object>. It also only
161 dumps the object itself, not any of the relationships, by calling
162 C<as_tree> with the parameter C<max_depth =E<gt> 1>.
164 Overwrite this in a sub-class if you need to dump more or differently
165 (see L<SL::Clipboard::RequirementSpecItem> for an example).
167 =item C<reload_object $object>
169 Reloads C<$object> from the database and returns a new instance. Can
170 be useful for sanitizing the object given to L</dump> before
171 converting into a tree. It is used by the default implementation of
176 Converts the dumped representation back to an Rose::DB::Object
177 instance. Several columns of the newly created object are cleared by
178 C<to_object> itself: the primary key columns (if any) and the columns
179 C<itime> and C<mtime> (if the object has such columns).
181 This function should not be overwritten by sub-classes. Instead,
182 functions can overwrite C<_fix_object> which can be used for sanitzing
183 the newly created object before handing it back to the caller.
187 Returns the actual clipped type (e.g. C<RequirementSpecItem>). This is
188 derived from the actual class name of C<$self>.
190 =item C<_fix_object $object>
192 This function is called by L</to_object> before the object is passed
193 back to the caller. It does not do anything in the default
194 implementation, but sub-classes are free to overwrite it if they need
195 to sanitize the object. See L<SL::Clipboard::RequirementSpecItem> for
198 Its return value is ignored.
200 =item C<_fix_tree $tree, $object>
202 This function is called by L</as_tree> after dumping and before the
203 object is stored during a copy operation. In the default
204 implementation all primary key columns and the columns C<itime> and
205 C<mtime> (if the object has such columns) are removed from the tree.
206 Sub-classes are free to overwrite it if they need to sanitize the
207 tree. See L<SL::Clipboard::RequirementSpecItem> for an example.
209 C<$object> is just passed in for reference and should not be modified.
211 Its return value is ignored.
221 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>