Class: Note::SKOS::Base

Inherits:
Base
  • Object
show all
Defined in:
app/models/note/skos/base.rb

Overview

Copyright 2011 innoQ Deutschland GmbH

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_from_rdf(subject, predicate, object) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/note/skos/base.rb', line 21

def self.build_from_rdf(subject, predicate, object)
  unless subject.class.reflections.include?(self.name.to_relation_name)
    raise "Note::SKOS::Base#build_from_rdf: Subject (#{subject}) must be able to receive this kind of note (#{self.class.name} => #{self.class.name.to_relation_name})."
  end

  case object
  when String # Literal
    unless object =~ /^"(.*)"(@(.+))$/
      raise "Note::SKOS::Base#build_from_rdf: Object (#{object}) must be a string literal"
    end
    lang = $3
    value = JSON.parse(%Q{["#{$1}"]})[0].gsub("\\n", "\n") # Trick to decode \uHHHHH chars
    subject.send(self.name.to_relation_name) << self.new(:value => value, :language => lang)
  when Array # Blank node
    note = self.create!(:owner => subject)
    object.each do |annotation|
      ns, pred = *annotation.first.split(":", 2)
      note.annotations.create! do |a|
        a.namespace = ns
        a.predicate = pred
        a.value = annotation.last.match(/^"(.+)"$/)[1]
      end
    end
  end
end

Instance Method Details

#build_rdf(document, subject) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/note/skos/base.rb', line 47

def build_rdf(document, subject)
  ns, id = "", ""
  if (self.rdf_namespace && self.rdf_predicate)
    ns, id = self.rdf_namespace, self.rdf_predicate
  elsif self.class == Note::SKOS::Base # This could be done by setting self.rdf_predicate to 'note'. But all subclasses would inherit this value.
    ns, id = "Skos", "note"
  else
    raise "Note::SKOS::Base#build_rdf: Class #{self.class.name} needs to define self.rdf_namespace and self.rdf_predicate."
  end

  if (IqRdf::Namespace.find_namespace_class(ns))
    subject.send(ns).send(id, value, :lang => language)
  else
    raise "Note::SKOS::Base#build_rdf: couldn't find Namespace '#{ns}'."
  end
end