/[Frey]/trunk/lib/Frey/Collection.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/Collection.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 143 - (show annotations)
Wed Jul 16 14:17:46 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 1850 byte(s)
huge refactor of code into more distinct classes

- Frey::Collection is now generally reusable from browser and designer
- Frey::Web::Item is now (bare) class instead of role
- Frey::Web::Layout now encapsulates different layouts for items
1 package Frey::Collection;
2 use Moose::Role;
3
4 use Data::Dump qw/dump/;
5 use Carp;
6
7 =head1 NAME
8
9 Frey::Collection - role for Fey::ORM to get all results
10
11 =head1 METHODS
12
13 =cut
14
15 =head2 total_entries
16
17 my $rows = $o->total_entries;
18
19 =cut
20
21 sub total_entries {
22 my $self = shift;
23 my $table = $self->_table;
24 my $count = Fey::Literal::Function->new( 'COUNT', $table->column('id') ); # FIXME id?
25 my $select = $self->_sql->select( $count )->from( $self->_table );
26 my $dbh = $self->_dbh($select);
27 my $sth = $dbh->prepare( $select->sql($dbh) );
28 $sth->execute;
29 return $sth->fetchrow_arrayref->[0];
30 }
31
32 sub _table {
33 my $self = shift;
34 return $self->SchemaClass()->Schema()->table( $self->collection_table );
35 }
36
37 sub _sql {
38 my $self = shift;
39 return $self->SchemaClass()->SQLFactoryClass()->new_select();
40 }
41
42 =head2 collection
43
44 my $pager = Frey::Pager->new( per_page => 20, page => 1 );
45 my $Fey_Object_Iterator = $o->collection($pager);
46
47 =cut
48
49 sub collection {
50 my ( $self, $pager ) = @_;
51
52 my $table = $self->_table;
53
54 my $select = $self->_sql->select( $table )
55 ->from( $table )
56 # ->where( $message_t->column('message_date'), '>=',
57 # DateTime->today()->subtract( days => 7 )->strftime( '%Y-%m-%d' ) )
58 ;
59
60 if ( blessed $pager ) {
61 if ( ref($pager) eq 'Frey::Pager' ) {
62 warn "FIXING wrong pager object to collection!";
63 $pager = $pager->pager;
64 }
65 warn "## using pager";
66 $pager->total_entries( $self->total_entries );
67 $select->limit( $pager->entries_per_page, $pager->skipped );
68 }
69
70 my $dbh = $self->_dbh($select);
71 my $sth = $dbh->prepare( $select->sql($dbh) );
72
73 my $i =
74 Fey::Object::Iterator->new(
75 classes => [ $self->meta()->ClassForTable( $table ) ],
76 handle => $sth,
77 bind_params => [ $select->bind_params() ],
78 );
79
80 confess "LEGACY: this calling convention is no longer supported" if wantarray;
81 return $i;
82 }
83
84 1;

  ViewVC Help
Powered by ViewVC 1.1.26