Re: Pagers and Iterators

[prev] [thread] [next] [Date index for 2005/02/25]

From: William Ross
Subject: Re: Pagers and Iterators
Date: 14:04 on 25 Feb 2005
On 25 Feb 2005, at 13:23, Carl Johnstone wrote:

>> Not really. The munging is actually a live calculation. Sorry, I 
>> should
>> really have been much clearer when I simplified the example.
>>
>> The easiest thing I could think of would be to create a new
>> Class::DBI::Iterator but the new method of that does not look designed
>> to be called manually.
>
> If you need to calculate something for each row to be able to sort, 
> you're not going to save yourself anything by using an iterator - 
> you'll already have created objects for every row in the DB.

that's true, but with some care it won't cost much extra either, and it 
has the useful consequence that you can use a single list-sorting 
component at the template level rather than maintaining lots of 
separate-but-similar sort operations in your data classes.

it's easy to build an iterator:

   my $it = Class::DBI::Iterator->new( $object_class, [ 
object_or_id_list ], @mappers );

where @mappers is a list of method names that you want called on each 
object as it is pulled from the list. You can also dip into cdbi's 
normal processes a bit earlier:

   my $it = $object_class->_ids_to_objects( [object_or_id_list] );

but it's _private and to my eyes less easily understood, since they're 
not ids at all.

I don't think this is a particularly expensive thing to do. I'd like to 
see a tweak to CDBI::_init so that if its $data hashref is already 
blessed into the right class it just returns it, but the object stash 
accomplishes the same thing once it has calculated the object's unique 
id.

best

will




> As far as getting your paging working correctly, use Data::Page 
> directly:
>
>  my $page = Data::Page->new($total_entries, $entries_per_page, 
> $current_page);
>
>  my @sorted = sort { $a->foo cmp $b->foo } Class->retrieve_all
>  my @wanted = $page->splice(\@sorted);
>
>
>
> Carl
>

(message missing)

Pagers and Iterators
Simon Wistow 11:30 on 25 Feb 2005

Re: Pagers and Iterators
Daniel Wijnands 11:34 on 25 Feb 2005

Re: Pagers and Iterators
Simon Wistow 11:47 on 25 Feb 2005

Re: Pagers and Iterators
Sean Davis 11:55 on 25 Feb 2005

Re: Pagers and Iterators
Simon Wistow 12:14 on 25 Feb 2005

Re: Pagers and Iterators
Carl Johnstone 13:23 on 25 Feb 2005

Re: Pagers and Iterators
Cees Hek 14:18 on 25 Feb 2005

Re: Pagers and Iterators
Matt S Trout 14:35 on 25 Feb 2005

Re: Pagers and Iterators
William Ross 12:15 on 25 Feb 2005

Re: Pagers and Iterators
William Ross 14:04 on 25 Feb 2005

Re: Pagers and Iterators
Simon Wistow 15:37 on 25 Feb 2005

Re: Pagers and Iterators
Simon Wistow 16:34 on 25 Feb 2005

Re: Pagers and Iterators
Ofer Nave 20:52 on 25 Feb 2005

Re: Pagers and Iterators
Cees Hek 16:41 on 25 Feb 2005

Re: Pagers and Iterators
Carl Johnstone 17:09 on 25 Feb 2005

Generated at 17:56 on 01 Mar 2005 by mariachi v0.52