Commits

Konstantin Baierer committed 0347686

rdf_formatter and rdf_parser meta-attributes for semantic attributes

Comments (0)

Files changed (3)

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

 #     predicate => 'has_rdfs_comment',
 # );
 
+=head2 rdf_formatter
+
+CodeRef of a function for coercing the value to a RDF literal. Defaults to the identity function.
+
+=cut
+
+has rdf_formatter => (
+    is => 'rw',
+    isa => 'CodeRef',
+    default => sub { sub{ return $_[0] }},
+);
+
+=head2 rdf_parser
+
+CodeRef of a function for parsing the literal value before importing this statement. Defaults to the identity function.
+
+=cut
+
+has rdf_parser => (
+    is => 'rw',
+    isa => 'CodeRef',
+    default => sub {sub{ return $_[0] }},
+);
+
 1;
 =head1 AUTHOR
 

lib/MooseX/Semantic/Role/RdfExport.pm

             },
             literal => sub {
                 my ($attr, $stash) = @_;
-                $self->_export_one_scalar( $model, $stash->{attr_val}, $_, $attr->rdf_lang, $attr->rdf_datatype, $opts{context})
+                my $val = $attr->rdf_formatter->($stash->{attr_val});
+                $self->_export_one_scalar( $model, $val, $_, $attr->rdf_lang, $attr->rdf_datatype, $opts{context})
                     for (@{ $stash->{uris} });
             },
             resource => sub {
             literal_in_array => sub {
                 my ($attr, $stash) = @_;
                 for my $subval ( @{$stash->{attr_val}} ) {
+                    $subval = $attr->rdf_formatter->( $subval );
                     $self->_export_one_scalar($model, $subval, $_, $attr->rdf_lang, $attr->rdf_datatype, $opts{context} )
                         for (@{ $stash->{uris} });
                 }
 sub _export_one_scalar {
     my $self = shift;
     my ($model,  $val, $rel, $lang, $datatype, $context) = @_;
+    # warn Dumper \@_;
     my $lit;
     if ($lang) {
         $lit = RDF::Trine::Node::Literal->new($val, $lang);

lib/MooseX/Semantic/Util/TypeConstraintWalker.pm

         #     && $type_constraint->{'__type_constraint'}->class eq $needle 
         # ) {
             # return $needle;
-            return $self->_find_parent_type_for_type_constraint($type_constraint->{'__type_constraint'}, $needle, %opts);
+        # warn Dumper [ keys(%{$type_constraint}->{'__type_constraint'}) ];
+        # warn Dumper {%{$type_constraint->{'__type_constraint'}}};
+        return $self->_find_parent_type_for_type_constraint($type_constraint->{'__type_constraint'}, $needle, %opts);
         # }
         # else {
         #     return
         # }
     }
     if ($type_constraint->has_parent) {
+        # warn Dumper {keys(%{$type_constraint})};
         return $self->_find_parent_type_for_type_constraint($type_constraint->parent, $needle, %opts );
     }
     else {
                 $callback_name = 'model';
             }
         }
+        elsif ($self->_find_parent_type($attr_type, 'Str')) {
+            $callback_name = 'literal';
+        }
         elsif ($self->_find_parent_type($attr->type_constraint, 'ArrayRef')) {
             if ( ! $attr_type->can('type_parameter')
                 || $attr_type->type_parameter eq 'Str'
         else {
             # warn Dumper $attr_type->has_parent;
             # warn Dumper $attr_type->parent;
-            # warn Dumper ref $attr_type;
+            warn Dumper ref $attr_type;
             # warn Dumper $self->_find_parent_type($attr_type, 'Object');
             warn "Can't handle this attribute: $attr_name";
             next;