Commits

Konstantin Baierer committed 867ff65

made rdf_formatter an optional attribute

Comments (0)

Files changed (5)

lib/MooseX/Semantic/Meta/Attribute/Trait.pm

 has rdf_formatter => (
     is => 'rw',
     isa => 'CodeRef',
-    default => sub { sub{ return $_[0] }},
+    predicate => 'has_rdf_formatter',
+    # default => sub { sub{ return $_[0] }},
 );
 
 =head2 rdf_parser

lib/MooseX/Semantic/Role/RdfExport.pm

             },
             literal => sub {
                 my ($attr, $stash) = @_;
-                my $val = $attr->rdf_formatter->($stash->{attr_val});
+                my $val = $stash->{attr_val};
+                if ($attr->has_rdf_formatter) {
+                    $val = $attr->rdf_formatter->( $val );
+                }
                 $self->_export_one_scalar( $model, $val, $_, $attr->rdf_lang, $attr->rdf_datatype, $opts{context})
                     for (@{ $stash->{uris} });
             },
             literal_in_array => sub {
                 my ($attr, $stash) = @_;
                 for my $subval ( @{$stash->{attr_val}} ) {
-                    $subval = $attr->rdf_formatter->( $subval );
+                    if ($attr->has_rdf_formatter) {
+                        $subval = $attr->rdf_formatter->( $subval );
+                    }
                     $self->_export_one_scalar($model, $subval, $_, $attr->rdf_lang, $attr->rdf_datatype, $opts{context} )
                         for (@{ $stash->{uris} });
                 }

lib/MooseX/Semantic/Role/RdfImport.pm

     return $resource_obj;
 }
 
+sub new_from_string {
+    my ($cls, $model_string, $uri, %opts) = @_;
+    $opts{format} //= 'nquads';
+    $opts{base_uri} //= 'urn:none:';
+    my $model = RDF::Trine::Model->temporary_model;
+    my $parser = RDF::Trine::Parser->new($opts{format});
+    $parser->parse_into_model($opts{base_uri}, $model_string, $model);
+    return $cls->new_from_model($model, $uri);
+}
+
 =head2 get_instance_hash
 
 Creates a hash of attribute/value pairs that can be passed to $cls->new

lib/MooseX/Semantic/Util/TypeConstraintWalker.pm

         # skip this attribute if the 'before' callback returns a true value
         next if $cb->{before}->($attr, $stash, @_);
         my $callback_name;
-        if ( $attr->can('rdf_formatter')
+        if ( $attr->has_rdf_formatter
             || ! $attr_type
             || $attr_type eq 'Str'
             || $self->_find_parent_type( $attr_type, 'Num' )

t/rdf_export/basic.t

 use Test::More tests=>10;
 use Test::Moose;
+use Carp::Always;
 use RDF::Trine qw(blank literal iri statement);
 use Data::Dumper;
 use MooseX::Semantic::Test::Person;