Quick test includes Record::OPAC rather than Record::Fetch
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Task.pm
1
2 package ZOOM::IRSpy::Task;
3
4 use 5.008;
5 use strict;
6 use warnings;
7
8 use Scalar::Util;
9
10 =head1 NAME
11
12 ZOOM::IRSpy::Task - base class for tasks in IRSpy
13
14 =head1 SYNOPSIS
15
16  use ZOOM::IRSpy::Task;
17  package ZOOM::IRSpy::Task::SomeTask;
18  our @ISA = qw(ZOOM::IRSpy::Task);
19  # ... override methods
20
21 =head1 DESCRIPTION
22
23 This class provides a base-class from which individual IRSpy task
24 classes can be derived.  For example, C<ZOOM::IRSpy::Task::Search>
25 will represent a searching task, carrying with it a query, a pointer
26 to a result-set, etc.
27
28 The base class provides nothing more exciting than a link to a
29 callback function to be called when the task is complete, and a
30 pointer to the next task to be performed after this.
31
32 =cut
33
34 sub new {
35     my $class = shift();
36     my($conn, $udata, $options, %cb) = @_;
37
38     my $this = bless {
39         irspy => $conn->{irspy},
40         conn => $conn,
41         udata => $udata,
42         options => $options,
43         cb => \%cb,
44         timeRegistered => time(),
45     }, $class;
46
47     #Scalar::Util::weaken($this->{irspy});
48     #Scalar::Util::weaken($this->{udata});
49
50     return $this;
51 }
52
53
54 sub irspy {
55     my $this = shift();
56     return $this->{irspy};
57 }
58
59 sub conn {
60     my $this = shift();
61     return $this->{conn};
62 }
63
64 sub udata {
65     my $this = shift();
66     return $this->{udata};
67 }
68
69 sub run {
70     my $this = shift();
71     die "can't run base-class task $this";
72 }
73
74 # In general, this sets the Connection's options to what is in the
75 # task's option hash and sets the option-hash entry to the
76 # Connection's old value of the option: this means that calling this
77 # twice restores the state to how it was before.  (That's not quite
78 # true as its impossible to unset an option that was previously set:
79 # such options are instead set to the empty string.)
80 #
81 # As a special case, options in the task's option-hash whose names
82 # begin with an asterisk are taken to be persistent: they are set into
83 # the Connection (with the leading asterisk removed) and deleted from
84 # the task's option-hash so that they will NOT be reset the next time
85 # this function is called.
86 #
87 sub set_options {
88     my $this = shift();
89
90     foreach my $key (sort keys %{ $this->{options} }) {
91         my $value = $this->{options}->{$key};
92         my $persistent = ($key =~ s/^\*//);
93         $value = "" if !defined $value;
94         $this->conn()->log("irspy_debug", "$this setting option '$key' -> ",
95                            defined $value ? "'$value'" : "undefined");
96         my $old = $this->conn()->option($key, $value);
97         if ($persistent) {
98             delete $this->{options}->{"*$key"}
99         } else {
100             $this->{options}->{$key} = $old;
101         }
102     }
103 }
104
105 sub render {
106     my $this = shift();
107     return "[base-class] " . ref($this);
108 }
109
110 use overload '""' => \&render;
111
112
113 =head1 SEE ALSO
114
115 ZOOM::IRSpy
116
117 =head1 AUTHOR
118
119 Mike Taylor, E<lt>mike@indexdata.comE<gt>
120
121 =head1 COPYRIGHT AND LICENSE
122
123 Copyright (C) 2006 by Index Data ApS.
124
125 This library is free software; you can redistribute it and/or modify
126 it under the same terms as Perl itself, either Perl version 5.8.7 or,
127 at your option, any later version of Perl 5 you may have available.
128
129 =cut
130
131 1;