43 |
|
|
44 |
=head2 take_action |
=head2 take_action |
45 |
|
|
46 |
|
Create C<results> which can be accessed from L<Template::Declare> like this: |
47 |
|
|
48 |
|
my $results = $search->result->content('results'); |
49 |
|
|
50 |
=cut |
=cut |
51 |
|
|
52 |
sub take_action { |
sub take_action { |
109 |
|
|
110 |
my $store; |
my $store; |
111 |
|
|
112 |
|
=head2 count |
113 |
|
|
114 |
|
Returns number of records in results |
115 |
|
|
116 |
|
my $nr_records = $results->count; |
117 |
|
|
118 |
|
=head2 next |
119 |
|
|
120 |
|
Fetch next result and return C<Webpacus::Search::DS> object |
121 |
|
|
122 |
|
while ( my $ds = $results->next ) { |
123 |
|
# do something with $ds object |
124 |
|
} |
125 |
|
|
126 |
|
=cut |
127 |
|
|
128 |
sub next { |
sub next { |
129 |
my $self = shift; |
my $self = shift; |
130 |
|
|
152 |
return; |
return; |
153 |
} |
} |
154 |
|
|
155 |
my $row; |
# add permanent fields |
156 |
|
$ds->{$_} ||= { display => $hit->{$_} } foreach ( qw/database input id/ ); |
157 |
|
|
158 |
foreach my $f ( keys %$ds ) { |
return Webpacus::Action::DS->new( $ds ); |
159 |
$row->{$f} = $ds->{$f}->{display} if defined $ds->{$f}->{display}; |
|
160 |
} |
} |
161 |
|
|
162 |
|
package Webpacus::Action::DS; |
163 |
|
|
164 |
|
use warnings; |
165 |
|
use strict; |
166 |
|
|
167 |
|
use Data::Dump qw/dump/; |
168 |
|
use Carp qw/confess/; |
169 |
|
|
170 |
return unless $row; |
sub new { |
171 |
|
my $class = shift; |
172 |
|
|
173 |
$row->{$_} ||= $hit->{$_} foreach ( qw/database input id/ ); |
my ( $ds ) = @_; |
174 |
|
|
175 |
warn "## next row = ", dump( $row ) if $debug; |
my $self = { |
176 |
|
ds => $ds, |
177 |
|
}; |
178 |
|
bless ($self, $class); |
179 |
|
|
180 |
return sub { |
return $self; |
181 |
my ( $name, $delimiter ) = @_; |
|
182 |
# default delimiter is space |
} |
183 |
$delimiter ||= ' '; |
|
184 |
die "no name?" unless $name; |
sub _row { |
185 |
if ( defined $row->{$name} ) { |
my ( $self, $type, $field, $delimiter ) = @_; |
186 |
# XXX disabled, Template::Declare always want scalars :-( |
|
187 |
if ( 0 && wantarray ) { |
confess "no type?" unless $type; |
188 |
if ( ref($row->{$name}) eq 'ARRAY' ) { |
confess "no field?" unless $field; |
189 |
return $row->{$name}; |
confess "no ds?" unless $self->{ds}; |
190 |
} else { |
|
191 |
return [ $row->{$name} ]; |
$delimiter ||= '[x]'; |
192 |
} |
|
193 |
} else { |
my $ds = $self->{ds}; |
194 |
if ( ref($row->{$name}) eq 'ARRAY' && $delimiter ne 'ARRAY' ) { |
|
195 |
warn "is array ", wantarray ? 'wantarray' : 'scalar', " $name"; |
warn "### ds = ",dump( $ds ) if $debug; |
196 |
return @{$row->{$name}} if wantarray; |
|
197 |
return join( $delimiter, @{ $row->{$name} }); |
return unless defined $ds->{$field}->{$type}; |
198 |
} else { |
|
199 |
warn "not array ", wantarray ? 'wantarray' : 'scalar', " $name"; |
if ( my $v = $ds->{$field}->{$type} ) { |
200 |
return $row->{$name}; |
|
201 |
} |
if ( ref($v) eq 'ARRAY' ) { |
202 |
} |
warn "is array ", wantarray ? 'wantarray' : 'scalar', " $field $type" if $debug; |
203 |
|
return @$v if wantarray; |
204 |
|
return join( $delimiter, @$v ); |
205 |
|
} else { |
206 |
|
warn "not array ", wantarray ? 'wantarray' : 'scalar', " $field $type" if $debug; |
207 |
|
return $v; |
208 |
} |
} |
209 |
|
|
210 |
|
} else { |
211 |
return; |
return; |
212 |
} |
} |
213 |
|
|
214 |
|
} |
215 |
|
|
216 |
|
=head2 display |
217 |
|
|
218 |
|
$ds->display( 'TitleProper' ); |
219 |
|
|
220 |
|
=cut |
221 |
|
|
222 |
|
sub display { |
223 |
|
my $self = shift; |
224 |
|
warn "## display(",dump(@_),")\n"; |
225 |
|
return $self->_row('display',@_); |
226 |
} |
} |
227 |
|
|
228 |
1; |
1; |