use strict;
use Carp qw/confess/;
+use Data::Dump qw/dump/;
=head1 NAME
WebPAC::Output - The great new WebPAC::Output!
-=head1 VERSION
-
-Version 0.01
-
=cut
-our $VERSION = '0.01';
-
=head1 SYNOPSIS
Common routines for output formats
=head2 ds_to_hash
my $hash = $self->ds_to_hash( $ds, 'display'
- disable_key_mungle => 1,
+ disable_key_mungle => 0,
+ singe_values = 0,
);
=cut
$name =~ s/\W+/_/g;
}
- # FIXME get rid of non hash values in data_structure for consistency?
- next unless ref($ds->{$t}) eq 'HASH';
+ my $v = $ds->{$t} || die "bug";
- if ( defined( $ds->{$t}->{$type} ) ) {
- $hash->{$name} = $ds->{$t}->{$type};
+ # FIXME get rid of non hash values in data_structure for consistency?
+ next unless ref($v) eq 'HASH';
+
+ if ( defined( $v->{$type} ) ) {
+ if ( $opt->{single_values} && ref($v->{$type}) eq 'ARRAY' ) {
+ $hash->{$name} = join(' ', map {
+ if(ref($_)) {
+ dump($_);
+ } else {
+ $_;
+ }
+ } @{ $v->{$type} });
+ } else {
+ $hash->{$name} = $v->{$type};
+ }
}
}
--- /dev/null
+#!/usr/bin/perl -w
+
+use strict;
+use blib;
+
+use Test::More tests => 8;
+
+BEGIN {
+use_ok( 'WebPAC::Test' );
+use_ok( 'WebPAC::Output' );
+}
+
+my $ds = {
+ '000' => { foo => [ 42 ], bar => 'bug!' },
+ '900' => { foo => [
+ { a => '900a1', b => '900b1' },
+ { a => '900a2', b => '900b2' },
+ { a => '900a3', b => '900b3' },
+ ] },
+ '902' => { foo => [
+ { a => '900a1', b => '900b1' },
+ ] },
+
+ 'StRaNgE!Field' => { foo => 'value' },
+
+ 'array' => { foo => [ 'a' .. 'c' ] },
+};
+
+my $hash;
+ok( $hash = WebPAC::Output->ds_to_hash( $ds, 'foo' ), 'ds_to_hash' );
+diag dump($hash) if $debug;
+is_deeply( $hash, {
+ "000" => [42],
+ 900 => [
+ { a => "900a1", b => "900b1" },
+ { a => "900a2", b => "900b2" },
+ { a => "900a3", b => "900b3" },
+ ],
+ 902 => [{ a => "900a1", b => "900b1" }],
+ array => ["a", "b", "c"],
+ strange_field => "value",
+}, 'is_deeply');
+
+ok( $hash = WebPAC::Output->ds_to_hash( $ds, 'foo', disable_key_mungle => 1 ), 'ds_to_hash disable_key_mungle' );
+diag dump($hash) if $debug;
+is_deeply( $hash, {
+ "000" => [42],
+ 900 => [
+ { a => "900a1", b => "900b1" },
+ { a => "900a2", b => "900b2" },
+ { a => "900a3", b => "900b3" },
+ ],
+ 902 => [{ a => "900a1", b => "900b1" }],
+ array => ["a", "b", "c"],
+ 'StRaNgE!Field' => "value",
+}, 'is_deeply');
+
+ok( $hash = WebPAC::Output->ds_to_hash( $ds, 'foo', single_values => 1 ), 'ds_to_hash single_values' );
+diag dump($hash) if $debug;
+is_deeply( $hash, {
+ "000" => 42,
+ 900 => "{ a => \"900a1\", b => \"900b1\" } { a => \"900a2\", b => \"900b2\" } { a => \"900a3\", b => \"900b3\" }",
+ 902 => "{ a => \"900a1\", b => \"900b1\" }",
+ array => "a b c",
+ strange_field => "value",
+}, 'is_deeply');
+
+