From 05452ca6896faddad6f28613b90c46dfb32b255d Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 20 Nov 2007 10:08:00 +0000 Subject: [PATCH 1/1] r1649@llin: dpavlin | 2007-11-20 10:32:15 +0100 experimental WebPAC::Output::Jifty to create arbitrary data in Jifty models git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1056 07558da8-63fa-0310-ba24-9fe276d99e06 --- Makefile.PL | 7 ++ lib/WebPAC/Output/Jifty.pm | 142 +++++++++++++++++++++++++++++++++++++ t/5-output-jifty.t | 46 ++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 lib/WebPAC/Output/Jifty.pm create mode 100755 t/5-output-jifty.t diff --git a/Makefile.PL b/Makefile.PL index ad6aca0..0af8011 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -38,6 +38,13 @@ requires 'JSON'; requires 'File::Spec'; requires 'Sort::External'; +features( + 'WebPAC::Output::Webpacus and WebPAC::Output::Jifty' => [ + -default => 0, + recommends('Jifty'), + ], +); + features( 'WebPAC::Input::XML' => [ -default => 0, diff --git a/lib/WebPAC/Output/Jifty.pm b/lib/WebPAC/Output/Jifty.pm new file mode 100644 index 0000000..299fc03 --- /dev/null +++ b/lib/WebPAC/Output/Jifty.pm @@ -0,0 +1,142 @@ +package WebPAC::Output::Jifty; + +use warnings; +use strict; + +use base qw/WebPAC::Common WebPAC::Output Class::Accessor/; +__PACKAGE__->mk_accessors(qw( + path + model +)); + +use File::Path; +use Data::Dump qw/dump/; +use WebPAC::Common qw/force_array/; +use Carp qw/confess/; +use Cwd; +use File::Slurp; + +use Jifty; + +=head1 NAME + +WebPAC::Output::Jifty - fill Jifty model from WebPAC data + +=cut + +our $VERSION = '0.01'; + +=head1 SYNOPSIS + +This is simple output which will fill one Jifty model with data + +=head1 FUNCTIONS + +=head2 new + + my $output = new WebPAC::Output::Jifty({ + path => '/path/to/Jifty', + model => 'Webpacus::Model::UFO', + }); + +=head2 init + + $output->init; + +=cut + +sub init { + my $self = shift; + + my $log = $self->_get_logger; + + foreach my $p (qw/path model/) { + $log->logdie("need $p") unless ($self->$p); + } + + my $path = $self->path; + + $log->logdie("Jifty path $path not found: $!") unless -d $path; + + my $config_path = "$path/etc/config.yml"; + + $log->logdie("expected Jifty config at $config_path: $!") unless -e $config_path; + + my $model = $self->model || confess "no model?"; + + $log->info("init Jifty at $path using model $model"); + my $webpac_dir = getcwd(); + chdir $path || $log->logdie("can't chdir($path): $!"); + Jifty->new(); + chdir $webpac_dir || $log->logdie("can't chdir($webpac_dir): $!"); + + my $system_user = Webpacus::CurrentUser->superuser; + $self->{_model} = $model->new(current_user => $system_user); + +} + + +=head2 add + +Adds one entry + + $output->add( 42, $ds ); + +=cut + +my $stat; + +sub add { + my $self = shift; + + my ( $id, $ds ) = @_; + + my $log = $self->_get_logger; + $log->logdie("need id") unless defined $id; + $log->logdie("need ds") unless $ds; + + $log->debug("id: $id ds = ",sub { dump($ds) }); + + my $stat; + + my $hash = $self->ds_to_hash( $ds, 'display' ) || next; + + $log->debug("data: ", sub { dump( $hash ) }); + + my ( $m_id, $msg ) = $self->{_model}->load_or_create( %$hash ); + $log->debug("ID: $m_id $msg"); + + push @{ $stat->{$msg} }, $m_id; + + return 1; +} + +=head2 finish + + $output->finish; + +=cut + +sub finish { + my $self = shift; + + my $log = $self->_get_logger; + $log->debug("stats: ", dump( $stat )); + + return 1; +} + +=head1 AUTHOR + +Dobrica Pavlinusic, C<< >> + +=head1 COPYRIGHT & LICENSE + +Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=cut + +1; diff --git a/t/5-output-jifty.t b/t/5-output-jifty.t new file mode 100755 index 0000000..e527516 --- /dev/null +++ b/t/5-output-jifty.t @@ -0,0 +1,46 @@ +#!/usr/bin/perl -w + +use strict; +use blib; + +use Test::More tests => 8; + +BEGIN { +use_ok( 'WebPAC::Test' ); +use_ok( 'WebPAC::Output::Jifty' ); +} + +ok(my $out = new WebPAC::Output::Jifty({ + path => '/data/Webpacus2/', + model => 'Webpacus::Model::UFO', + clean => 1, + %LOG +}), "new"); + +ok( $out->init, 'init' ); + +my $ds = { + date => { + display => '2007-11-19', + }, + time => { + display => '23:20:19', + }, + town => { + display => 'Sometown', + }, + county => { + display => 'Somewhere', + }, + comment => { + display => 'test data', + } +}; + +throws_ok { $out->add( ) } qr/need id/, 'add without params'; +throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds'; + +ok( $out->add( 42, $ds ), 'add 42' ); + +ok( $out->finish, 'finish' ); + -- 2.20.1