| 1 | #!perl | ||
| 2 | |||
| 3 | # ########################################################################## # | ||
| 4 | # Title: Hash to datastream generator | ||
| 5 | # Creation date: 2007-03-05 | ||
| 6 | # Author: Michael Zedeler | ||
| 7 | # Description: Produces a datastream from a hash reference | ||
| 8 | # Data Stream class | ||
| 9 | # Data importer | ||
| 10 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/Importer/Hash.pm,v $ | ||
| 11 | # Repository: kronhjorten | ||
| 12 | # State: $State: Exp $ | ||
| 13 | # Documentation: inline | ||
| 14 | # Recepient: Dataservice | ||
| 15 | # ########################################################################## # | ||
| 16 | |||
| 17 | package DS::Importer::Hash; | ||
| 18 | |||
| 19 | use base qw{ DS::Importer }; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use Carp::Assert; | ||
| 23 | |||
| 24 | our ($VERSION) = $DS::VERSION; | ||
| 25 | our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/; | ||
| 26 | |||
| 27 | |||
| 28 | use constant DS_IMPORTER_HASH_INIT => 1; | ||
| 29 | use constant DS_IMPORTER_HASH_FETCHED => 2; | ||
| 30 | use constant DS_IMPORTER_HASH_DONE => 3; | ||
| 31 | |||
| 32 | #TODO Get type spec from keys in hash | ||
| 33 | sub new { | ||
| 34 | my( $class, $data_source, $target, $typespec) = @_; | ||
| 35 | |||
| 36 | my $self = $class::SUPER->new( $typespec, $target ); | ||
| 37 | |||
| 38 | assert(ref($data_source) eq 'HASH'); | ||
| 39 | $self->{data_source} = {%$data_source}; | ||
| 40 | $self->{state} = DS_IMPORTER_HASH_INIT; | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _fetch { | ||
| 46 | my($self) = @_; | ||
| 47 | |||
| 48 | my $result = undef; | ||
| 49 | |||
| 50 | if($self->{state} == DS_IMPORTER_HASH_INIT) { | ||
| 51 | %{$self->{row}} = %{$self->{data_source}}; | ||
| 52 | $self-> {state} = DS_IMPORTER_HASH_FETCHED; | ||
| 53 | $result = 1; | ||
| 54 | } elsif($self->{state} == DS_IMPORTER_HASH_FETCHED) { | ||
| 55 | %{$self->{row}} = (); | ||
| 56 | $self-> {state} = DS_IMPORTER_HASH_DONE; | ||
| 57 | } | ||
| 58 | |||
| 59 | return $result ? $self->{row} : undef; | ||
| 60 | } | ||
| 61 | |||
| 62 | 1; | ||
| 63 | |||
| 64 |