MooseX-Semantic

 view release on metacpan or  search on metacpan

lib/MooseX/Semantic/Role/RdfExport.pm  view on Meta::CPAN

    return $serializer;
}

=head2 export_to_string

C<export_to_string( %opts )>

For C<%opts>, see L<EXPORT OPTIONS> below.

=cut

sub export_to_string {
    my ($self, %opts) = @_;
    # HACK HACK HACK
    my $context = $opts{context} || 0;
    my $model = $self->export_to_model($opts{model}, context => $opts{context});
    # my $iter = $model->get_statements;
    # while ($_ = $iter->next) {
    #     warn Dumper [
    #         $_->subject->uri,
    #         $_->predicate->uri,
    #         $_->object->as_string,
    #     ];
    # }
    my $serializer = $self->_get_serializer(%opts)->serialize_model_to_string($model); 
}

=head2 export_to_file

TODO

=cut

sub export_to_file {
    my ($self, $fh, %opts) = @_;
    if (! ref $fh) {
        open $fh, ">", $fh;
    } elsif (ref $fh ne 'GLOB') {
        warn "can't open file for ref type " . ref $fh;
        return;
    }
    my $model = $self->export_to_model($opts{model}, context => $opts{context});
    # TODO prove that data was actually written out to $fh
    # and return undef otherwise
    $self->_get_serializer(%opts)->serialize_model_to_file($fh, $model); 
    return 1;
}

=head2 export_to_web

TODO

=cut

sub export_to_web {
    my ($self, $method, $uri, %opts) = @_;
    confess "Method must be PUT or POST" unless $method =~ /^(PUT|POST)$/;
    
    ### XXX: It would be handy if there were an application/sparql-update
    ###      serializer for Trine.
    ### kb Tue Nov 29 03:55:55 CET 2011
    #   started sparqlu_insert serializer
    my $ser = $self->_get_serializer(%opts);
    my ($type) = $ser->media_types;
    
    my $req = HTTP::Request->new(POST => $uri);
    $req->header(Content_Type => $type);
    my $model = $self->export_to_model($opts{model}, %opts);
    $req->content( $ser->serialize_model_to_string($model) );
    
    my $res = $self->_user_agent->request($req);
    $res->is_success or
        confess("<%s> HTTP %d Error: %s", $uri, $res->code, $res->message);
    return $res;
}

=head2 export_to_hash

TODO

=cut

sub export_to_hash {
    # warn Dumper [@_];<>;
    my ($self, %opts) = @_;
    my $self_hash = {};
    $opts{max_recursion} //= 0;
    $opts{hash_key} //= 'Moose';
    $self_hash->{rdf_about} = $self->rdf_about->uri;
    $self->_walk_attributes({
        # skip empty attributes;
        before => sub {
            my ($attr, $stash) = @_;
            push (@{$stash->{uris}}, @{$attr->uri_writer}) if $attr->has_uri_writer;
            return not $stash->{attr_val};
        },
        literal => sub {
            my ($attr, $stash) = @_;
            $self->_attr_to_hash( $self_hash, $attr, $stash->{attr_val}, %opts);
        },
        model => sub {
            my ($attr, $stash) = @_;
            $self_hash->{$attr->name} = $self->{$attr->name}->as_hashref;
        },
        resource => sub {
            my ($attr, $stash) = @_;
            my $self_hash_value = $self->export_to_hash( $stash->{attr_val}, %opts );
            $self_hash_value = [ map {$_->export_to_hash(%opts)} @{ $stash->{attr_val} } ];
        }
        ,
        literal_in_array => sub {
            my ($attr, $stash) = @_;
            $self->_attr_to_hash( $self_hash, $attr, $stash->{attr_val}, %opts);
        },
        resource_in_array => sub {
            my ($attr, $stash) = @_;
            my $self_hash_value;
            if ($opts{max_recursion}-- > 0) {
                $self_hash_value = [ map {$_->export_to_hash(%opts)} @{ $stash->{attr_val} } ];
            }
            else {



( run in 0.565 second using v1.01-cache-2.11-cpan-e1769b4cff6 )