[libcatmandu-rdf-perl] 01/07: aref_query fix
Jonas Smedegaard
dr at jones.dk
Wed Oct 15 09:47:49 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository libcatmandu-rdf-perl.
commit e85ab8cd8be5d4a3594bd52d607a83e19b766a47
Author: Jakob Voss <voss at gbv.de>
Date: Tue Oct 14 12:52:55 2014 +0200
aref_query fix
---
Changes | 2 ++
META.json | 4 ++--
cpanfile | 2 +-
lib/Catmandu/Fix/aref_query.pm | 45 ++++++++++++++++++++++++++++++++++++++++++
lib/Catmandu/Importer/RDF.pm | 5 +++++
lib/Catmandu/RDF.pm | 2 +-
t/fix-aref-query.t | 24 ++++++++++++++++++++++
7 files changed, 80 insertions(+), 4 deletions(-)
diff --git a/Changes b/Changes
index c8dddf1..1e4411b 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
Changelog for Catmandu-RDF
{{$NEXT}}
+ - add field _url on import from url (issue #9)
+ - started fix function aref_query
0.17 2014-10-02 14:26:35 CEST
- Migrate to Dist::Milla
diff --git a/META.json b/META.json
index 4a258f7..0b5da74 100644
--- a/META.json
+++ b/META.json
@@ -40,7 +40,7 @@
"Catmandu" : "0.9",
"RDF::NS" : "20140910",
"RDF::Trine" : "1.0",
- "RDF::aREF" : "0.14"
+ "RDF::aREF" : "0.18"
}
}
},
@@ -56,7 +56,7 @@
"web" : "https://github.com/gbv/Catmandu-RDF"
}
},
- "version" : "0.17",
+ "version" : "0.18",
"x_contributors" : [
"Jakob Voss <voss at gbv.de>"
]
diff --git a/cpanfile b/cpanfile
index a43c378..2f4fca9 100644
--- a/cpanfile
+++ b/cpanfile
@@ -1,4 +1,4 @@
requires 'Catmandu', '0.9';
requires 'RDF::Trine', '1.0';
requires 'RDF::NS', '20140910';
-requires 'RDF::aREF', '0.14';
+requires 'RDF::aREF', '0.18';
diff --git a/lib/Catmandu/Fix/aref_query.pm b/lib/Catmandu/Fix/aref_query.pm
new file mode 100644
index 0000000..64e2616
--- /dev/null
+++ b/lib/Catmandu/Fix/aref_query.pm
@@ -0,0 +1,45 @@
+package Catmandu::Fix::aref_query;
+
+use Catmandu::Sane;
+use Moo;
+use RDF::aREF::Query;
+
+has query => (
+ is => 'ro',
+ coerce => sub {
+ RDF::aREF::Query->new( query => $_[0] )
+ # TODO: ns
+ }
+);
+has field => (is => 'ro');
+
+sub fix {
+ my ($self, $data) = @_;
+
+ my $field = $self->field;
+ my $origin = $data->{_uri} // $data->{_url};
+ my @values = $self->query->apply( $data, $origin );
+
+ if (@values) {
+ if (defined $data->{$field}) {
+ if (ref $data->{$field}) {
+ push @{$data->{$field}}, @values;
+ } else {
+ $data->{$field} = [ $data->{$field}, @values ];
+ }
+ } else {
+ $data->{$field} = @values > 1 ? \@values : $values[0];
+ }
+ }
+
+ $data;
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Catmandu::Fix::aref_query
+
+=cut
diff --git a/lib/Catmandu/Importer/RDF.pm b/lib/Catmandu/Importer/RDF.pm
index 683d25b..4c76098 100644
--- a/lib/Catmandu/Importer/RDF.pm
+++ b/lib/Catmandu/Importer/RDF.pm
@@ -54,6 +54,11 @@ sub generator {
}
}
$aref = $stream;
+
+ if ($self->url) {
+ $aref->{_url} = $self->url;
+ }
+
$stream = undef;
return $aref;
diff --git a/lib/Catmandu/RDF.pm b/lib/Catmandu/RDF.pm
index 07092dd..72e1dc6 100644
--- a/lib/Catmandu/RDF.pm
+++ b/lib/Catmandu/RDF.pm
@@ -6,7 +6,7 @@ use Catmandu::Util qw(is_instance);
use Moo::Role;
use RDF::NS;
-our $VERSION = '0.17';
+our $VERSION = '0.18';
our %TYPE_ALIAS = (
Ttl => 'Turtle',
diff --git a/t/fix-aref-query.t b/t/fix-aref-query.t
new file mode 100644
index 0000000..c95d5fb
--- /dev/null
+++ b/t/fix-aref-query.t
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+use Test::More;
+use Catmandu ':all';
+
+use_ok 'Catmandu::Fix::aref_query';
+
+my $fixer = Catmandu::Fix::aref_query->new(
+ field => 'title',
+ query => 'dc_title'
+);
+
+my $rdf = importer('RDF', file => 't/example.ttl')->first;
+($rdf->{_uri}) = keys $rdf;
+
+$fixer->fix( $rdf );
+delete $rdf->{ $rdf->{_uri} };
+
+is_deeply $rdf, {
+ '_uri' => 'http://example.org',
+ title => 'BAR'
+}, 'simple RDF fix';
+
+done_testing;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcatmandu-rdf-perl.git
More information about the Pkg-perl-cvs-commits
mailing list