Module: LinkedVocabs::Controlled

Defined in:
lib/linked_vocabs/controlled.rb

Overview

Adds add support for controlled vocabularies and QuestioningAuthority to RdfResource classes. @TODO: introduce graph context for provenance

Defined Under Namespace

Modules: ClassMethods Classes: ControlledVocabularyError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



12
13
14
15
16
# File 'lib/linked_vocabs/controlled.rb', line 12

def self.included(klass)
  klass.extend ClassMethods
  klass.property :hiddenLabel, :predicate => RDF::SKOS.hiddenLabel
  klass.validates_with LinkedVocabs::Validators::AuthorityValidator
end

Instance Method Details

#in_vocab?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/linked_vocabs/controlled.rb', line 54

def in_vocab?
  return false unless self.class.uses_vocab_prefix?(rdf_subject.to_s)
  self.class.vocabularies.each do |vocab, config|
    return false if rdf_subject == config[:prefix]
    return false if config[:class].strict? and not config[:class].respond_to? rdf_subject.to_s.gsub(config[:prefix], '').to_sym
  end
  true
end

#qa_interfaceObject



18
19
20
# File 'lib/linked_vocabs/controlled.rb', line 18

def qa_interface
  self.class.qa_interface
end

#rdf_labelObject



63
64
65
66
67
68
69
70
71
# File 'lib/linked_vocabs/controlled.rb', line 63

def rdf_label
  labels = Array(self.class.rdf_label)
  labels += default_labels
  labels.each do |label|
    values = get_values(label) if values.blank?
    return values unless values.empty?
  end
  node? ? [] : [rdf_subject.to_s]
end

#set_subject!(uri_or_str) ⇒ Object

Override set_subject! to find terms when (and only when) they exist in the vocabulary



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/linked_vocabs/controlled.rb', line 26

def set_subject!(uri_or_str)
  vocab_matches = []
  begin
    uri = get_uri(uri_or_str)
    uri_or_str = uri
  rescue RuntimeError, NoMethodError
  end

  self.class.vocabularies.each do |vocab, config|
    if uri_or_str.start_with? config[:prefix]
      # @TODO: is it good to need a full URI for a non-strict vocab?
      return super if config[:strict] == false
      uri_stub = uri_or_str.to_s.gsub(config[:prefix], '')
      return super(config[:class].send(uri_stub)) if config[:class].respond_to? uri_stub
    else
      # this only matches if the term is explictly defined
      # @TODO: what about the possibility of terms like "entries" or
      # "map" which are methods but not defined properties?  does
      # this need to be patched in RDF::Vocabulary or am I missing
      # something?
      vocab_matches << config[:class].send(uri_or_str) if config[:class].respond_to? uri_or_str
    end
  end
  return super if vocab_matches.empty?
  uri_or_str = vocab_matches.first
  return super if self.class.uses_vocab_prefix?(uri_or_str) and not uri_or_str.kind_of? RDF::Node
end