--- trunk/lib/WebPAC/Normalize.pm 2007/11/12 11:10:48 1036 +++ trunk/lib/WebPAC/Normalize.pm 2011/12/15 21:40:37 1367 @@ -7,6 +7,7 @@ _debug _pack_subfields_hash + to search_display search display sorted rec1 rec2 rec @@ -20,6 +21,9 @@ get set count + row + rec_array + /; use warnings; @@ -27,11 +31,11 @@ #use base qw/WebPAC::Common/; use Data::Dump qw/dump/; -use Storable qw/dclone/; use Carp qw/confess/; # debugging warn(s) my $debug = 0; +_debug( $debug ); # FIXME use WebPAC::Normalize::ISBN; @@ -46,13 +50,15 @@ marc_template /); +use Storable qw/dclone/; + =head1 NAME WebPAC::Normalize - describe normalisaton rules using sets =cut -our $VERSION = '0.35'; +our $VERSION = '0.36'; =head1 SYNOPSIS @@ -117,7 +123,7 @@ $load_row_coderef = $arg->{load_row_coderef}; no strict 'subs'; - no warnings 'redefine'; + no warnings 'all'; eval "$arg->{rules};"; die "error evaling $arg->{rules}: $@\n" if ($@); @@ -139,7 +145,7 @@ $WebPAC::Normalize::MARC::rec = $rec; } -=head2 +=head2 _get_rec my $rec = _get_rec(); @@ -147,6 +153,14 @@ sub _get_rec { $rec }; +=head2 _set_rec + + _set_rec( $rec ); + +=cut + +sub _set_rec { $rec = $_[0] } + =head2 _set_config Set current config hash @@ -269,13 +283,30 @@ Those functions generally have to first in your normalization file. +=head2 to + +Generic way to set values for some name + + to('field-name', 'name-value' => rec('200','a') ); + +There are many helpers defined below which might be easier to use. + +=cut + +sub to { + my $type = shift or confess "need type -- BUG?"; + my $name = shift or confess "needs name as first argument"; + my @o = grep { defined($_) && $_ ne '' } @_; + return unless (@o); + $out->{$name}->{$type} = \@o; +} + =head2 search_display Define output for L and L at the same time search_display('Title', rec('200','a') ); - =cut sub search_display { @@ -288,7 +319,7 @@ =head2 tag -Old name for L, but supported +Old name for L, it will probably be removed at one point. =cut @@ -304,15 +335,7 @@ =cut -sub _field { - my $type = shift or confess "need type -- BUG?"; - my $name = shift or confess "needs name as first argument"; - my @o = grep { defined($_) && $_ ne '' } @_; - return unless (@o); - $out->{$name}->{$type} = \@o; -} - -sub display { _field( 'display', @_ ) } +sub display { to( 'display', @_ ) } =head2 search @@ -322,7 +345,7 @@ =cut -sub search { _field( 'search', @_ ) } +sub search { to( 'search', @_ ) } =head2 sorted @@ -332,8 +355,23 @@ =cut -sub sorted { _field( 'sorted', @_ ) } +sub sorted { to( 'sorted', @_ ) } +=head2 row + +Insert new row of data into output module + + row( column => 'foo', column2 => 'bar' ); + +=cut + +use Data::Dump qw/dump/; + +sub row { + die "array doesn't have odd number of elements but $#_: ",dump( @_ ) if $#_ % 2 == 1; + my $table = shift @_; + push @{ $out->{'_rows'}->{$table} }, {@_}; +} =head1 Functions to extract data from input @@ -355,10 +393,12 @@ warn "## _pack_subfields_hash( ",dump(@_), " )\n" if ($debug > 1); - my ($h,$include_subfields) = @_; + my ($hash,$include_subfields) = @_; # sanity and ease of use - return $h if (ref($h) ne 'HASH'); + return $hash if (ref($hash) ne 'HASH'); + + my $h = dclone( $hash ); if ( defined($h->{subfields}) ) { my $sfs = delete $h->{subfields} || die "no subfields?"; @@ -449,7 +489,7 @@ } else { $_->{$sf}; } - } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; + } grep { ref($_) eq 'HASH' && defined $_->{$sf} } @{ $rec->{$f} }; } =head2 rec @@ -918,5 +958,21 @@ return @_ . ''; } +=head2 rec_array + +Always return field as array + + foreach my $d ( rec_array('field') ) { + warn $d; + } + +=cut + +sub rec_array { + my $d = $rec->{ $_[0] }; + return @$d if ref($d) eq 'ARRAY'; + return ($d); +} + # END 1;