From e6810ca7ee786765e9178a47fb2dc891cdb4f3ba Mon Sep 17 00:00:00 2001 From: Lars Dieckow Date: Thu, 27 Jun 2013 12:48:54 +0200 Subject: [PATCH] add component for JSON serialisation hooks --- Build.PL | 2 ++ bin/ngcp-dump-schema | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Build.PL b/Build.PL index f4b46917..791efd66 100644 --- a/Build.PL +++ b/Build.PL @@ -8,6 +8,7 @@ my $builder = Module::Build->new( 'aliased' => 0, 'DBD::mysql' => 0, 'DBIx::Class::EncodedColumn' => 0, + 'DBIx::Class::Helper::Row::ToJSON' => 0, 'DBIx::Class::Schema::Loader' => 0, 'File::Path' => 0, 'Getopt::Long' => 0, @@ -20,6 +21,7 @@ my $builder = Module::Build->new( 'Pod::Usage' => 0, 'Regexp::Common' => 0, 'Regexp::IPv6' => 0, + 'Scalar::Util' => 0, 'Sipwise::Base' => '1.002', 'Throwable::Error' => 0, 'XML::Simple' => 0, diff --git a/bin/ngcp-dump-schema b/bin/ngcp-dump-schema index a97755ae..a529df58 100644 --- a/bin/ngcp-dump-schema +++ b/bin/ngcp-dump-schema @@ -6,6 +6,15 @@ use Getopt::Long qw(GetOptions); use Quantum::Superpositions qw(any); use Pod::Usage qw(pod2usage); +my $result_class_code = <<'END'; +sub TO_JSON { + my ($self) = @_; + return { + map { blessed($_) && $_->isa('DateTime') ? $_->datetime : $_ } %{ $self->next::method } + }; +} +END + my %opt = ( dump_directory => 'lib', ); @@ -56,9 +65,10 @@ for my $db (qw(accounting billing carrier kamailio ngcp provisioning sipstats)) @source = ( $package, "use Sipwise::Base;\n", - ($package =~ /::Result::/ ? "use MooseX::NonMoose;\n" : ()), + ($package =~ /::Result::/ ? "use MooseX::NonMoose;\nuse Scalar::Util qw(blessed);\n" : ()), "our \$VERSION = '$opt{version}';\n", @source, + ($package =~ /::Result::/ ? $result_class_code : ()), ); @pod = ("=encoding UTF-8\n\n", @pod, "=cut\n") if @pod; return join '', @source, @pod; @@ -68,7 +78,7 @@ for my $db (qw(accounting billing carrier kamailio ngcp provisioning sipstats)) my ($column) = @_; return $CLASS->can($column) ? "column_$column" : $column; }, - components => [qw(InflateColumn::DateTime)], + components => [qw(InflateColumn::DateTime Helper::Row::ToJSON)], quiet => 1, use_moose => 1, },