Class: RDF::Mongo::Conversion

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/mongo.rb

Class Method Summary collapse

Class Method Details

.from_mongo(value, value_type = :u, literal_extra = nil) ⇒ RDF::Value

Translate an BSON positional reference to an RDF Value.

Returns:

  • (RDF::Value)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rdf/mongo.rb', line 90

def self.from_mongo(value, value_type = :u, literal_extra = nil)
  case value_type
  when :u
    RDF::URI.intern(value)
  when :ll
    RDF::Literal.new(value, language: literal_extra.to_sym)
  when :lt
    RDF::Literal.new(value, datatype: RDF::URI.intern(literal_extra))
  when :l
    RDF::Literal.new(value)
  when :n
    @nodes ||= {}
    @nodes[value] ||= RDF::Node.new(value)
  when :default
    nil # The default context returns as nil, although it's queried as false.
  end
end

.to_mongo(value, place_in_statement) ⇒ Hash

Translate an RDF::Value type to BSON key/value pairs.

Parameters:

  • value (RDF::Value, Symbol, false, nil)

    URI, BNode or Literal. May also be a Variable or Symbol to indicate a pattern for a named graph, or false to indicate the default graph. A value of nil indicates a pattern that matches any value.

  • place_in_statement (:subject, :predicate, :object, :graph_name)

    Position within statement.

Returns:

  • (Hash)

    BSON representation of the statement



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rdf/mongo.rb', line 45

def self.to_mongo(value, place_in_statement)
  case value
  when RDF::URI
    v, k = value.to_s, :u
  when RDF::Literal
    if value.has_language?
      v, k, ll = value.value, :ll, value.language.to_s
    elsif value.has_datatype?
      v, k, ll = value.value, :lt, value.datatype.to_s
    else
      v, k, ll = value.value, :l, nil
    end
  when RDF::Node
    v, k = value.id.to_s, :n
  when RDF::Query::Variable, Symbol
    # Returns anything other than the default context
    v, k = nil, {"$ne" => :default}
  when false
    # Used for the default context
    v, k = false, :default
  when nil
    v, k = nil, nil
  else
    v, k = value.to_s, :u
  end
  v = nil if v == ''

  case place_in_statement
  when :subject
    t, k1, lt = :st, :s, :sl
  when :predicate
    t, k1, lt = :pt, :p, :pl
  when :object
    t, k1, lt = :ot, :o, :ol
  when :graph_name
    t, k1, lt = :ct, :c, :cl
  end
  h = {k1 => v, t => k, lt => ll}
  h.delete_if {|kk,_| h[kk].nil?}
end