Module: Redlander::Serializing

Included in:
Model
Defined in:
lib/redlander/serializing.rb

Overview

Syntax parsing methods. “self” is assumed to be an instance of Redlander::Model

Instance Method Summary collapse

Instance Method Details

#to(options = {}) ⇒ Object

Serialize model into a string

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :format (String)

    name of the serializer to use,

  • :mime_type (String)

    MIME type of the syntax, if applicable,

  • :type_uri (String, URI)

    URI of syntax, if applicable,

  • :base_uri (String, URI)

    base URI, to be applied to the nodes with relative URIs.

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/redlander/serializing.rb', line 14

def to(options = {})
  format = options[:format].to_s
  mime_type = options[:mime_type] && options[:mime_type].to_s
  type_uri = options[:type_uri] && options[:type_uri].to_s
  base_uri = options[:base_uri] && options[:base_uri].to_s

  rdf_serializer = Redland.librdf_new_serializer(Redlander.rdf_world, format, mime_type, type_uri)
  raise RedlandError, "Failed to create a new serializer" if rdf_serializer.null?

  begin
    if options[:file]
      Redland.librdf_serializer_serialize_model_to_file(rdf_serializer, options[:file], base_uri, @rdf_model).zero?
    else
      Redland.librdf_serializer_serialize_model_to_string(rdf_serializer, base_uri, @rdf_model)
    end
  ensure
    Redland.librdf_free_serializer(rdf_serializer)
  end
end

#to_dot(options = {}) ⇒ String

Serialize the model in Dot format. Shortcut for #to(:format => “dot”).

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


75
76
77
# File 'lib/redlander/serializing.rb', line 75

def to_dot(options = {})
  to(options.merge(:format => "dot"))
end

#to_file(filename, options = {}) ⇒ void

This method returns an undefined value.

Serialize the model to a file. Shortcut for #to(:format => “rdfxml”).

Parameters:

  • filename (String)

    path to the output file

  • options (Hash) (defaults to: {})

    (see #to options)



85
86
87
# File 'lib/redlander/serializing.rb', line 85

def to_file(filename, options = {})
  to(options.merge(:file => filename))
end

#to_json(options = {}) ⇒ String

Serialize the model in JSON format. Shortcut for #to(:format => “json”).

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


66
67
68
# File 'lib/redlander/serializing.rb', line 66

def to_json(options = {})
  to(options.merge(:format => "json"))
end

#to_ntriples(options = {}) ⇒ String

Serialize the model in NTriples format. Shortcut for #to(:format => “ntriples”).

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


48
49
50
# File 'lib/redlander/serializing.rb', line 48

def to_ntriples(options = {})
  to(options.merge(:format => "ntriples"))
end

#to_rdfxml(options = {}) ⇒ String

Serialize the model in RDF/XML format. Shortcut for #to(:format => “rdfxml”).

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


39
40
41
# File 'lib/redlander/serializing.rb', line 39

def to_rdfxml(options = {})
  to(options.merge(:format => "rdfxml"))
end

#to_turtle(options = {}) ⇒ String

Serialize the model in Turtle format. Shortcut for #to(:format => “turtle”).

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


57
58
59
# File 'lib/redlander/serializing.rb', line 57

def to_turtle(options = {})
  to(options.merge(:format => "turtle"))
end