package APKPM::Gearman;

use Moose::Role;

use Data::Dump qw(dump);
use JSON::XS;

use DateTime;
use DateTime::Format::Pg;

use Data::Dumper;

sub e_json {
	my ( $self, $result ) = @_;
	warn "# e_json ", dump $result;
	return $result unless ref $result; # FIXME report error?
	return JSON::XS::encode_json($result);
}

sub d_json {
	my ( $self, $workload ) = @_;
	warn "# d_json $workload";
	return JSON::XS::decode_json($workload);
}

sub d_array {
	my ( $self, $result ) = @_;
	warn "# d_array $result";
	return [ split(/\s+/,$result) ];

}

sub datetime_now {
	return DateTime::Format::Pg->format_datetime( DateTime->now );
}

sub to_hstore {
	my ($self,$hash) = @_;

	warn "# hash ",dump $hash;
	local $Data::Dumper::Useqq = 1; # double quote for PostgreSQL
	my $h = Dumper($hash);
	$h =~ s/\s+/ /gs;
	$h =~ s/^\$\w+\s*=\s*{\s*//s;
	$h =~ s/\s*}\s*;\s*$//s;

	return $h;
}

1;
